简体   繁体   English

当loadonce:jqGrid中为true时,分页不起作用

[英]Paging not working when loadonce:true in jqGrid

In jqGrid have property loadonce:true then i am getting only first page records. 在jqGrid中具有属性loadonce:true,那么我仅获得首页记录。 how can i get second and third page records. 我如何获得第二页和第三页记录。

code: 码:

$(function () {

        $("#pendingAppgrid").jqGrid({
            colNames: ['Name', 'Email'],
            colModel: [
                        { name: 'Name', index: 'Name', sortable: true, align: 'left', width: '200',
                            editable: false, edittype: 'text',search:true

                        },

                         { name: 'Email', index: 'Email', sortable: false, align: 'left', width: '200',
                             editable: false, edittype: 'text',search:true
                         },  
                  ],
            pager: jQuery('#pager'),
            sortname: 'Name',
            rowNum: 15,
            rowList: [10, 20, 25],
            sortorder: "desc",
            height: 345,
            ignoreCase: true,
            viewrecords: true,
            rownumbers: true,
            caption: 'Pending Approvals',
            width: 660,
            url: "@Url.Content("~/Home/PendingApprovals")",
            datatype: 'json',
            mtype: 'GET',
            loadonce: true
        })
        jQuery("#pendingAppgrid").jqGrid('filterToolbar', { searchOnEnter: true, enableClear: false });

    });

Server code 服务器代码

public ActionResult PendingApprovals(int page, int rows, string sidx, string sord)
        {

            //return View(GetPendingApprovals());
            int currentPage = Convert.ToInt32(page) - 1;
            int totalRecords = 0;
            List<ViewModels.Channel.UserChannels> lTemChannel = new List<ViewModels.Channel.UserChannels>();
            List<ViewModels.Channel.UserChannels> lstChannel = new List<ViewModels.Channel.UserChannels>();
            lTemChannel = GetPendingApprovals();
            foreach (ViewModels.Channel.UserChannels cha in lTemChannel)
            {
                ViewModels.Channel.UserChannels channelWithLogo = new ViewModels.Channel.UserChannels();
                channelWithLogo.ID = cha.ID;
                channelWithLogo.Name = cha.Name;
                channelWithLogo.Email = cha.Email;

                lstChannel.Add(channelWithLogo);
            }
            totalRecords = lstChannel.Count;
            var totalPages = (int)Math.Ceiling(totalRecords / (float)rows);
                lstChannel = lstChannel.ToList<ViewModels.Channel.UserChannels>();

            IPagedList<ViewModels.Channel.UserChannels> ilstChannel;
            switch (sord)
            {
                case "desc":
                    ilstChannel = lstChannel.OrderByDescending(m => m.Name).ToPagedList(page, rows);
                    break;
                case "asc":
                    ilstChannel = lstChannel.OrderBy(m => m.Name).ToPagedList(page, rows);
                    break;
                default:
                    ilstChannel = lstChannel.OrderBy(m => m.Name).ToPagedList(page, rows);
                    break;
            }

            var data = ilstChannel;

            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,

                rows = (from m in data
                        select new
                        {

                            id = m.ChannelID,
                            cell = new object[]
                           {
                               m.Name,
                               m.Email
                           }
                        }).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

Here i am getting only first page records. 在这里,我只获得首页记录。 i have more pages. 我有更多页面。 search functionality working fine. 搜索功能正常运行。 problem is i am only first page records. 问题是我只是首页记录。 not getting other pages records. 没有获取其他页面记录。 how can i get another page records. 我如何获得另一个页面记录。 Please help this. 请帮忙。

If you use loadonce:true jqGrid change the datatype parameters to ' local ' after the first load of data from the grid. 如果您使用loadonce:true jqGrid在第一次从网格加载数据后将数据类型参数更改为“ local ”。 All next grid reloading (sorting, paging, filtering) works local. 接下来的所有网格重新加载(排序,分页,过滤)都在本地进行。 If you want refresh the grid data from the server one more time you should set datatype to its original value (' json ' or 'xml'). 如果要再次从服务器刷新网格数据,则应将数据类型设置为其原始值(“ json ”或“ xml”)。 For example: 例如:

$("#pendingAppgrid").setGridParam({datatype:'json', page:1}).trigger('reloadGrid');

Please visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options for more details of the jqGrid options. 请访问http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options了解jqGrid选项的更多详细信息。

The problem is complex, read all even the comentaries, you need to recover the actual page of the Jgrid with: 问题很复杂,即使阅读所有评论,您也需要使用以下方法恢复Jgrid的实际页面:

var llPage = $("#listaCfdi").jqGrid('getGridParam', 'page');
//1 but this page is not the page of the "pager" and is before to call the method that fill the jgrid again
//, so the page will change after to fill the grid, you must call "onPaging" to fill again the Jgrid, like this:

//2
, onPaging: function (pgButton) {
        FillAgainJgrid();
    }

and in FillAgainJgrid(); 并在FillAgainJgrid();中 (in this example) you need to call again other method that will have the real page of the "pager" that the user want, in example, the jgrid is in the page:5, and you want the las page, something like page:15, so the second call will have the real page, that the importan thing of the second call. (在此示例中),您需要再次调用其他方法,该方法将具有用户所需的“ pager”的真实页面,例如,jgrid位于page:5中,并且您想要las页面,例如page :15,因此第二个调用将具有实际页面,即第二个调用的重要性。 Only it needs two calls to be sure that the page is the correct. 只有它需要两次调用才能确保页面正确。 And in this example, FillAgainJgridEnsure(); 在此示例中,FillAgainJgridEnsure(); is exact to FillAgainJgrid(); 与FillAgainJgrid()完全相同; except that doesnt have onPaging again 除了没有再次onPaging

//3. In the server side, the page you are passed to the method only will work in the first call, because have all the data
//, and all the pages, but in example in the second call with the page:3, the jgrid will lost all the real information about the size of page, and only will return page:3 like if was the only one, you need a trick like this
//, (they are two list, one with all the data, and then you will get only the page you need, and the second list will have all the other rows in empty
//, so in this way you will get always all the correct rows number of the query, you need Concat in this example because we need all the rows, and if you are filled with dataset, in the sql server you need to use union all) :

ENTIDADES.Cfdi[] resultadoDos = null; //This is the class to provide the correct number of rows

        //this is the query that always will have all the data
        resultado = new CLASES.DAL.Facturacion().ObtenerCfdiDisponibles(criteriosBusqueda);
        if (resultado.Length > 100)
        {
            resultadoDos = new ENTIDADES.Cfdi[resultado.Length - 100];
            for (int i = 0; i < resultado.Length - 100; i++)
            {
                ENTIDADES.Cfdi referenciaVacia = new ENTIDADES.Cfdi();
                resultadoDos[i] = referenciaVacia;
            }
        }
//Paging here, in the server size
        resultado = resultado.OrderBy(p => p.Cliente).Skip(((Convert.ToInt32(_Pagina)) - 1) * 100).Take(100).ToArray();
        if (resultadoDos != null) //if there are more that 100 rows, in this example
        {//concat the page and the fill data empty 
            resultado = resultado.Concat(resultadoDos).OrderByDescending(x => x.Cliente).ToArray();
        }

        regresar = Newtonsoft.Json.JsonConvert.SerializeObject(resultado);

//Note, dont forget to OrderByDescending (in this example), because it will show first the data that have the rows filled, 100 is the size of rows of the page. ///注意,请不要忘记使用OrderByDescending(在此示例中),因为它将首先显示已填充行的数据,即页面的行大小为100。

Omar Romero Mexico City 奥马尔·罗梅罗墨西哥城

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

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