2011-05-13 200 views
3

我嘗試在mvc應用程序的同一頁面上使用兩個不同的jqgrid,表格使用不同的URL來加載數據和不同的名稱。有可能在同一頁面上使用多個jqgrid!?!?在同一頁面加載多個jqgrid

在此先感謝

修訂:爲快速反應

問題繼續後,我已經改變了IDS,你告訴我首先感謝!
這裏是我的代碼:

Javasrcipt:

一格:

jQuery("#listMedicosTODO").jqGrid({ 
    url: '/Medico/LoadToDoMedicos/', 
    datatype: 'json', 
    mtype: 'GET', 
    colNames: ['Cod.', 'Titulo', 'Estado', 'Ultima Actualização'], 
    colModel: [ 
     { name: 'CodRelatorio', index: 'CodRelatorio', width: 50, align: 'center', hidden: true, sortable: false }, 
     { name: 'TituloRelatorio', index: 'TituloRelatorio', width: 100, align: 'center', sortable: true }, 
     { name: 'EstadoRelatorio', index: 'EstadoRelatorio', width: 100, align: 'left', sortable: false }, 
     { name: 'DataUltimaActualizao', index: 'DataUltimaActualizao', width: 100, align: 'left', hidden: false, sortable: false } 
    ], 
    pager: jQuery('#pager1'), 
    rowNum: 50, 
    rowList: [50], 
    sortname: 'Id', 
    sortorder: "asc", 
    viewrecords: true, 
    imgpath: '/scripts/themes/steel/images', 
    caption: 'Tarefas Pendentes Médicos', 
    onSelectRow: function (id) { 
     var data = $("#listMedicosTODO").getRowData(id); 
     alert("select row " + data.CodRelatorio); 
    }, 
    loadComplete: function (data) { 
     alert("Load Complete"); 
     //$('#list').setGridParam({ url: '/PesquisarRelatorios/GetGridData/' }); 
    }, 
    gridComplete: function() { alert("Grid Complete"); }, 
    beforeRequest: function() { }, 
    viewrecords: true, 
    autowidth: true, 
    autoheight: true 
}).navGrid(pager, { edit: false, add: true, del: true, refresh: true, search: false }); 

二格:

jQuery("#listaAssistentesTODO").jqGrid({ 
    url: '/Medico/LoadToDoAssistentes/', 
    datatype: 'json', 
    mtype: 'GET', 
    colNames: ['Cod.', 'Titulo', 'Assistente', 'Estado', 'Ultima Actualização'], 
    colModel: [ 
     { name: 'CodRelatorio', index: 'CodRelatorio', width: 50, align: 'center', hidden: true, sortable: false }, 
     { name: 'TituloRelatorio', index: 'TituloRelatorio', width: 100, align: 'center', sortable: true }, 
     { name: 'Assistente', index: 'Assistente', width: 100, align: 'center', sortable: false }, 
     { name: 'EstadoRelatorio', index: 'EstadoRelatorio', width: 100, align: 'left', sortable: false }, 
     { name: 'DataUltimaActualizao', index: 'DataUltimaActualizao', width: 100, align: 'left', hidden: false, sortable: false } 
    ], 
    pager: jQuery('#page2'), 
    rowNum: 50, 
    rowList: [50], 
    sortname: 'CodRelatorio', 
    sortorder: "asc", 
    viewrecords: true, 
    imgpath: '/scripts/themes/steel/images', 
    caption: 'Tarefas Pendentes Assistentes', 
    onSelectRow: function (id) { 
     var data = $("#listaAssistentesTODO").getRowData(id); 
     alert("select row " + data.CodRelatorio); 
    }, 
    loadComplete: function (data) { 
     alert("Load Complete"); 
     //$('#list').setGridParam({ url: '/PesquisarRelatorios/GetGridData/' }); 
    }, 
    gridComplete: function() { alert("Grid Complet"); }, 
    beforeRequest: function() { }, 
    viewrecords: true, 
    autowidth: true, 
    autoheight: true 
}).navGrid(pager, { edit: false, add: true, del: true, refresh: true, search: false }); 

服務器端點:

if(list != null) 
{ 
    int pageIndex = Convert.ToInt32(page) - 1; 
    int pageSize = rows; 
    int totalRecords = list.Count ; 
    var totalPages = (int)Math.Ceiling(totalRecords/(float)pageSize); 

    var jsonData = new 
         { 
          total = totalPages, 
          page, 
          records = totalRecords, 
          rows = (from item in list 
            select new 
               { 
                i ="a" + item.CodRelatorio, 
                cell = new[] 
                  { 
                   item.CodRelatorio , 
                   item.TituloRelatorio, 
                   item.Assistente , 
                   "Em Elaboração", 
                   item.DataUltimaActualizao 
                  } 

              }).ToArray() 
         }; 


    return Json(jsonData,JsonRequestBehavior.AllowGet); 
} 

2º終點

if (list != null) 
{ 
    int pageIndex = Convert.ToInt32(page) - 1; 
    int pageSize = rows; 
    int totalRecords = list.Count; 
    var totalPages = (int)Math.Ceiling(totalRecords/(float)pageSize); 

    var jsonData = new 
    { 
     total = totalPages, 
     page, 
     records = totalRecords, 
     rows = (from item in list 
       select new 
       { 
        i = "b"+ item.CodRelatorio, 
        cell = new[] 
           { 
            item.CodRelatorio , 
            item.TituloRelatorio, 
            "Em Elaboração", 
            item.DataUltimaActualizao 
           } 

       }).ToArray() 
    }; 

    return Json(jsonData, JsonRequestBehavior.AllowGet); 

此代碼包含您recomendations

感謝

回答

6

有可能使用更多的作爲一個jqGrid的一個頁面上。您應該知道的最重要的事情是您從服務器發佈的id在兩個網格中必須是不同的。例如,如果您需要第一個網格id = 1234,第二個網格需要相同,則可以使用"a1234"作爲第一個網格,"b1234"作爲第二個網格。

如果您將繼續遇到兩個網格問題,您應該發佈兩個網格的定義(JavaScript代碼)以及您遇到問題的測試JSON或XML數據。

已更新:您的主要錯誤是,您沒有設置和id在服務器端。而不是你設置i財產是未知的,哪些將被忽略。如果沒有定義id jqGrd嘗試使用整數:「1」,「2」,...值作爲ID。這種「id-fix」在頁面上有一個網格的情況下工作,但不在兩個網格中。

所以你必須改變i ="a" + item.CodRelatorioi = "b"+ item.CodRelatorioid ="a" + item.CodRelatorioid = "b"+ item.CodRelatorio

爲了說明Phil Haack發佈的the demo example中的信任是相同的書寫錯誤,但它在2011年3月6日修復(請參閱頁面上的評論)。

另一個小的變化,你應該做的是

  1. 刪除deprecated jqGrid的參數imgpath。這是多年以來沒有使用過的。
  2. 而不是未知參數autowidth: trueautoheight: true您可能想使用height:'auto'
  3. 最好使用pager:'#page1'pager:'#page2'而不是pager: '#page1'pager: '#page2'
  4. 第一個網格沒有名稱爲'Id'的列。因此,您應該將sortname: 'Id' jqGrid選項替換爲例如sortname: 'CodRelatorio'

我建議您閱讀the answer的「UPDATED」部分。您可以從答案中下載the example,並將其用作應用程序的模板。

+0

問題仍然存在,因爲您告訴我我更改了ID! – mastervv

+0

@mastervv:您應該閱讀我的回答:「如果您仍然有兩個網格出現問題,您應該**發佈兩個網格的定義(JavaScript代碼)以及您遇到問題的測試JSON或XML數據** 」。 – Oleg

+0

我發佈的JavaScript代碼和服務器端代碼,請看看它! – mastervv

0

是的,我們可以在一個頁面中使用多個Jqgrid,但必須給出不同的Jqgrid ID。

請參閱下面的代碼。工作示例,

jQuery(document).ready(function() { 

     $("#datagrid").jqGrid({ //////////// 1st Grid 
      url: "service url", 
      //url: "service url", 
      type: "GET", 
      ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
      datatype: "json", 
      //colNames:['Id','MID','Status','DocNo','VendorID','InvoiceNo','VendorName','Amount','Type','DocDate','DueDate','ClDoc','Texxt','UserName','Currency','ConCode','Region','Stat','Comb','Comments'], 

      colNames:['Id','MID','Status','VendorID','VendorName','InvoiceNo','DocDate','Amount','DocNo','Type','DueDate','ClDoc','Text','UserName','Currency','ConCode','Region','Stat','Process','Comb','Comments'], 
      colModel:[ 
    {name:'id',index:'id', width:50,sortable:true}, 
    {name:'mid',index:'mid', width:50, sortable:true}, 
    {name:'status',index:'status', width:70, sortable:true}, 
    {name:'vendorid',index:'vendorid', width:90, sortable:false,align:"left"}, 
    {name:'vendorname',index:'vendorname', width:170, sortable:false,align:"left"}, 
    {name:'invoiceno',index:'invoiceno', width:130, sortable:false,align:"left"}, 
    {name:'docdate',index:'docdate', width:100, sortable:false}, 
    {name:'amount',index:'amount', width:80, sortable:false,align:"Right"}, 
    {name:'docno',index:'docno', width:100, sortable:false}, 
    {name:'typee',index:'typee', width:50, sortable:false}, 
    {name:'duedate',index:'duedate', width:100, sortable:false}, 
    {name:'cldoc',index:'cldoc', width:80, sortable:false}, 
    {name:'text',index:'texxt', width:70, sortable:false}, 
    {name:'username',index:'username', width:100, sortable:false}, 
    {name:'currency',index:'currency', width:80, sortable:false}, 
    {name:'concode',index:'concode', width:80, sortable:false}, 
    {name:'region',index:'region', width:70, sortable:false}, 
    {name:'stat',index:'stat', width:60, sortable:false}, 
    {name:'process',index:'process', width:60, sortable:false}, 
    {name:'combination',index:'combination', width:60, sortable:true}, 
    {name:'comments',index:'comments', width:150, height:20, edittype:'textarea', sortable:false, editable: true, 
      editoptions: {disabled: false, size:50, resizable:true}} 
    ], 
viewrecords: true 
}); 


     $("#datagrid1").jqGrid({    ///////////////2nd Grid 

      url: "service url", 
      type: "GET", 
      ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
      datatype: "json", 
      //colNames:['Id','MID','Status','DocNo','VendorID','InvoiceNo','VendorName','Amount','Type','DocDate','DueDate','ClDoc','Texxt','UserName','Currency','ConCode','Region','Stat','Comb','Comments'], 
      colNames:['Id','MID','Status','VendorID','VendorName','InvoiceNo','DocDate','Amount','DocNo','Type','DueDate','ClDoc','Text','UserName','Currency','ConCode','Region','Stat','Process','Comb','Comments'], 
      colModel:[ 
    {name:'id',index:'id', width:50,sortable:true}, 
    {name:'mid',index:'mid', width:50, sortable:true}, 
    {name:'status',index:'status', width:70, sortable:true}, 
    {name:'vendorid',index:'vendorid', width:90, sortable:false,align:"left"}, 
    {name:'vendorname',index:'vendorname', width:170, sortable:false,align:"left"}, 
    {name:'invoiceno',index:'invoiceno', width:130, sortable:false,align:"left"}, 
    {name:'docdate',index:'docdate', width:100, sortable:false}, 
    {name:'amount',index:'amount', width:80, sortable:false,align:"Right" }, 
    {name:'docno',index:'docno', width:100, sortable:false}, 
    {name:'typee',index:'typee', width:50, sortable:false}, 
    {name:'duedate',index:'duedate', width:100, sortable:false}, 
    {name:'cldoc',index:'cldoc', width:80, sortable:false}, 
    {name:'text',index:'texxt', width:70, sortable:false}, 
    {name:'username',index:'username', width:100, sortable:false}, 
    {name:'currency',index:'currency', width:80, sortable:false}, 
    {name:'concode',index:'concode', width:80, sortable:false}, 
    {name:'region',index:'region', width:70, sortable:false}, 
    {name:'stat',index:'stat', width:60, sortable:false}, 
    {name:'process',index:'process', width:60, sortable:false}, 
    {name:'combination',index:'combination', width:60, sortable:true}, 
    {name:'comments',index:'comments', width:150, edittype:'textarea', sortable:false, editable: true, 
      editoptions: {disabled: false, size:50, resizable:true}} 
     ] 
viewrecords: true 
}); 

});