jQuery UI Widgets › Forums › General Discussions › Navigation › Tree › How To Add Items Programatically?
Tagged: add item to tree, javascript tree, jquery tree, Tree, TreeView
This topic contains 3 replies, has 2 voices, and was last updated by Peter Stoev 13 years, 10 months ago.
-
Author
-
Can someone please post an example of how to add tree items through code – not via static source?
The docs are really unclear around what an “element” is defined as:
$(‘#jqxTree’).jqxTree(‘addTo’, element, parentElement);
How do you reference a specific element by id?
Is this the appropriate way to add a root level element?
$(‘#jqxTree’).jqxTree(‘addTo’, { label: “top level”, id: “1” });Thanks.
Hi Eric,
To add an item programmatically, you can do the following:
1. Add item to the Tree:
$('#jqxTree').jqxTree('addTo', { label: 'Item' });2. Add a sub item to a specific tree item:
var treeItems = $("#jqxTree").jqxTree('getItems');var firstItem = treeItems[0];var firstItemElement = firstItem.element;$('#jqxTree').jqxTree('addTo', { label: 'Item' }, firstItemElement);
3. Add a sub item to a specific tree item by using the id of the item’s associated element:
var elementByID = $('#jqxTree').find("#home")[0];$('#jqxTree').jqxTree('addTo', { label: 'Item' }, elementByID);
‘home’ in the above code is the ID of a LI element.
<li id='home'>Home</li>
Hope this helps.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHow do add an element to the tree with an ‘id’ value?
In your example you refer to id=’home’ …. but how do you add that to the tree?
var elementByID = $(‘#jqxTree’).find(“#home”)[0];
$(‘#jqxTree’).jqxTree(‘addTo’, { label: ‘Item’ }, elementByID);If I do:
$(‘#jqxTree’).jqxTree(‘addTo’, { label: “Sports”, id: “sports” });
and then try to find with:
var elementByID = $(‘#jqxTree’).find(“#sports”)[0];
it is not resolved.
Please advise.
Thanks.
Hi Eric,
In my example, I was adding a new tree item as a sub-item of a tree item with id=”home”
To add a new item which has a specific ID, use this code:
$('#jqxTree').jqxTree('addTo', { html: "<span style='font-weight: bold;' id='myItem'>Item</span>" });Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com -
AuthorPosts
You must be logged in to reply to this topic.