Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqGrid Set Selected Rows

Tags:

jqgrid

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);
like image 845
kamaci Avatar asked Nov 29 '11 12:11

kamaci


3 Answers

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);
}
like image 169
Oleg Avatar answered Oct 09 '22 12:10

Oleg


$.each(rowsToSelect, function(_, rowId) {
    $grid.setSelection(rowId, false);
});

No much difference. Just seemed neater :)

like image 24
Robin Maben Avatar answered Oct 09 '22 12:10

Robin Maben


(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.

like image 2
Mike Gledhill Avatar answered Oct 09 '22 11:10

Mike Gledhill