如何在jqGrid中快速加载数据

时间:2011-12-02 06:58:41

标签: jquery jqgrid

问题是在jqgrid中加载数据需要花费很多时间,目前我正在使用

for(var i=0;i<homeFileList.length;i++)
        jQuery("#tblHomeFileList").jqGrid('addRowData',i+1,homeFileList[i]);

将数据加载到网格中,但由于它是迭代的,所以需要花费很多时间,有没有办法更快地加载数据?

我看到addRowData甚至可以一次插入多个数据(Reading Link),我认为它可能更快,但它不会向我的网格插入任何内容

请参阅下面的代码。

var homeFileList=[];
$(xml).find('IPC').each(function(){ 
    $(this).children('homefilelist').each(function(){ 
        $(this).children('homefilelist_info').each(function(){ 
            var row={};
            isPresent=true;
            row.permission=$(this).attr('permission');
            row.hardlink=$(this).attr('hardlink');
            row.owner=$(this).attr('owner');
            row.group=$(this).attr('group');
            row.fsize=$(this).attr('fsize');
            row.month=$(this).attr('month');
            row.date=$(this).attr('date');
            row.time=$(this).attr('time');
            row.filename=$(this).attr('filename');
            homeFileList.push(row);
        });
    });
});

Oleg评论后更新

//HomeFileList
if(homeFileList.length>0)
{
    jQuery("#tblHomeFileList").jqGrid({
        datatype: "clientSide",
        colNames:['Permission','Hardlink','Owner','Group','Size','Month','Date','Year/Time','Filename'],
        colModel:[
            {name:'permission',index:'permission', align:"left",width:"100px"}, 
            {name:'hardlink',index:'hardlink', align:"left", width:"80px"},
            {name:'owner',index:'owner', align:"left",width:"100px"},
            {name:'group',index:'group', align:"left"},
            {name:'fsize',index:'fsize', align:"left", width:"90px"},
            {name:'month',index:'month', align:"left",width:"100px"},   
            {name:'date',index:'date', align:"left", width:"80px"},
            {name:'time',index:'time', align:"left",width:"100px"},
            {name:'filename',index:'filename', align:"left"}
        ],

                    pager : '#HomeFileListGridpager',
        rowNum:10,
        rowList:[10,50,100],
        scrollOffset:0,
        height: 'auto',
        autowidth:true,
        viewrecords: true,
        gridview: true
            });

    /*This for loop loads my data from homeFileList into grid
for(var i=0;i<homeFileList.length;i++)
        jQuery("#tblHomeFileList").jqGrid('addRowData',i+1,homeFileList[i]);*/

    alert($.isArray(homeFileList)); //returns true


    jQuery("#tblHomeFileList").jqGrid('addRowData',homeFileList);


    jQuery("#tblHomeFileList").setGridParam({rowNum:10}).trigger("reloadGrid");
}
else
    gridUnavailable("#tblHomeFileList");

XML响应(我已经缩短了下面的xml,因为它是homefilelist内的巨大数据)

<?xml-stylesheet type="text/xsl" href="client9.xsl"?><client version="1.0"><IPC>
<homefilelist>
    <homefilelist_info permission='-rwxr-xr-x' hardlink='1' owner='asimon' group='support' fsize='61597' month='Mar' date='22' time='2011' filename='libpmwspsvrcmn.so' />
    <homefilelist_info permission='-rwxr-xr-x' hardlink='1' owner='asimon' group='support' fsize='21778' month='Mar' date='22' time='2011' filename='libpmorablk.so' />
    <homefilelist_info permission='-rwxr-xr-x' hardlink='1' owner='asimon' group='support' fsize='36226' month='Mar' date='22' time='2011' filename='libpmjvm.so' />
</homefilelist>
</IPC>
</client>

2 个答案:

答案 0 :(得分:1)

您可以使用jqGrid的data参数:

$("#tblHomeFileList").jqGrid({
    datatype: "local",
    data: homeFileList,
    ...
});

the demo。如果网格的整个数据将被放置在网格中,第一页将被绘制。我相信你会立即看到大量行的性能差异。

我建议您在网格中另外添加formattersorttype属性。例如,保存整数数据的'fsize'的定义可以如下:

{name: 'fsize', index: 'fsize', width: 90, formatter: 'integer', sorttype: 'int'}

答案 1 :(得分:0)

如果您可以将数据转换为JSON / XML,则可以使用未记录的jqGrid.p.data属性快速添加它,然后只需设置它,然后重新加载网格。您需要确保您的数据已经符合您已映射的列。

if(_theGrid == undefined) {
    _theGrid = $("#list3").jqGrid({...});
}
else {
    _theGrid.p.data = data;
    $(_theGrid).trigger('reloadGrid');
}