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

时间:2015-06-22 18:49:15

标签: javascript jquery jqgrid

我有一个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"});    
});

重现的步骤:

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

问题:

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

环境: jqgrid 4.6.0,jquery 1.11.1和IE 9

1 个答案:

答案 0 :(得分:1)

您必须使用addRow method来附加数据。这是一个更新的小提琴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); 
});