问题
I have this jqGrid. The following is my desired behavior:
- The selected item will have the selection persist (and be rendered as such to the user) after changing pages or doing a search (toolbar or filter)
When the select all button is selected, if no items on the current page are selected, it selects them all. If there is an item already selected, it will clear the entire list, whether on the page or not.
When the Invoice Print button is clicked, it will either use the list of IDs that has been created as we go, or create a list of all IDS that have been selected, whether in the current display or not.
It would be acceptable if filter was not supported, but preferred.
To be sure, I know little about js, but here are some things that I have tried with mixed success:
This answer suggests to use the onSelectRow and onSelectAll, but I was unable to implement. see fail
This looks promising, but only will fix the pagination thing. So #1 looks like a prefered route. pastebin for question #2
P.S. back to the know little about js. in my project the alert and not shown functionality of the function select_ids does work, not sure why it is not displaying the alert in the jsfiddle. so sorry in advance that it needs repair, brownie points to however posts the forked fix.
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"});
回答1:
here is the script. onSelectAll
and onSelectRow
allow to save the state which is restored in 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);
}
}
});
来源:https://stackoverflow.com/questions/18502592/make-jqgrid-multiselect-selection-persist-following-pagination-toolbar-search