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');
}
}
Author by
Donald T
A software engineer who develops cutting-edge Web applications.
Updated on August 07, 2022Comments
-
Donald T almost 2 years
How can one get the ID's of every row in a grid, even across pages?
getDataIDs
andgetRowData
only gives the ID's of the current page.Thanks!
-
user1447718 over 10 yearsHi 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 over 10 years@user1447718: Yes you can use
$("#list").jqGrid('getGridParam', 'data');
to get data about all pages. If there are no column which containsid
information then you can use$("#list").jqGrid('getGridParam', '_index')
to get all ids and the index in thedata
array which is the data corresponds to the id. -
user1447718 over 10 yearsthanks 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 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 over 10 yearsThanks @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 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 over 10 yearsThanks @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 about 8 yearsOleg, you are my hero!
-
Oleg about 8 years@SergeyShabanov: Thanks!
-
Matt over 6 yearsNote 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 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 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 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 indata
array with the data item, which represents local data of the row. On the other sidegetDataIDs
returns array of ids and thus the items/ids are ordered in the array. -
Matt over 6 yearsYes, 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 over 6 yearsOne 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 over 6 years@Matt: First of all we should use the same terminology. Rowid is the value of
id
attribute of the row (theid
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 existidPrefix
option, which force, that the rowid will build from id of the source data and the prefix. All demos from the page usesidPrefix
. -
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 ofjsonReader
/xmlReader
/localReader
2) usage ofkey: true
property in one column ofcolModel
, 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 over 6 years@Matt: TreeGrid with
datatype !== "local"
(for exampledatatype: "json"
) still saves the data locally indata
and it used historically_id_
property to save rowid. Later one introducedloadonce: true
option, which saved loaded data also indata
option and it used_id_
property too. Later one introducedlocalReader
(likejsonReader
before), which allows to specifyid
used for local data, butlocalReader.id
will be reset to"_id_"
in case oftreeGrid:true
or if(datatype !== "local" && p.loadonce) || datatype === "xmlstring" || datatype === "jsonstring"
.