It would have been easier if you had provided a specific sample of your data and html, but I have given a generic solution that should still be helpful.
1) Creating an "add below" button
The ui-tree simply renders your tree model. In Angular, all you need to do is manipulate the data to add a node under another one. To achieve this, you need to access an array of sibling nodes and the current index. The index can be accessed using $index
as ui-tree uses the standard ngRepeat
. Getting the array of siblings is a bit trickier. By looking at the documentation, you can find that $parentNodesScope
refers to the scope of the tree-nodes (siblings container), which contains $modelValue
representing the siblings array. Here is how you can accomplish it:
<button ng-click='$parentNodesScope.$modelValue.splice($index+1,0,{value:"New",nodes:[]})'>Add below</button>
Replace {value:"New",nodes:[]}
with the structure of the data you are working with.
2) Setting focus on the input of the added node
There are various ways to focus inputs in Angular, such as the method discussed in this Stack Overflow answer. Personally, I find this solution most effective. It focuses on elements as they are inserted into the DOM, including when ui-tree re-renders the tree with new nodes. One drawback is that after the initial page load, the focus will default to the last node added, which you can handle accordingly.
You can view a demo of the implementation here: Demo Link