如何在ui-tree angular js中添加新节点

时间:2016-09-17 06:57:57

标签: angularjs angular-ui-tree

在我的应用程序中,我使用了角度ui-tree。

现在添加我在代码

下面使用的新节点
 <button class="btn btn-info btn-circle btn-sm" type="button" ng-click='x.tasks.push({task: "New Task",depth:"2", tasks:[]})' style="margin-top:15px" data-nodrag><i class="fa fa-plus"></i></button>

 <script type="text/ng-template" id="nodes_renderer.html">
                            <div ui-tree-handle class="tree-node tree-node-content">

                                <div style="width:100%">
                                    <div style="width:87%;display: inline-block">
                                        <a class="" style="color:#c1c1c1" data-nodrag ng-click="toggle(this)"><span class="fa" ng-class="{'fa-plus': collapsed, 'fa-minus': !collapsed}"></span>
                                            <span class="label label-info"><i class="fa fa-tasks"></i></span> </a>
                                            <span style="width:80%;word-wrap: break-word" class="remove-editable-click cursor-pointer" editable-text="node.task" buttons="no" e-ng-model="WBSData.task" e-ng-blur="changeFieldEdit(this)" e-required data-nodrag e-style="border:1px solid #DDDDDD;width:90px">
                                               {{node.task}}
                                            </span>
                                    </div>
                                    <div style="width:10%;display: inline-block">
                                        <input icheck type="checkbox" ng-model="node.WBSDataSelectedTask" checklist-value="node.task" style="float:right" value="none">
                                    </div>

                                </div>
                            </div>
                            <ol ui-tree-nodes="" ng-model="node.subtasks" ng-class="{hidden: collapsed}">
                                <li ng-repeat="node in node.subtasks" ui-tree-node ng-include="'nodes_renderer1.html'">
                                </li>
                            </ol>
                        </script>
                        <div ui-tree="treeOptions" id="tree-root">

                            <ol ui-tree-nodes ng-model="x.tasks">
                                <li ng-repeat="node in x.tasks" ui-tree-node ng-include="'nodes_renderer.html'"></li>
                            </ol>
                        </div>

通过点击按钮,它正在添加节点,但在底部添加节点。我需要将其添加到顶部。我该怎么办?

1 个答案:

答案 0 :(得分:4)

x.task.push在数组末尾添加item。那就是你需要改变的地方

ng-click='x.tasks.push({task: "New Task",depth:"2", tasks:[]})'

请改用unshift

ng-click='x.tasks.unshift({task: "New Task",depth:"2", tasks:[]})'

供参考:How can I add new array elements at the beginning of an array in JavaScript?