Get all row IDs in jqGrid

18,034

It is possible only if you have local grid (datatype:'local' or having loadonce:true). In the case all data inclusive ids for all pages are already locally. In the case you can use _index parameter, which will be used typically together with another more known parameter data. With

var idToDataIndex = $("#list").jqGrid('getGridParam','_index');

you will get the _index parameter. It is an object which has as the properties all ids of grid. So you can enumerate the ids with

var id;
for (id in idToDataIndex) {
    if (idToDataIndex.hasOwnProperty(id)) {
        // id is the rowid.
        // to get the data you can use
        // mydata[idToDataIndex[id]] where
        // var mydata = $("#list").jqGrid('getGridParam','data');
    }
}
Share:
18,034
Donald T
Author by

Donald T

A software engineer who develops cutting-edge Web applications.

Updated on August 07, 2022

Comments

  • Donald T
    Donald T almost 2 years

    How can one get the ID's of every row in a grid, even across pages?

    getDataIDs and getRowData only gives the ID's of the current page.

    Thanks!

  • user1447718
    user1447718 over 10 years
    Hi Oleg, my jqgrid has datatype:json and am reading the data via json reader. i have given loadonce = true, even then am also facing this issue where getRowData is returning only the current page's ids. How can i solve this problem? Thanks for your help.
  • Oleg
    Oleg over 10 years
    @user1447718: Yes you can use $("#list").jqGrid('getGridParam', 'data'); to get data about all pages. If there are no column which contains id information then you can use $("#list").jqGrid('getGridParam', '_index') to get all ids and the index in the data array which is the data corresponds to the id.
  • user1447718
    user1447718 over 10 years
    thanks Oleg. seeing strange thing though in my jqgrid. i have given the following Datatype:'json', loadonce:'true', cellsubmit:'clientArray'. the data is getting loaded in two pages. but when i do inspectelement, i see TRs getting created only for the first page. i don't see the TRs of second page. is that the reason why getRows is not returning data of second page while am in first page?
  • Oleg
    Oleg over 10 years
    @user1447718: You are welcome! It's all correct what you describe. For example you can insert 10000 rows or data in the grid and display the page with 10 rows. jqGrid will work very quickly and the user can very quickly do paging of filtering of the data (for example using filterToolbar). The reason of the good performance - the 10000 rows of data exists only as JavaScrip array of items and HTML table contains only 10 <tr> rows. Working with DOM elements (HTML elements) is much slowly as with JavaScript objects.
  • user1447718
    user1447718 over 10 years
    Thanks @Oleg. that explains why am not seeing other TRs. is there any property for each row that i can use to tag the operation done on that row. for example, if edit was doen, i want to tag that row as edited, so that when i do batch save, i want to identify only the tagged rows and send to server. earlier i was adding a custom attribute to TR tag to indicate the operation done on that row, but the prob is when i navigate between pages, that attribute goes off. is there any build in property like id that i can use to tag a row?
  • Oleg
    Oleg over 10 years
    @user1447718: It's better if you open new question and describe the problem which you try to solve in details. There are many ways to solve the problem which you have, but one need know details to give you recommendations which are really helpful for you. For example it's important to know how you fill the grid (one row of data for example). Is the operation "done" can by already in the input data (some rows need be shown but not edited)? Which exactly editing mode you use? and so on.
  • user1447718
    user1447718 over 10 years
    Thanks @Oleg. The demo is available here with the difference being in demo the data is loaded locally via js, but in actual code, jqgrid will make ajax call to server and get the data in json format. we can continue our discussion here.
  • Sergey Shabanov
    Sergey Shabanov about 8 years
    Oleg, you are my hero!
  • Oleg
    Oleg about 8 years
    @SergeyShabanov: Thanks!
  • Matt
    Matt over 6 years
    Note that .jqGrid('getDataIDs') gives you an array a[i] where i is the row number, and a[i] contains the grid key k. The function .jqGrid('getGridParam','_index') gives you the inverse array: a[k] gives you the row number i for a given grid key k.
  • Oleg
    Oleg over 6 years
    @Matt: The question was: Get all row IDs in jqGrid. The problem exist in case of local data and local paging. The method getDataIDs returns only the ids from the current page. The _index gives all ids.
  • Matt
    Matt over 6 years
    @Oleg - with that I am completely with you. Just wanted to mention that the data is inverse if you compare the functions with each other. And yes, only the one you mentioed returns all indices. The difference I mentioned was important when I replaced the function call in my code.
  • Oleg
    Oleg over 6 years
    @Matt: _index is object with ids as properties. Properties have no specific order. for (prop in _index) will enumerate properties in different oder in different web browsers (see here, for example). _index is just object with rowids as properties. The value of _index[rowid] is the index in data array with the data item, which represents local data of the row. On the other side getDataIDs returns array of ids and thus the items/ids are ordered in the array.
  • Matt
    Matt over 6 years
    Yes, and I also noticed that if you specify any column as key (key: true), then you have _index[keyid] which returns the number of the row. So it is no longer the row id, but the key value (I named it keyid before) in the specified column.
  • Matt
    Matt over 6 years
    One question - I came across with code that was using the _id_ property (contained in every data row). Does _id_ contain the row key as well?
  • Oleg
    Oleg over 6 years
    @Matt: First of all we should use the same terminology. Rowid is the value of id attribute of the row (the id of <tr> element). See here. If one have more as one tables on the page (or grid with subgrids) then simpale usage of native ids can follow id duplicates, which is an error in HTML. Thus there are exist idPrefix option, which force, that the rowid will build from id of the source data and the prefix. All demos from the page uses idPrefix.
  • Oleg
    Oleg over 6 years
    @Matt: One have to specify information about ids in the input data. Only if the ids are not specified then jqGrid generate unique values itself. There are multiple ways to specify id information: 1) usage id property of jsonReader/xmlReader/localReader 2) usage of key: true property in one column of colModel, which must have unique values in every row. I try to hold maximal compatibility with old jqGrid versions (<=4.7) to simplify upgrade to free jqGrid. Because of that all that is true for free jqGrid. Special case if _id_ property. It has mostly historical reason: TreeGrid
  • Oleg
    Oleg over 6 years
    @Matt: TreeGrid with datatype !== "local" (for example datatype: "json") still saves the data locally in data and it used historically _id_ property to save rowid. Later one introduced loadonce: true option, which saved loaded data also in data option and it used _id_ property too. Later one introduced localReader (like jsonReader before), which allows to specify id used for local data, but localReader.id will be reset to "_id_" in case of treeGrid:true or if (datatype !== "local" && p.loadonce) || datatype === "xmlstring" || datatype === "jsonstring".