How to return jqgrid data of selected rows
Asked Answered
E

2

7

In JQGrid

 var gridData=$("#SearchResults").jqGrid('getRowData')

The above line gives you the grid data of all the rows, is there a way where I can get the grid data of only the selected rows.

selRowId = myGrid.jqGrid ('getGridParam', 'selrow'),

The above gives the selected row IDs but I want the data as well of all the selected rows as it returns with gridData but I need only of those of selected one

Endocrine answered 13/9, 2014 at 18:44 Comment(0)
F
16

It's very simple. The second optional option parameter of getRowData method is rowid of the row which data is requested (see the documentation). So you can use

var selRowId = myGrid.jqGrid("getGridParam", "selrow");

to get last selected rowid first and then get the data of the row by

var rowData = myGrid.jqGrid("getRowData", selRowId);

If you use datatype: "local" or some remote datatype ("xml" or "json"), but with loadonce: true then jqGrid hold the data internally in data array. In the case the usage of getLocalRow method is more effective as the usage of getRowData:

var rowData = myGrid.jqGrid("getLocalRow", selRowId);

If you use multiselect: true option then jqGrid supports selarrrow array of selected rowids and you can get all required data in the loop:

var i, selRowIds = myGrid.jqGrid("getGridParam", "selarrrow"), n, rowData;
for (i = 0, n = selRowIds.length; i < n; i++) {
    rowData = myGrid.jqGrid("getLocalRow", selRowIds[i]);
    // one can uses the data here
}
Format answered 13/9, 2014 at 21:19 Comment(6)
in version 4.5.4, this doesn't return the correct row data, if the grid is sorted differently from original data. also id is the selected rows, id in onSelectRow: function (id, status, e) { ... }Endymion
@AaA: I'm sure that your are wrong. I suppose that you misunderstand what is rowid. Many newcomers mix the row index with rowid and additionally fill the grid in the wrong way (without specifying the id in the input data). I'd recommend you to post new question with the demo which demonstrates your problem. Additionally I'd recommend you to upgrade retro version 4.5.4 (which is more as 4 years old) to free jqGrid 4.15.2.Format
Thank you Oleg, I knew that. I think I mis-phrased it. I was refering to selRowId in your solution which is equal to id in onSelectRow event. in version 4.5.4 it returns incorrect row, however it is working correctly in 4.15.2.Endymion
@AaA: You are welcome! Upgrade to 4.15.2 should improve the performance additionally. I strictly recommend you the upgrade independent on your current problem. I still not sure that version 4.5.4 contains the bug in getLocalRow. I suppose that you have another problem. I could analyse it if you would provide the demo, which reproduces the problem.Format
Sure, I'll post it as a question and add a note here. Thank you againEndymion
I would be remiss if I did not point out the typo in the last example is in the library, so is correct: "selarrrow" does in fact have 3 character r's in a row. That took a while to debug.Siphonophore
E
1

If you are trying to grab your data from inside a grid event such as onSelectRow you can ignore the first part of Oleg's answer and get the data like following:

myGrid.jqGrid({
    ... // Grid create options ->
    datatype: 'local',
    data: gridData,  
    // <--
    onSelectRow: function(id){
        data = $(this).jqGrid("getLocalRow", id);

        // if you need actual content of the cells
        // data = myGrid.jqGrid("getRowData", id); 
    }
});

Obviously this only works on a single row, however if the intention is rows, I believe Oleg's answer is the only choice.

Note: this doesn't work in version 4.5.4 and below however it is fixed in oleg's free jqgrid 4.15.2

Endymion answered 2/11, 2017 at 3:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.