How to expand a specific item in the accordion layout of ExtJS
As required by the project, extJS is used as the front-end framework of the background management system.
As we all know, the background management system generally displays menus Based on permissions.
The menu uses the accordion layout of the panel, and then uses the panel to enclose a tree (treepanel ).
It is inconvenient to refresh, because you want to automatically open the expanded menu of the last time.
The key points are as follows:
1. Use Cookies or the LocalStorage of HTML5 to remember the last menu-expanded rootID.
2. Call the expand () method of the accordion layout neutron element panel.
Some codes in Controller are as follows:
// Load the menu loadMenuResources according to the permission: function (view) {// obtain data from the store Using ajax var _ self = this; // var last_menu_index = 0; // create a tree var tree = _ self. createTree (ESSM. resoures); // Add menu var menuArray = []; Ext. array. each (tree, function (root, index) {// if (root. id) {var last_menu_id = getCookie ("last_menu_id"); if (root. id = last_menu_id) {last_menu_index = index ;}// menuArray. push ({title: root ['text'], autoScroll: true, items: {xtype: 'treepanel ', rootVisible: false, lines: false, border: false, autoScroll: 'none', store: _ self. createTreeStore (root), listeners: {itemclick: function (view, record, item, index, e, eOpts) {// select a menu event // create a controller to display the current content _ self. createController (_ self, record. get ('id'), record. get ('text'), record); // if no error occurs, cache root. id setCookie ("last_menu_id", root. id) ;}}}) ;}; view. add (menuArray); // expandLastMenu (); function expandLastMenu () {// expand the last loaded menu // var last_menu_id = getCookie ("last_menu_id "); // fireEvent // expand try {(view. items. items [last_menu_index]. expand ();} catch (ex) {// eat }};}
Of course, the code is ugly. The key is the expandLastMenu () method. In fact, the logic is very simple. eapand () can be used, and the EXT source code is not involved.
In addition, the two tool methods (reading and writing cookies) are as follows:
/*************************************** ********************* ******************************* // Obtain the cookie value (key) function getCookie (cookieName) {// obtain the cookie string var strCookie = document. cookie; // cut multiple cookies into multiple names/value pairs var arrCookie = strCookie. split (";"); var cookieValue = null; // traverses the cookie array and processes each cookie pair for (var I = 0; I