Send expanded TreeGrid Nodes in cookie

前端 未结 1 1165
离开以前
离开以前 2020-12-01 23:07

In continue of this topic I want to save expanded nodes in cookie. Is it the best way?

I\'m not sure in way of JSON data checking.

Why expan

1条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 23:31

    I recommend you to use localStorage instead of cookies. I describe the reason in the answer. I made the demo base on the demos from the answer and another one. For the demo I used the test data from one more my recent answer.

    Try to expand in the demo some tree nodes and reload the grid with F5. You will see that all expanded rows stay expanded after the reloading.

    The code of the demo you will find below:

    var $grid = $('#treegridCompanies'),
        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)) || [];
    
    $grid.jqGrid({
        url: 'SPATEN-TreeGrid2.json',
        datatype: 'json',
        ajaxGridOptions: { contentType: "application/json" },
        jsonReader: {
            id: "CompanyId",
            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[5] = index >= 0; // set expanded column
                row[6] = true;       // set loaded column
            }
        },
        colNames:  ['CompanyId', 'Company'],
        colModel: [
            { name: 'CompanyId', index: 'CompanyId', width: 1, hidden: true, key: true },
            { name: 'Company', index: 'Company', width: 500 }
        ],
        height: 'auto',
        pager: '#pager',
        rowNum: 10000,
        sortable: false,
        treeGrid: true,
        treeGridModel: 'adjacency',
        ExpandColumn: 'Company'
    });
    $grid.jqGrid('navGrid', '#pager', {edit: false, add: false, del: false, search: false});
    $grid.jqGrid('navButtonAdd', '#pager', {
        caption: "",
        buttonicon: "ui-icon-closethick",
        title: "Clear saved grid's settings",
        onClickButton: function () {
            removeObjectFromLocalStorage(myColumnStateName($(this)));
            window.location.reload();
        }
    });
    $.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);
        }
    });
    

    0 讨论(0)
提交回复
热议问题