jqGrid Set Selected Rows
Asked Answered
B

3

8

I have a jqgrid with multiselect true and I want to set some of rows.(I know the row ids.) How can I do that?

I mean opposite of

$("#myTable").jqGrid('getGridParam', 'selarrrow');

as like:

$("#myTable").jqGrid('setGridParam', 'selarrrow', rowArray);
Brechtel answered 29/11, 2011 at 12:0 Comment(0)
C
10

You have to loop through the rowArray array and call setSelection method for every rowid from the rowArray:

var i, count, $grid = $("#myTable");
for (i = 0, count = rowArray.length; i < count; i += 1) {
    $grid.jqGrid('setSelection', rowArray[i], false);
}
Choroiditis answered 29/11, 2011 at 13:1 Comment(1)
Oleg, This works well if applied for a few row. If I use a for-loop for about 1000 rows, the browser crashes. Is there any way to handle this problem without using for loop?Hunsinger
R
5
$.each(rowsToSelect, function(_, rowId) {
    $grid.setSelection(rowId, false);
});

No much difference. Just seemed neater :)

Roid answered 12/7, 2012 at 11:22 Comment(1)
or any recent browser: rowsToSelect.forEach(function(rowId){$grid.setSelection(rowId, false);});Resultant
W
2

(Pretty amazing that even now, in 2014, jqGrid doesn't persist checkboxes when paging..)

Here's the code I needed to use, with jqGrid 4.4.5, to get the checkboxes to set, after moving to a new page:

var idsOfSelectedRows = [];   //  list of RowIDs for rows which have been ticked

$("#tblContracts").jqGrid({
      ...   
      colModel: [
          { name: 'AddContract', width: 50, align: "center", editable: true, edittype: 'checkbox', editoptions: { value: "True:False" }, formatter: "checkbox", formatoptions: { disabled: false } },
          { name: "ContractName", search: true, width: 80, align: "center" }
      ],
      loadComplete: function () {
          for (i = 0; i < idsOfSelectedRows.length; i++) {
              $(this).setCell(idsOfSelectedRows[i], 'AddContract', true);
          }
      },

During development, I put an "alert" in that "for" loop. I found that using "setSelection" simply stepped through my list of RowIDs, selected the row (so it would become highlighted), then moved on to the next, selecting that one instead.

It didn't ever tick any of the checkboxes.

Notice that my "setCell" function includes the name of the jqGrid column where I have a checkbox.

If you cut'n'paste this code, make sure you change this line to reflect the name of your jqGrid checkbox column.

Warner answered 13/6, 2014 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.