2012-05-09 48 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');刷新。如果你需要更详细的解释,我建议开始一个新的问题。 –