使 jqGrid 多选选择在分页、工具栏搜索或筛选后保持不变

2024-01-06

我有这个jqGrid http://jsfiddle.net/david_c/mUMGs/3/。以下是我想要的行为:

  1. 更改页面或执行搜索(工具栏或过滤器)后,选定的项目将保留选择(并如此呈现给用户)
  2. 当选择全选按钮时,如果当前页面上没有选择任何项目,则会选择所有项目。如果已经选择了一个项目,它将清除整个列表,无论是否在页面上。

  3. 单击“发票打印”按钮时,它将使用已创建的 ID 列表,或创建已选择的所有 IDS 的列表,无论是否在当前显示中。

如果不支持过滤器也是可以接受的,但这是首选。


可以肯定的是,我对 js 知之甚少,但以下是我尝试过的一些事情,但效果参差不齐:

  1. This answer https://stackoverflow.com/questions/4710780/jqgrid-multiselect-only-selects-rows-on-the-current-page-if-paging-is-enabled建议使用onSelectRow和onSelectAll,但我无法实现。see fail http://jsfiddle.net/david_c/ECtVw/

  2. This http://www.trirand.com/blog/?page_id=393/help/maintaining-checkbox-selections-through-a-page-operation/看起来很有希望,但只能解决分页问题。所以#1 看起来像是首选路线。问题#2的pastebin http://pastebin.com/1Yyf07xA


附:回到对js了解甚少。在我的项目中,函数 select_ids 的警报和未显示的功能确实有效,不知道为什么它不在 jsfiddle 中显示警报。提前抱歉,它需要修复,布朗尼指出,但是发布了分叉修复。

grid.jqGrid({
            datatype: "local",
            data: mydata,
            colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
            colModel:[
                {name:'id',index:'id', key: true, width:70, sorttype:"int"},
                {name:'invdate',index:'invdate', width:90, sorttype:"date"},
                {name:'name',index:'name', width:100},
                {name:'amount',index:'amount', width:80, align:"right",sorttype:"float"},
                {name:'tax',index:'tax', width:80, align:"right",sorttype:"float"},
                {name:'total',index:'total', width:80,align:"right",sorttype:"float"},
                {name:'note',index:'note', width:150, sortable:false}
            ],
            search:true,
            pager:'#pager',
            jsonReader: {cell:""},
            rowNum: 10,
            rowList: [5, 10, 20, 50],
            sortname: 'id',
            sortorder: 'asc',
            viewrecords: true,
            multiSort: true, 
            multiselect: true, 

            height: "100%",
            caption: "Invoice Print"
        });
        grid.jqGrid('navGrid','#pager',{add:false,edit:false,del:false,search:true,refresh:true},
                    {},{},{},{multipleSearch:true, multipleGroup:true, showQuery: true});
        grid.jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false, defaultSearch:"cn"});

这是脚本。onSelectAll and onSelectRow允许保存恢复的状态gridComplete

$(function () {

            var selectedRows = {};
            var agentsGrid = $('#agentsTbl');
            agentsGrid.jqGrid({
                height: 400,
                datatype: 'local',
                multiselect: true,
                ignoreCase: true,
                colNames: [
                    'isn', 'Agent', 'Type', 'Country', 'Plan', 'Date To'],
                colModel: [
                    { name: 'isn', index: 'isn', hidden: true, key: true, align: 'center' },
                    { name: 'agentName', index: 'agentName', align: 'center', search: true, stype: 'text', searchoptions: { sopt: ['cn'] } },
                    { name: 'agentType', index: 'agentType', hidden: true },
                    { name: 'country', index: 'country', align: 'center', search: true, width: 100, fixed: true },
                    { name: 'scheme', index: 'scheme', align: 'center', search: true, stype: 'text', searchoptions: { sopt: ['cn'] } },
                    { name: 'dateTo', index: 'dateTo', align: 'center', search: false }
                ],
                grouping: true,
                groupingView: {
                    groupField: ['agentType'],
                    groupDataSorted: true,
                    groupColumnShow: false
                },
                // to save selection state
                onSelectAll: function (rowIds, status) {
                    if (status === true) {
                        for (var i = 0; i < rowIds.length; i++) {
                            selectedRows[rowIds[i]] = true;
                        }
                    } else {
                        for (var i = 0; i < rowIds.length; i++) {
                            delete selectedRows[rowIds[i]];
                        }
                    }
                },
                onSelectRow: function (rowId, status, e) {
                    if (status === false) {
                        delete selectedRows[rowId];
                    } else {
                        selectedRows[rowId] = status;
                    }

                },
                gridComplete: function () {
                    for (var rowId in selectedRows) {
                        agentsGrid.setSelection(rowId, true);
                    }
                }
            });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使 jqGrid 多选选择在分页、工具栏搜索或筛选后保持不变 的相关文章

随机推荐