我有一个简单的子网格作为网格实现,并且父网格有超过 1 页。
以下是测试代码:
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css"/>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/grid.locale-en.js" ></script>
<script type="text/javascript" src="js/jquery.jqGrid.min.js" ></script>
</head>
<body>
<table id="grid1">
<div id="pager1"></div>
</table>
<script type="text/javascript">
jQuery("#grid1").jqGrid({
datatype: "local",
height: 250,
rowNum:5,
rowList:[5,10,15],
cellEdit:true,
cellsubmit: 'clientArray',
pager: '#pager1',
colNames:['Inv No', 'Client','Notes','Checked?' ],
colModel:[
{name:'id',index:'id', width:60, sortable:false},
{name:'name',index:'name', width:100, sortable:false},
{name:'note',index:'note', width:150, sortable:false, editable:true},
{name:'ind_checked',index:'ind_checked', width:100, sortable:false, align:'center', editable:true,
edittype:'checkbox', editoptions: { value:"Yes:No" }, formatter:'checkbox'}
],
caption: "Testing",
loadonce: true,
subGrid : true,
subGridOptions: { reloadOnExpand : false},
subGridRowExpanded: function(subgrid_id, parent_rowid) {
subgrid_table_id = subgrid_id+"_t";
subgrid_pager_id = 'p_'+subgrid_table_id;
jQuery("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'></table><div id='" + subgrid_pager_id + "'></div>");
jQuery("#"+subgrid_table_id).jqGrid({
datatype: "local",
height: "100%",
cellEdit:true,
cellsubmit: 'clientArray',
pager: '#'+subgrid_pager_id,
colNames:['Inv No','Item No', 'Qty'],
colModel:[
{name:'id',index:'id', width:60, sortable:false},
{name:'icode',index:'icode', width:60, sortable:false},
{name:'qty',index:'qty', width:100, sortable:false, editable:true}
],
loadonce: true,
loadComplete : function() {
var dataIds = $('#grid1').jqGrid('getDataIDs');
var data = $('#grid1').jqGrid('getRowData',dataIds[parent_rowid-1]);
loadDetail(data.id);
}
});
}
}).navGrid('#pager1');
var mydata = [
{id:"1",name:"test1",note:"note1",ind_checked:"yes"},
{id:"2",name:"test2",note:"note2",ind_checked:"no"},
{id:"3",name:"test3",note:"note3",ind_checked:"yes"},
{id:"4",name:"test4",note:"note4",ind_checked:"yes"},
{id:"5",name:"test5",note:"note5",ind_checked:"no"},
{id:"6",name:"test6",note:"note6",ind_checked:"no"},
{id:"7",name:"test7",note:"note7",ind_checked:"yes"},
{id:"8",name:"test8",note:"note8",ind_checked:"no"},
{id:"9",name:"test9",note:"note9",ind_checked:"no"} ]
;
for(var i=0;i<mydata.length;i++) {
jQuery("#grid1").jqGrid('addRowData',i+1,mydata[i]);
}
var mySGdata = [
{id:"1",icode:"item1",qty:10},
{id:"1",icode:"item2",qty:10},
{id:"3",icode:"item1",qty:10},
{id:"3",icode:"item2",qty:10},
{id:"3",icode:"item3",qty:10},
{id:"4",icode:"item2",qty:10},
{id:"7",icode:"item1",qty:10},
{id:"7",icode:"item3",qty:10},
{id:"9",icode:"item3",qty:10} ]
;
function loadDetail(id) {
for (var i=0; i<mySGdata.length; i++) {
if (mySGdata[i].id==id) {
//alert('Add subgridRow');
jQuery("#"+subgrid_table_id).jqGrid('addRowData',i+1,mySGdata[i]);
}
}
}
</script>
</body>
</html>
问题是如果父网格更改了页面,子网格将重新加载本地数据,即使我添加了 subGridOptions: { reloadOnExpand : false} 和 loadonce:ture 选项em>
比方说,您可以更改子网格中的数量,然后折叠和展开它。子网格不会重新加载本地数据,并且仍然保留我的更改。
但是,如果我切换到下一页并切换回上一页,则当我展开子网格时,子网格将再次重新加载本地数据。所以,我失去了所有的改变。
有人有任何想法和解决方法,或者我缺少什么吗?请建议我。谢谢。
要测试我的脚本,请单击 rowList 并使网格首先包含 5 行并有 2 个页面,因为我不知道如何使页面/容器从本地数据开始显示 5 行。 如果有人知道怎么做,这对我来说也是一个奖励。谢谢!
更新: 我发现这不仅与页面更改有关,当我更改 rowList(页面上显示多少行)时,它还会强制子网格重新加载。 有什么办法可以避免这种行为吗?请。谢谢
最佳答案
首先,我将首先创建数据数组并直接在网格选项中设置它们(如 this example )。那么行数就可以了,不需要额外的javascript代码来加载。
否则,您必须在添加数据后重新加载网格才能正确获取行数:
jQuery("#grid1").trigger("reloadGrid");
另请参阅此 reload example .
<小时/>对于您的主要问题:选项 loadonce
和寻呼机不能一起工作(请参阅 this link )。
解决方法:我认为您必须在每次数据更改之后或每次分页之前更改数据变量(例如,您可以使用 onPaging
事件)。
关于jquery - jqGrid - 如果父网格页面更改,则重新加载子网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8826422/