Keep checked state of checkbox in custom formatted

2019-05-24 20:41发布

问题:

I have a load once jqgrid that uses a custom formatter to display checkboxes for bool values that are persisted only when a save button is clicked. However, whenever a sort is clicked the checked state of all the checkboxes is not kept.

jQuery("#list2").jqGrid({
    url:myurl,
    datatype: "json",
    loadonce: true,
    colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
    colModel:[
        {name:'id',index:'id', width:55},
        {name:'isChecked',index:'isChecked', width:90, formatter:chkFmatter},
        {name:'name',index:'name asc, invdate', width:100},
        {name:'amount',index:'amount', width:80, align:"right"},
        {name:'tax',index:'tax', width:80, align:"right"},      
        {name:'total',index:'total', width:80,align:"right"},       
        {name:'note',index:'note', width:150, sortable:false}       
    ],
    rowNum:10,
    rowList:[10,20,30],
    pager: '#pager2',
    sortname: 'id',
    viewrecords: true,
    sortorder: "desc",
    caption:"JSON Example"
});
jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});

function chkFmatter(cellvalue, options, rowObject) {
    // do something here to format column
    return new_format_value
}

Is there anyway that I can keep what checkboxes were clicked when sorting, paging, etc.?

回答1:

It is because the data that is bound to the grid is not being updated, so any new requests (paging, sorting, etc.) will reflect the original state that the property was bound to. Your best bet would be anytime the state of the checkbox changes to update the actual data to reflect this.

You can retrieve the data that is bound to the grid using getGridParam:

var data = $('#' + gridid).jqGrid('getGridParam', 'data');

Then to update it use your rowid, name of the column, and value to update the data object:

data[row - 1][columnname] = value;


标签: jqgrid