jqGrid Set Selected Rows

試著忘記壹切 提交于 2019-12-30 08:25:21

问题


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);

回答1:


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);
}



回答2:


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

No much difference. Just seemed neater :)




回答3:


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



来源:https://stackoverflow.com/questions/8310339/jqgrid-set-selected-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!