【问题标题】:Paging not working when loadonce:true in jqGridjqGrid中loadonce:true时分页不起作用
【发布时间】:2013-06-21 06:04:32
【问题描述】:

在 jqGrid 中有属性 loadonce:true 然后我只得到第一页记录。如何获取第二页和第三页记录。

代码:

$(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 });

    });

服务器代码

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);
        }

这里我只得到第一页记录。我有更多的页面。搜索功能工作正常。问题是我只是第一页记录。没有得到其他页面记录。我怎样才能得到另一个页面记录。请帮忙。

【问题讨论】:

    标签: asp.net-mvc jqgrid-asp.net


    【解决方案1】:

    如果您使用loadonce:true jqGrid,请在第一次从网格加载数据后将数据类型参数更改为“local”。所有下一个网格重新加载(排序、分页、过滤)都在本地工作。如果您想再次从服务器刷新网格数据,您应该将数据类型设置为其原始值('json' 或 'xml')。例如:

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

    有关 jqGrid 选项的更多详细信息,请访问http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options

    【讨论】:

    • 我做了同样的事情。更改数据类型='本地'。并将您在函数中给出的代码放在“filterToolbar”之后。但我没有。
    • @Ram,您不需要将数据类型更改为本地,当您将选项 loadonce 设置为 true 时,jqGrid 本身在第一次加载后就完成了。您必须在寻呼机中添加此代码,请发布寻呼代码。
    • 好的。请您正确发布如何为此进行分页。在这里,我只获得第一页记录。如果我在页脚部分更改第 2 页,它不会显示第二页记录。
    • @Ram,解决方案取决于您的分页代码,请将其发布在有问题的地方,以便解决确切的问题。通用解决方案可能对您没有帮助,可能会浪费我们的时间。
    • @Ram,正是这种情况,第 2 页不显示,因为“loadonce”选项在首次加载和服务器端命中后将数据类型设置为本地,但它可以对本地数据执行过滤,这就是我首先要传达的答案。尝试在分页按钮的单击上添加该代码(将您带到下一页的按钮,它必须在#pager div中)并用所需的页面替换页码。
    【解决方案2】:

    问题很复杂,连评论都看完了,需要恢复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();
        }
    

    并在 FillAgainJgrid(); (在此示例中)您需要再次调用其他方法,该方法将具有用户想要的“寻呼机”的真实页面,例如,jgrid 在 page:5 中,并且您想要 las 页面,类似于 page :15,所以第二次调用会有真正的页面,这是第二次调用的重要内容。只有它需要两次调用来确保页面是正确的。在这个例子中, FillAgainJgridEnsure();与 FillAgainJgrid() 完全相同;除了没有 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);
    
    //注意,不要忘记OrderByDescending(在这个例子中),因为它会首先显示填充​​了行的数据,100是页面的行数。

    奥马尔·罗梅罗墨西哥城

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多