如何在轮询ajax时填充子网格,然后在jqgrid中添加现有网格? [英] How to populate subgrid while polling ajax then apppend existing grid in jqgrid?

查看:75
本文介绍了如何在轮询ajax时填充子网格,然后在jqgrid中添加现有网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用代码的列表json列表轮询每6秒使用相同的列表json列表从db附加数据,它在没有子网格系统的情况下也可以正常工作,但是在我得到之后进行整合父轮询数据正确,但我没有得到子网格数据为空(获取数据后端以得到子网格很好).

I have list of list json using the code and got a proper answer also after i want polling every 6 sec append data from db with same list of list json format its also working fine without subgrid system but am integrating both after i got a parent polling data correct but I am not getting the subgrid data its empty(fetch the data backend for subgrid fine).

Oleg评论后第二次更新代码我已附上代码,请找到它

Second time UPDATED CODE after Oleg comments I have attached the code please find it

    var $grid = $("#list11"),
    mainGridPrefix = "s_";

jQuery("#list11").jqGrid({
    url: 'server.php?getList',
    datatype: "json",
    data: firstListJson,
    height: 200,
    colNames: ['Inv No', 'Date', 'Client'],
    colModel: [{
        name: 'id',
        index: 'id',
        width: 55
    }, {
        name: 'invdate',
        index: 'invdate',
        width: 90
    }, {
        name: 'name',
        index: 'name',
        width: 100
    }],
    rowNum: 10,
    gridview: true,
    rowList: [10, 20, 30],
    pager: '#pager11',
    loadonce: false,
    sortname: 'id',
    viewrecords: true,
    idPrefix: mainGridPrefix,
    sortorder: "desc",
    multiselect: false,
    caption: "Subgrid With polling",
    jsonReader: {
        repeatitems: false,
        id: 'id'
    },
    beforeProcessing: function(data) {

        var rows = data,
            l = data.length,
            i, item, subgrids = {};

        for (i = 0; i < l; i++) {
            item = rows[i];
            if (item.subGridData) {
                subgrids[item.id] = item.subGridData;
            }
        }
        //alert(subgrids);
        data.userdata = subgrids;
    },
    subGrid: true,
    subGridRowExpanded: function(subgridDivId, rowId) {


        var subGridID = $("#" + subgridDivId + "_t");
        var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
            pureRowId = $.jgrid.stripPref(mainGridPrefix, rowId),
            subgrids = $(this).jqGrid("getGridParam", "userData");

        $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId));
        $subgrid.jqGrid({
            datatype: "local",
            data: subgrids[pureRowId],
            colNames: ['Emp ID', 'Name', 'Age'],
            colModel: [{
                name: 'id',
                index: 'id',
                width: 55
            }, {
                name: 'name',
                index: 'name',
                width: 90
            }, {
                name: 'age',
                index: 'age',
                width: 100
            }],
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: 'id',
            viewrecords: true,
            sortorder: "desc",
            multiselect: false

        });
    }
});
jQuery("#list11").jqGrid('navGrid', '#pager11', {
    add: false,
    edit: false,
    del: false
});





Below code
for json list of list



var firstListJson = [{
    "id": "01",
    "invdate": "2014-07-24",
    "name": "John",
    "subGridData": [{
        "id": "01",
        "name": "Krishna",
        "age": "28"
    }, {
        "id": "01",
        "name": "Jai",
        "age": "28"
    }, {
        "id": "01",
        "name": "Suresh",
        "age": "28"
    }]
}, {
    "id": "02",
    "invdate": "2014-07-24",
    "name": "Hill",
    "subGridData": [{
        "id": "01",
        "name": "Mani",
        "age": "28"
    }, {
        "id": "01",
        "name": "Raj",
        "age": "28"
    }, {
        "id": "01",
        "name": "Main",
        "age": "28"
    }]
}];

Below code
for polling code

function pollData() {
    var pollingListUrl = 'server.php?getPollList';
    $.ajax({
        type: "POST",
        url: pollingListUrl,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function(data) {
            var $mygrid = $("#list11");
            $mygrid.jqGrid("addRowData", "id", data);
            $mygrid.trigger("reloadGrid", [{
                current: true
            }]);
        },
        error: function(x, e) {
            alert("error occur");
        }
    });
}

推荐答案

我在您的代码中看到3个错误:

I see 3 errors in your code:

  1. 您使用datatype: "xml",但是beforeProcessing的代码针对的是data object 而不是XML-Document的代码.

  1. you use datatype: "xml", but the code of beforeProcessing are oriented on the code where data is object instead of XML-Document.

通过这种方式,我严格建议您在网格和子网格中都添加gridview: true,以提高性能.

By the way I strictly recommend you to add gridview: true in both grid and subgrid to improve the performance.

您使用$mygrid.jqGrid("addRowData", "ID", data);,但是网格不包含列"ID".您应该改用"id".

You use $mygrid.jqGrid("addRowData", "ID", data); but the grid don't contain the column "ID". You should use "id" instead.

代替

var firstListJson=[
    {"id":"01","invdate":"2014-07-24","name":"John",
        "subGridData":"[{
                "id":"01","name":"Krishna","age":"28"},
                {"id":"01","name":"Jai","age":"28"},
                {"id":"01","name":"Suresh","age":"28"}
            ]"},
    {"id":"02","invdate":"2014-07-24","name":"Hill",
        "subGridData":"[{
                "id":"01","name":"Mani","age":"28"},
                {"id":"01","name":"Raj","age":"28"},
                {"id":"01","name":"Main","age":"28"}
            ]"}
];

应该使用

var firstListJson=[
    {"id":"01","invdate":"2014-07-24","name":"John",
        "subGridData": [{
               "id":"01","name":"Krishna","age":"28"},
               {"id":"01","name":"Jai","age":"28"},
               {"id":"01","name":"Suresh","age":"28"}
            ]},
    {"id":"02","invdate":"2014-07-24","name":"Hill",
        "subGridData": [{
                "id":"01","name":"Mani","age":"28"},
               {"id":"01","name":"Raj","age":"28"},
               {"id":"01","name":"Main","age":"28"}
            ]}
];

如果subGridData的值将是对象(项目数组)而不是字符串.

In the case the value of subGridData will be object (array of items) instead of string.

这篇关于如何在轮询ajax时填充子网格,然后在jqgrid中添加现有网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆