jqgrid multiselect only selects rows on the current page, if paging is enabled. How to make it select rows across pages?

天涯浪子 提交于 2019-11-30 09:18:55

I know this question is kind of dusty, but I recently had a need for this functionality and found what I consider a much more clean way to do it.

Instead of attaching an event to each checkbox, why not use the onSelectRow and onSelectAll events of the jqGrid? When a row is selected, check if our list of selected rows includes this row (based on id). Add it to the list if it was not there and has been selected, remove it if it was there and is no longer selected. If all rows are selected, iterate through them.

var $grid  = jQuery("#myGrid");
var updateIdsOfSelectedRows = function (id, isSelected) {
    var contains = idsOfSelectedRows.contains(id);
    if (!isSelected && contains) {
        for(var i=0; i<idsOfSelectedRows.length; i++) {
            if(idsOfSelectedRows[i] == id) {
                idsOfSelectedRows.splice(i, 1);
                break;
            }
        }
    }
    else if (!contains) {
        idsOfSelectedRows.push(id);
    }
};

$grid.jqGrid({
    ....
    onSelectRow: function(rowid, status){
        updateIdsOfSelectedRows(rowid, status);
    },
    onSelectAll: function (aRowids, status) {
        var i, count, id;
        for (i = 0, count = aRowids.length; i < count; i++) {
            id = aRowids[i];
            updateIdsOfSelectedRows(id, status);
        }
    },
    ....
 )};

Hope this helps others looking for a solution.

I'm assuming you would like to do select some rows in one page, then go to another page, maybe select some more rows, and have all of those selection persisted.

You will have to handle the selection events on each check box. Attach an event handler to each $("#cb_my_grid") and persist an array of selected items. Then when you need the data (maybe when the page is submitted) you can submit the values in the array.

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