EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: devnull on August 29, 2016, 02:01:29 AM



Title: Get tree-like object from menu
Post by: devnull on August 29, 2016, 02:01:29 AM
Hi;

Is it possible to return a tree like object containing the menus and children from the menu() class ?

i.e
Code:
$('#mm').menu('getItems');

Thanks


Title: Re: Get tree-like object from menu
Post by: tofix on August 30, 2016, 01:52:29 AM
Not supported, I could not find that too.
Finally I ended up selecting all menuItems using jquery without having them as tree.
Because all items are just added before </body> from plain html this structure could not be extracted.

I added custom properties to the menu items and looped all of them with something like

 cmenu.menu('appendItem', {
                    parent: elem.target,
                    text: col.title,
                    custom: value
                });

  var menuItem = cmenu.menu('getItem', $('#customid')[0]); // contains custom
  cmenu.menu('enableItem', menuItem.target);


Title: Re: Get tree-like object from menu
Post by: jarry on August 30, 2016, 02:07:33 AM
This method can be extended as below:
Code:
<script>
(function($){
$.extend($.fn.menu.methods, {
getItems: function(jq){
return _items(jq);

function _items(m){
var items = [];
$(m).children('.menu-item').each(function(){
var item = jq.menu('getItem', this);
if (this.submenu){
item.children = _items(this.submenu);
}
items.push(item);
});
return items;
}
}
});
})(jQuery);
</script>