2013-06-21 51 views
2

在jqGrid中有屬性loadonce:true然後我只獲取第一頁記錄。我怎樣才能得到第二和第三頁的記錄。在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 }); 

    }); 

Server代碼

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

我在這裏只得到了第一頁的記錄。我有更多的頁面。搜索功能正常工作。問題是我只是第一頁的記錄。沒有獲得其他網頁記錄。我怎麼能得到另一個頁面記錄。請幫助這個。

回答

4

如果使用loadonce:true jqGrid在第一次從網格加載數據後,將數據類型參數更改爲'本地'。所有下一個網格重新加載(排序,分頁,過濾)在本地工作。如果要再次從服務器刷新網格數據,應再次將數據類型設置爲其原始值('json'或'xml')。例如:

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

有關jqGrid選項的更多詳細信息,請訪問http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options

+0

我做了同樣的事情。改變了datatype ='local'。並在「filterToolbar」之後的函數內部放置了你給出的代碼。但我沒有得到。 – Ram

+1

@Ram,您不需要將數據類型更改爲本地數據類型,jqGrid本身在第一次加載後執行此操作,此時您將選項loadonce設置爲true。您必須在傳呼機中添加此代碼,請發佈分頁代碼。 –

+0

好的。請你能正確地發佈如何做這個分頁。在這裏,我只獲取第一頁記錄。如果我在頁腳部分更改頁面編號:2,則不顯示第二頁記錄。 – Ram

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(); (在這個例子中),你需要再次調用其他方法,該方法具有用戶想要的「尋呼機」的實際頁面,例如,jgrid在頁面中:5,並且您希望las頁面,如頁面:15,所以第二個電話會有真正的頁面,這是第二個電話的重要內容。只有兩次調用才能確保頁面是正確的。在這個例子中,FillAgainJgridEnsure();與FillAgainJgrid()完全相同。除了犯規再次

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

//注意onPaging,不要忘記OrderByDescending(在本例中),因爲它會首先顯示具有行充滿了數據,100是頁面的行的大小。

奧馬爾羅梅羅墨西哥城