【问题标题】:Jqgrid Get postDataJqgrid 获取 postData
【发布时间】:2012-05-09 17:14:14
【问题描述】:

我只想从 jqgrid 导出数据。

所以基本上我已经有一个包含数据的绑定网格,我想将数据作为 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
    });
}

【问题讨论】:

  • 如果你想使用data参数你应该使用datatype: 'local'而不是datatype: 'json'。此外,不清楚您是要导出当前显示的一页数据还是要导出所有数据。此外,您写了关于“以 json 字符串形式获取数据”的文章,这有点奇怪。通常,将数据作为 object(表示网格行中数据的项目数组)并在某些情况下仅在需要将其发送到服务器时将其转换为 JSON。
  • 不要担心数据参数,因为我正在动态构建网格脚本,因此有些页面会立即加载数据,其中一些页面会通过服务器发布来获取数据。我想用列名导出数据,这样我就可以向它添加更多数据并将其绑定回来,我正在做一些事情,比如获取前 25 条记录,然后当你点击下一页时,我将这 25 条记录存储在某个地方和我的路上抓取下一个 25 后返回我想将它们添加到前 25 条并显示 50 条记录,我想看看是否有办法使用内置寻呼机。

标签: jquery jquery-plugins jqgrid jqgrid-asp.net jqgrid-php


【解决方案1】:

loadComplete 事件中,您可以访问绑定到网格的数据,并且可以对其进行 JSON 字符串化。我通常将其设置为 Jquery 数据属性,以便在事件之外访问它。

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

您可以稍后通过以下方式访问该数据:

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

【讨论】:

  • 满足我的需要,但现在如何修改这些数据并重新加载网格?
  • 修改数据就像修改任何其他 Jquery 对象一样。修改后,您只需将该对象重新绑定到网格,将其设置为本地模式,然后调用 $('#gridid').trigger('reloadGrid');刷新。如果您需要更详细的解释,我建议您开始一个新问题。
猜你喜欢
  • 2023-03-16
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 2011-05-25
相关资源
最近更新 更多