0

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).

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");
        }
    });
}
Community
  • 1
  • 1

1 Answers1

0

I see 3 errors in your code:

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

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

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

  3. You use unneeded " in subGridData part of your data. Even colors in the code (I mean prettify highlighting) which you posted shows you the same.

Instead of

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"}
            ]"}
];

one should use

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"}
            ]}
];

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

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Oleg please view above updated code. working fine for first list fetch from db then populate parent grid and subgrid good. after polling action not working subgrid. code used as follows var $mygrid= $("#list11"); $mygrid.jqGrid("addRowData", "ID", data); $mygrid.trigger("reloadGrid", [{current: true}]); – – Krishnakumar Subbaiyan Aug 27 '14 at 10:25
  • @KrishnakumarSubbaiyan: It's unclear for me which format have the data returned from `server.php?getPollList`. Have it the format like `firstListJson`? Do you fixed format of `subGridData` part like I described in my answer? Moreover why you still use `"ID"` parameter of `addRowData`? It's wrong and should be replaced probably to `"id"` (see my answer for details). – Oleg Aug 27 '14 at 10:29
  • Yes Oleg, i have updated the "id" and sub grid data json remove ' quotes. but not working now.Second time i have updated the code above please check – Krishnakumar Subbaiyan Aug 27 '14 at 10:54
  • I have reviewed the all the links. i have implement correct but not working. Please help me Oleg. – Krishnakumar Subbaiyan Aug 27 '14 at 12:11
  • @KrishnakumarSubbaiyan: Sorry, but asked you multiple times abou the format of the JSON response on `pollingListUrl`, but you never answered me. Could you provide my an example of data? How many rows you load in the way? Are you sure that no data with the same ids already exist in the grid? Do you add 1-2 rows or some large number of rows which count is close to the count of rows in the grid? Moreover if you write "not working" what exactly you have? – Oleg Aug 27 '14 at 12:25
  • Sorry for the my unclear answer to you, model data is attached name firstListJson(this is pollingListUrl output),the number of rows will get from db using polling is min 5 to max 100 rows, the number of sub rows count might be greater than in grid. There might be duplicate ids will come. – Krishnakumar Subbaiyan Aug 28 '14 at 06:05
  • @KrishnakumarSubbaiyan: You can't use `addRowData` if id of added rows could be the same as already exist in jqGrid. In the case you should **modify** existing rows with `setRowData`. If it could be many rows to add/update then it would be *much more effective* to modify `data` (and probably `_index`) parameters of jqGrid and then trigger `reloadGrid`. You can use `_index` to test whether row with some id is loaded already in the grid. Moreover you **have to** use `idPrefix` for subgrids because of problem with id duplicate. – Oleg Aug 28 '14 at 06:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60168/discussion-between-krishnakumar-subbaiyan-and-oleg). – Krishnakumar Subbaiyan Aug 28 '14 at 12:41