简体   繁体   English

ASP.NET MVC和jqGrid:持久化多选

[英]ASP.NET MVC and jqGrid: Persisting Multiselection

I have a jqGrid in an ASP.NET MVC View with the option multiselect:true . 我在ASP.NET MVC视图中有一个jqGrid,选项为multiselect:true There are over 200 records displayed in the grid, so I have paging enabled. 网格中显示了200多条记录,因此我启用了分页。 This works great, but when I navigate from page to page, the selections are lost when I navigate. 这很好用,但是当我从一个页面导航到另一个页面时,我导航时选择会丢失。

Is there a good, clean way to persist the selections so that they are maintained while paging? 是否有一种良好,干净的方式来保留选择,以便在分页时保持它们?

Managed it with some javascript trickery: 用一些javascript技巧管理它:

    var pages = [];
    onSelectRow: function(rowid, status) {
                    var pageId = $('#grdApplications').getGridParam('page');
                    var selRows = [];
                    if (status) {
                        //item selected, add index to array
                        if (pages[pageId] == null) {
                            pages[pageId] = [];
                        }
                        selRows = pages[pageId];

                        if (selRows.indexOf(rowid) == -1)
                        { selRows.push(rowid); }
                    }
                    else {
                        //item deselected, remove from array
                        selRows = pages[pageId];
                        var index = selRows.indexOf(rowid)
                        if (index != -1) {
                            pages[pageId].splice(index, 1);
                        }
                    }
                },

loadComplete: function() {
                if (pages[$('#grdApplications').getGridParam('page')] != null) {
                    var selRows = pages[$('#grdApplications').getGridParam('page')];
                    var i;
                    var limit = selRows.length;
                    for (i = 0; i < limit; i++) {
                        $('#grdApplications').setSelection(selRows[i], true);
                    }
                }
            },

user279248 (I know it's an old post, but it's a good question) - all of the row ids are being stored in the selRows arrays in the pages array, so just iterate through them, ie user279248(我知道这是一个老帖子,但这是一个很好的问题) - 所有行id都存储在pages数组中的selRows数组中,所以只需遍历它们,即

for (j=0;j<pages.length;j++) { 
  var selRow = pages[j];
  for (k=0;k<selRow.length;k++) {
   alert('RowID:'+selRow[k]);
  }
}

Hope this helps someone. 希望这有助于某人。

Dave - your solution is still going strong two years later! 戴夫 - 两年后你的解决方案仍然很强劲! Thanks for the code. 谢谢你的代码。 My only tweak is elevating the code into functions - useful to apply to multiple grids on the same page. 我唯一的调整是将代码提升为函数 - 对于应用于同一页面上的多个网格非常有用。

function maint_chkbxs_oSR(obj_ref, rowid, status, pages) {

        var pageId = $(obj_ref).jqGrid('getGridParam','page');
        var selRows = [];
        if (status) {
            //item selected, add index to array
            if (pages[pageId] == null) {
                pages[pageId] = [];
            }
            selRows = pages[pageId];

            //if (selRows.indexOf(rowid) == -1)
            if ($.inArray(""+rowid,selRows) == -1)
            { selRows.push(rowid); }
        }
        else {
            //item deselected, remove from array
            selRows = pages[pageId];
            var index = $.inArray(""+rowid,selRows);
            if (index != -1) {
                pages[pageId].splice(index, 1);
            }
        }   
    }

    function maint_ckbxs_lC(obj_ref, pages) {

        if (pages[$(obj_ref).jqGrid('getGridParam','page')] != null) {
            var selRows = pages[$(obj_ref).jqGrid('getGridParam','page')];
            var i;
            var limit = selRows.length;
            for (i = 0; i < limit; i++) {
                //$('#grid_bucket').setSelection(selRows[i], true);
                $(obj_ref).jqGrid('setSelection',selRows[i],true);
            }
        }   
    }

You just have to remember to create a dedicated page array for each grid. 您只需要记住为每个网格创建一个专用的页面数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM