loadonce时分页不工作:在jqGrid中为true

时间:2013-06-21 06:04:33

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

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

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

2 个答案:

答案 0 :(得分:4)

如果您使用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

答案 1 :(得分:0)

问题很复杂,甚至连所有的comentaries都读过,你需要恢复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()中; (在此示例中)您需要再次调用其他方法,该方法将具有&#34; pager&#34;的真实页面。用户想要的,例如,jgrid在页面中: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表示​​页面行的大小。

Omar Romero墨西哥城