jquery - jqGrid: Set in localStorage treegrid expanded after reloadGrid -
i have read this thread , have downloaded demo of oleg in code doesn't work.
i have jqgrid external php json. use setinteval (1 second) refresh data in real time when explode tree closes immediately.
my javascript code jquery(document).ready(function(): `
// odd 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 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; (i = 0; < 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);
`
i have change this
row[30] = index >= 0; row[31] = true;
with position of "expanded" , "loaded" field of json.
you wrote in comment 1 row of json data looks like
{"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}
it means format of data corresponds not repeatitems: true
inside of jsonreader
. because of feature autotedection of json format introduced in jqgrid 4.4.5 , fixed in jqgrid 4.5.0 (see here , here) grid filled. recommend use jsonreader
repeatitems: false
or @ least remove repeatitems: false
don't corresponds data format.
to set expanded
, loaded
column should use
row.expanded = index >= 0; // set expanded column row.loaded = true; // set loaded column
instead of
row[30] = index >= 0; // set expanded column row[31] = true; // set loaded column
additionally should use row.id
instead of row[0]
(see index = $.inarray(row[0], idsofexpandedrows)
in code).
probably need make more changes above. can't tests code, above changes not full. in way above changes required.
additionally careful usage of integers column names. better change names of columns "0"
, "1"
, ..."24"
example "c0"
, "c1"
, ..."c24"
more corresponds names of identifiers , protect additionally id duplicates rowids have integers (see "id":"13"
, "13":"38"
). save time in future.
Comments
Post a Comment