gpt4 book ai didi

javascript - jqgrid:动态添加记录后排序不起作用

转载 作者:行者123 更新时间:2023-11-28 00:09:57 36 4
gpt4 key购买 nike

我有一个 jqgrid,最初填充了一些记录。用户可以选择向网格添加更多行。但是,添加新行后,如果用户尝试对记录进行排序,结果会不一致,并且某些记录会在网格中丢失。

fiddler :https://jsfiddle.net/99x50s2s/68/

HTML:

<table id="sg1"></table>
<div id="psg1"></div>
</br>
<button id="new" type="button">Add New</button>

代码:

jQuery("#sg1").jqGrid({
datatype: "local",
gridview: true,
loadonce: true,
shrinkToFit: false,
autoencode: true,
height: 'auto',
viewrecords: true,
sortorder: "desc",
scrollrows: true,
loadui: 'disable',
colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
colModel:[
{name:'Id',index:'Id', width:60, 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}
],
caption: "Test Grid"
});

var initialData = [
{Id:"1",invdate:"2007-10-01",name:"test 12349",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{Id:"2",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}
];

var subData = [
{Id:"3",invdate:"2007-10-01",name:"test 12349",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{Id:"4",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}
];

jQuery("#sg1")[0].addJSONData(initialData);

var i=2;

$('#new').on('click', function(){
i = i+i;
jQuery("#sg1").jqGrid(
'addRowData',
$.jgrid.randId(),
{Id:i,invdate:"2007-10-02",name:"test"+i,note:"note" + i,amount:"300.00",tax:"20.00",total:"320.00"});
});

重现步骤:

a.启动 fiddler(您将看到一个包含 2 条记录的测试网格)b.现在,单击“添加新项”按钮,然后尝试通过单击任意列标题进行排序。

问题:

代码有问题吗?为什么有些记录消失了?

环境:jqgrid 4.6.0、jquery 1.11.1 和 IE 9

最佳答案

您必须使用addRow method附加数据。这是更新的 fiddle https://jsfiddle.net/99x50s2s/69/

$('#new').on('click', function(){
i = i+i;
var parameters =
{
initdata : {Id:i,invdate:"2007-10-02",name:"test"+i,note:"note" + i,amount:"300.00",tax:"20.00",total:"320.00"},
position :"last"
};
jQuery("#sg1").jqGrid('addRow',parameters);
});

关于javascript - jqgrid:动态添加记录后排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987559/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com