简体   繁体   English

如果数据来自单独的ajax调用,如何为jqGrid添加服务器端分页?[Trirand JQgrid 4.6]

[英]How do i add server side paging for jqGrid if data comes from a separate ajax call?[Trirand JQgrid 4.6]

I need to generate a jqGrid structure that similar to Grouped Header Row Config example in trirand JqGrid 4.6 demo website. 我需要生成一个jqGrid的结构类似于分组标题行配置例如trirand的jqGrid 4.6演示网站。

在此处输入图片说明

In this i have a separate ajax call to get data and the ajax calls a ASP MVC action method. 在这种情况下,我有一个单独的ajax调用来获取数据,而ajax调用ASP MVC操作方法。

$.ajax({
url:'Controller/Action/ClientId?='+ id,
method:'get',
success:function(data){

var gridData = data;

jQuery("#list483").jqGrid({
    datatype: "local",
    height: 'auto',
    rowNum: 30,
    rowList: [10,20,30],
    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", formatter:"date"},
        {name:'name',index:'name', width:100, editable:true},
        {name:'amount',index:'amount', width:80, align:"right",sorttype:"float", formatter:"number", editable:true},
        {name:'tax',index:'tax', width:80, align:"right",sorttype:"float", editable:true},      
        {name:'total',index:'total', width:80,align:"right",sorttype:"float"},      
        {name:'note',index:'note', width:150, sortable:false}       
    ],
    gridComplete:function(){


                        var dataArray = $('#list483').jqGrid('getGridParam', 'data');

//do some processing here

},
    pager: "#plist483",
    viewrecords: true,
    sortname: 'name',
    grouping:true,
    groupingView : {
        groupField : ['name'],
        groupColumnShow : [false],
        groupText : ['<b>{0} - {1} Item(s)</b>']
    },
    caption: "Configure group header row"
});


//load data to grid here
for(var n=0;n<gridData .length;n++){
                    jQuery("#list483").jqGrid('addRowData', n + 1, gridData [n]);
                }





}


})

assume the data that comes from ajax is same as the array in demo website as paste down here: 假设来自ajax的数据与演示网站中的数组相同,如下所示:

 [
        {id:"1",invdate:"2010-05-24",name:"test",note:"note",tax:"10.00",total:"2111.00"} ,
        {id:"2",invdate:"2010-05-25",name:"test2",note:"note2",tax:"20.00",total:"320.00"},
        {id:"3",invdate:"2007-09-01",name:"test3",note:"note3",tax:"30.00",total:"430.00"},
        {id:"4",invdate:"2007-10-04",name:"test",note:"note",tax:"10.00",total:"210.00"},
        {id:"5",invdate:"2007-10-05",name:"test2",note:"note2",tax:"20.00",total:"320.00"},
        {id:"6",invdate:"2007-09-06",name:"test3",note:"note3",tax:"30.00",total:"430.00"},
        {id:"7",invdate:"2007-10-04",name:"test",note:"note",tax:"10.00",total:"210.00"},
        {id:"8",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"21.00",total:"320.00"},
        {id:"9",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
        {id:"11",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
        {id:"12",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
        {id:"13",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
        {id:"14",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
        {id:"15",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
        {id:"16",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
        {id:"17",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
        {id:"18",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
        {id:"19",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
        {id:"21",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
        {id:"22",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
        {id:"23",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
        {id:"24",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
        {id:"25",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
        {id:"26",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
        {id:"27",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
        {id:"28",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
        {id:"29",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}
    ]

Assume the above data array has 1000 objects. 假设上面的数据数组有1000个对象。 so i need server side paging . 所以我需要服务器端分页 But how do i add server side paging to this external ajax call ? 但是,如何在此外ajax调用中添加服务器端分页

Once add server side paging the following must work: 添加服务器端分页后,以下各项必须起作用:

  1. Client side sorting must work 客户端排序必须有效
  2. RowList must work 行列表必须工作

thanks 谢谢

The main problem: you posted some dummy code, which probably has no relation to your real code. 主要问题:您发布了一些伪代码,这些伪代码可能与您的真实代码无关。 It's better to t the code, which more close to your real code and just to replace the data to any dummy data in the same format. 最好编写代码,使其更接近真实代码,而只是将数据替换为相同格式的任何伪数据。

The most problem: you use datatype: "local", but write about the requirement to implement server side paging. 最大的问题:您使用的数据类型为:“本地”,但是写了实现服务器端分页的要求。 It's not consequent. 这不是结果。 Moreover, you fill the grid using addRowData called in the loop. 此外,您可以使用addRowData调用的addRowData填充网格。 It's the worst and the slowest way, which I know. 我知道这是最坏,最慢的方式。 If you use datatype: "local" , then you should use data: gridData to fill the data. 如果使用datatype: "local" ,则应使用data: gridData填充数据。 In the way you create the grid with the data. 用您创建带有数据的网格的方式。 jqGrid will sort the input data first by groupingView.groupField , then by sortname and to display the first page of the resulting data in the grid. jqGrid将首先通过groupingView.groupField排序输入数据,然后通过sortname并在网格中显示结果数据的第一页。

I'd recommend you additionally to use gridview: true and autoencode: true in all your grids to have better performance and to display the data correctly if you load pure data, which contains no HTML fragments. 我建议您另外在所有网格中使用gridview: trueautoencode: true ,以提高性能并在加载纯数据(不包含HTML片段)时正确显示数据。 You recommend to remove unneeded index properties from colModel . 您建议从colModel删除不需要的index属性。

I recommend you to post demos in JSFiddle , which will simplify analyzing of your problems and preparing the fix of there. 我建议您在JSFiddle中发布演示,这将简化对问题的分析并准备解决方法。 You can use Echo service of JSFiddle additionally(see here ). 您可以另外使用JSFiddle的Echo服务(请参阅此处 )。 I created the demo https://jsfiddle.net/OlegKi/mkgtuuzy/ for you, which uses almost your code and the following options additionally 我为您创建了一个演示https://jsfiddle.net/OlegKi/mkgtuuzy/ ,该演示几乎使用了您的代码和以下选项

sortname: 'id',
gridview: true,
autoencode: true,

I would recommend you additionally, don't use retro version 4.6. 我会另外建议您,不要使用复古版本4.6。 Instead of that you can use free jqGrid 4.13.4. 相反,您可以使用免费的jqGrid 4.13.4。 It's the fork, which I develop since the end of 2014, after renaming the main jqGrid fork to Guriddo jqGrid JS (see the post ), changing the license agreement and making it commercial (see the prices here ). 这是我在2014年末开发的分叉,在将主jqGrid分叉重命名为Guriddo jqGrid JS(请参阅post ),更改了许可协议并使之商业化之后(请参见此处的价格)。 Free jqGrid 4.13.4 is full compatible with the old 4.6, but it contains a lot of fixes, performance improvements and many new features described in README to every published version and the wiki . 免费的jqGrid 4.13.4与旧的4.6完全兼容,但是它包含许多修复程序,性能改进以及每个发行版和wiki的 README中描述的许多新功能。 I recommend you to read the article and the wiki articles this one and this one too, which described some small, but important improvements, which you can use in free jqGrid additionally. 我建议你阅读文章和维基文章这一次这一次也一样,这说明一些小的,但重要的改进,你可以在免费的jqGrid另外使用。

The demo https://jsfiddle.net/OlegKi/mkgtuuzy/1/ uses the same code as the previous page, but includes CSS of Font Awesome 4.7, free jqGrid 4.13.4 and the option iconSet: "fontAwesome" . 演示https://jsfiddle.net/OlegKi/mkgtuuzy/1/使用与上一页相同的代码,但包含Font Awesome 4.7的CSS,免费jqGrid 4.13.4和选项iconSet: "fontAwesome"

About the requirement of server side paging you should good understand that 1000 rows of data is a small dataset for jqGrid if you fill the data correctly. 关于服务器端分页的要求,您应该很好地理解,如果正确填充数据,则jqGrid的数据集为1000行。 The demo demonstrates the performance of free jqGrid with 5000 rows, 13 columns and 25 rows per page. 该演示演示了免费jqGrid的性能,每页5000行,13列和25行。 You can see the performance of local paging, sorting and filtering the data. 您可以看到本地分页,排序和过滤数据的性能。 If you would load compressed (gziped) JSON data loading from the server and add loadonce: true (and forceClientSorting: true in free jqGrid) then you can easy load all the data using datatype: "json" directly from the url . 如果要从服务器加载压缩(gziped)的JSON数据并添加loadonce: true (和在免费jqGrid中为forceClientSorting: true ),则可以直接从url轻松使用datatype: "json"加载所有数据。 You should have very good total performance. 您应该具有非常好的总体表现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM