2012-05-09 51 views
0

我想從jqgrid只導出數據。Jqgrid獲取postData

所以基本上我已經有了一個綁定的數據網格,我想以json字符串的形式獲取數據,因此我可以稍後調用BindGridModel(data)並綁定數據,而無需從客戶端返回到服務器。

我怎樣才能得到數據作爲json字符串,所以後來我可以把它作爲數據給網格?

這是我的網格配置:

function BindGridModel(data) { 
    $('#jqgInventory').jqGrid({ 
     autowidth: true, 
     caption: 'Inventory', 
     datatype: 'json', 
     forceFit: true, 
     gridview: true, 
     height: 500, 
     hidegrid: false, 
     ignoreCase: true, 
     loadui: 'disable', 
     pager: '#pager', 
     mtype: 'post', 
     rowNum: 25, 
     shrinkToFit: true, 
     url: '/MCI/Inventory/Inventory/GetIndexGridData', 
     viewrecords: true, 
     postData: { 
      modelView: JSON.stringify(model), 
      __RequestVerificationToken: $('[name="__RequestVerificationToken"]').val() 
     }, 
     beforeRequest: function() { 
      $('#gridScript').block(); 
     }, 
     beforeSelectRow: function(rowid, e) { 
      return false; 
     }, 
     gridComplete: function() { 
      $('#lblVehicleCount').html($('#jqgInventory').getGridParam('records')); 
      $('#gridScript').unblock(); 
      Inventory.modifyGridCellClick(); 
     }, 
     colModel: [ 
      { 
      align: 'center', 
      name: 'Select', 
      label: 'SEL', 
      title: true, 
      width: 20, 
      index: 'Select'}, 
     { 
      align: 'left', 
      name: 'Photo', 
      hidden: false, 
      label: 'PHOTO', 
      stype: 'text', 
      sortable: false, 
      sorttype: 'text', 
      title: true, 
      width: 100, 
      index: 'Photo'}, 
     { 
      align: 'left', 
      name: 'Information', 
      hidden: false, 
      label: 'INFO', 
      stype: 'text', 
      sortable: false, 
      sorttype: 'text', 
      title: true, 
      width: 100, 
      index: 'Information'}, 
     { 
      align: 'right', 
      name: 'Price', 
      hidden: false, 
      label: 'PRICE', 
      stype: 'text', 
      sortable: true, 
      sorttype: function(cellValue) { 
       return CustomGridSortByIntegerAsString(cellValue); 
      }, 
      title: true, 
      width: 50, 
      index: 'Price'}, 
     { 
      align: 'right', 
      name: 'Mileage', 
      hidden: false, 
      label: 'MILEAGE', 
      stype: 'text', 
      sortable: true, 
      sorttype: function(cellValue) { 
       return CustomGridSortByIntegerAsString(cellValue); 
      }, 
      title: true, 
      width: 25, 
      index: 'Mileage'}, 
     { 
      align: 'right', 
      name: 'Age', 
      hidden: false, 
      label: 'AGE', 
      stype: 'text', 
      sortable: true, 
      sorttype: function(cellValue) { 
       return CustomGridSortByIntegerAsString(cellValue); 
      }, 
      title: true, 
      width: 50, 
      index: 'Age'}, 
     { 
      name: 'VehicleKey', 
      hidden: true, 
      label: 'VEHICLEKEY', 
      width: 50, 
      index: 'VehicleKey'} 
     ], 
     data: data 
    }); 
} 
+0

: 'local''而不是'數據類型:' json'' 。此外,您是否想要導出當前顯示的一頁數據或者是否要導出所有數據尚不清楚。此外,你寫了「將數據作爲json字符串」,這有點奇怪。通常情況下,只有在需要將數據發送到服務器時,纔會將數據作爲* object *(表示網格行數據的項目數組)獲取並在某些情況下將其轉換爲JSON。 – Oleg

+0

不要擔心數據參數,因爲我正在動態地構建網格腳本,因此一些頁面會立即加載數據,其中一些頁面會執行服務器發佈以獲取數據。我想導出帶有列名的數據,這樣我就可以添加更多的數據並將其綁定回來,我正在做類似獲取前25條記錄,然後當你點擊下一頁時,我將這25條記錄存儲在某處並在我的路上在抓取下一個25後回來,我想將它們添加到前25個,並顯示50條記錄,我試圖看看是否有內置尋呼機的方式。 –

回答

0

在您有權訪問被綁定到網格的數據loadComplete事件,並能JSON字符串化它。我通常將其設置爲一個Jquery數據屬性,以便在事件之外訪問它。

loadComplete: function(data) { 
    $('#gridid').data('jqdata', data);//JSON stringify the data 1st if you need to 
}, 

您可以再後來通過訪問這些數據:如果您想使用`data`參數,你應該使用`數據類型

$('#gridid').data('jqdata'); 
+0

偉大的,我需要什麼,但現在如何修改這些數據並重新加載網格? –

+0

修改數據就像修改任何其他Jquery對象一樣。修改後,您只需將該對象重新綁定到網格,將其設置爲本地模式,然後調用$('#gridid')。trigger('reloadGrid');刷新。如果你需要更詳細的解釋,我建議開始一個新的問題。 –