0

我已经阅读了这个线程并且我已经下载了 Oleg 的演示,但是在我的代码中它不起作用。

我有一个带有 Json 的外部 PHP 的 jqGrid。我使用 setinteval(1 秒)实时刷新数据,但是当我炸毁一棵树时,它会立即关闭。

我的 javascript 代码到 jQuery(document).ready(function(): `

    // odd even row
    var resetAltRows = function () {
            $(this).children("tbody:first").children('tr.jqgrow').removeClass('odd');
            $(this).children("tbody:first").children('tr.jqgrow:visible:odd').addClass('odd');
    };

    'use strict';
    var $grid = $('#list'),
        saveObjectInLocalStorage = function (storageItemName, object) {
            if (typeof window.localStorage !== 'undefined') {
                window.localStorage.setItem(storageItemName, JSON.stringify(object));
            }
        },
        removeObjectFromLocalStorage = function (storageItemName) {
            if (typeof window.localStorage !== 'undefined') {
                window.localStorage.removeItem(storageItemName);
            }
        },
        getObjectFromLocalStorage = function (storageItemName) {
            if (typeof window.localStorage !== 'undefined') {
                return JSON.parse(window.localStorage.getItem(storageItemName));
            }
        },
        myColumnStateName = function (grid) {
            return window.location.pathname + '#' + grid[0].id;
        },
        idsOfExpandedRows = [],
        updateIdsOfExpandedRows = function (id, isExpanded) {
            var index = $.inArray(id, idsOfExpandedRows);
            if (!isExpanded && index >= 0) {
                idsOfExpandedRows.splice(index, 1); // remove id from the list
            } else if (index < 0) {
                idsOfExpandedRows.push(id);
            }
            saveObjectInLocalStorage(myColumnStateName($grid), idsOfExpandedRows);
        },
        orgExpandRow = $.fn.jqGrid.expandRow,
        orgCollapseRow = $.fn.jqGrid.collapseRow;

     idsOfExpandedRows = getObjectFromLocalStorage(myColumnStateName($grid)) || [];

    jQuery("#list").jqGrid({
        url:'get_tree.php',
        datatype: "json",
        ajaxGridOptions: { contentType: "application/json" },
        jsonReader: {
                id: "id",
                cell: "",
                root: function (obj) { return obj.rows; },
                page: function () { return 1; },
                total: function () { return 1; },
                records: function (obj) { return obj.rows.length; },
                repeatitems: true
            },
            beforeProcessing: function (data) {
                var rows = data.rows, i, l = rows.length, row, index;
                for (i = 0; i < l; i++) {
                    row = rows[i];
                    index = $.inArray(row[0], idsOfExpandedRows);
                    row[30] = index >= 0; // set expanded column
                    row[31] = true;       // set loaded column
                }
            },

        colNames:[...............],
        colModel :[
            {name:'id', index:'id', width:15, sortable: false, title: false,hidden: true},
            {name:'0', index:'0', classes:'piu', width:15, sortable: false, title: false},
            {..............}
            ],
        rowNum:100,
        viewrecords: true,
        autowidth: true,
        height: 'auto',
        loadonce:true,
        key: true,
           loadComplete: function() {
                var grid = this;
                resetAltRows.call(this);
                $(this).find('tr.jqgrow td div.treeclick').click(function(){
                    resetAltRows.call(grid);
                });
                $(this).find('tr.jqgrow td span.cell-wrapper').click(function(){
                    resetAltRows.call(grid);
                });
            },
        gridview: true,
        treeGrid: true,
        treeGridModel: "adjacency",
        ExpandColumn: '0',
        ExpandColClick: true
    });
    $.jgrid.extend({
        expandRow: function (rc) {
            alert('before expandNode: rowid="' + rc._id_ + '", name="' + rc.name + '"');
            updateIdsOfExpandedRows(rc._id_, true);
            return orgExpandRow.call(this, rc);
        },
        collapseRow: function (rc) {
            alert('before collapseNode: rowid="' + rc._id_ + '", name="' + rc.name + '"');
            updateIdsOfExpandedRows(rc._id_, false);
            return orgCollapseRow.call(this, rc);
        }
    });

    //REFRESH JSON
    setInterval(
        function() {
        jQuery("#list").jqGrid('setGridParam',{datatype:'json'}).trigger('reloadGrid');
        }
    , 1000);

`

我也改变了这个

row[30] = index >= 0; row[31] = true;

具有json的“扩展”和“加载”字段的确切位置。

4

1 回答 1

0

您在评论中写道,一行 JSON 数据看起来像

{"id":"13","0":"","1":"01","2":"","3":"12345","4":"asdasdasd","5":"59.67",
"6":"‌​asd","7":"UL","8":"5","9":"04:13","10":"1","11":"40","12":"40","13":"38",
"14":"(5‌​) 5","15":"2","16":"2","17":"2","18":"0","19":"0","20":"6","21":"24",
"22":"99","23‌​":"1.874","24":"0_01|0",
"level":"0","parent":null,"isLeaf":false,"expanded":false‌​,"loaded":true}

这意味着数据的格式 repeatitems: true对应于jsonReader. 仅由于 jqGrid 4.4.5 中引入并在 jqGrid 4.5.0 中修复的 JSON 格式的自动检测功能(参见此处此处),网格将被填充。因此,我建议您使用jsonReaderrepeatitems: false至少删除repeatitems: false不符合您的数据格式的内容。

要设置expandedloaded列,您应该使用

row.expanded = index >= 0; // set expanded column
row.loaded = true;         // set loaded column

代替

row[30] = index >= 0; // set expanded column
row[31] = true;       // set loaded column

此外,您应该使用row.id代替row[0](参见index = $.inArray(row[0], idsOfExpandedRows)代码)。

可能您需要像上面那样进行更多更改。我无法测试代码,因此上述更改可能不完整。无论如何,确实需要上述更改。

此外,我会非常小心使用整数作为列名。我最好将列的名称从"0", "1", ..."24"更改为例如"c0", "c1", ..."c24"这更多地对应于标识符的名称,并额外保护您免受带有 rowids 的 id 重复,这些 rowids 也是整数(请参阅"id":"13""13":"38")。它可以为您将来节省很多时间。

于 2013-07-22T16:40:16.160 回答