Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:57:01+00:00 2026-05-29T09:57:01+00:00

In continue of this topic I want to save expanded nodes in cookie. Is

  • 0

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 expandRow doesn’t work?

var gridId = "#table";
var grid = $(gridId);
grid.jqGrid({

...

loadComplete: function() {
var ids = grid.jqGrid('getDataIDs');

var cookie = $.cookie(gridId + '_expanded');
var expanded = false;

if (typeof(cookie) == 'string')
var expanded = JSON.parse(cookie);

for (var i=0;i<ids.length;i++) {
var id=ids[i];
var row_data = $(this).jqGrid('getRowData', id);

if (expanded && id in expanded && expanded[id] == 'true')
$(gridId + ' tr#' + id + ' div.treeclick').trigger("click");
//Why it doesn't work?
//grid.jqGrid('expandRow', row_data);

}
}

...

});

function setCookie() {
            var ids = grid.jqGrid('getDataIDs');

            var expanded = Object();
            var string = '';

            for (var i=0;i<ids.length;i++) {    
                var id=ids[i];
                var rowData = grid.jqGrid('getRowData', id);                

                if (rowData.parent != '' && grid.jqGrid('isVisibleNode', rowData) === true)
                    expanded[rowData.parent] = true
            }

            for (e in expanded) 
                string += '"' + e + '":' + '"' + expanded[e] + '",';

            $.cookie(gridId + '_expanded', '{'+ string.substring(0, string.length - 1) + '}', { expires: 365 });    
}

var orgExpandNode = $.fn.jqGrid.expandNode, orgCollapseNode = $.fn.jqGrid.collapseNode;

        $.jgrid.extend({
            expandNode : function(rc) {
                orgExpandNode.call(this, rc);
                setCookie();            
            },
            collapseNode : function(rc) {
                orgCollapseNode.call(this, rc);
                setCookie();                        
            },
});

P.S. I allways get this stupid alert 🙂

Your post does not have much context to explain the code sections; please explain your scenario more clearly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:57:02+00:00Added an answer on May 29, 2026 at 9:57 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continue from this topic link text Hi guys. I am using codeigniter and want
I have posted on this topic before, but as yet I have not had
Id like to know before I continue if this Book => http://exploring.liftweb.net/ is fit
I want to continue one task because my head has some problem with a
I post this topic because I have a problem with my iPhone application since
Preface: I know this is an unusual/improper way to do this. I can do
I'm building a menu with topics and items. Each topic can be expanded and
I read this topic, but his problem maybe different from mine Writing to both
I'm using a mysql memory table as a way to cache data rows which
I know that there have been plenty of topics describing this topic but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.