angularjs从数组中删除而不知道索引

时间:2016-02-09 16:35:28

标签: angularjs

我有一个arraylist,用于显示请假工作流程。如果休假请求当前保存为草稿,则该请求将添加到我的草稿列表中,但如果已将其从模式表单提交以获得批准,则需要将其从草稿列表中删除并添加到批准列表中。我的问题是列表是我的表单外部,我的表单不知道列表的索引。当我将草稿中的请求更新为批准时,如何将其从草稿列表中删除并将其添加到批准的列表中,而不知道索引。

我想到的第一个想法是使用对象数据库pk创建一个唯一的属性,如data-approval="7",并使用某种jquery函数选择它并从列表中删除该项,但是从我读到的内容中删除,这不是有意义的做事方式。因此,如果不知道索引,我将如何从angularjs中的列表中删除项目?

循环示例。

<ul>
    <li ng-repeat="e in pendingApprovals" data-approval="{{e.id}}">
        <div class="alert alert-info">
            <h5>{{e.title}}</h5>
            <div>
            {{e.type}}          
            </div>      
            {{e.start | date:'MM/dd/yyyy - hh:mm a'}}
            {{e.end | date:'MM/dd/yyyy - hh:mm a'}}     
            <a ng-click="rejectClick(e.id)" class="btn btn-danger btn-xs btn-block"> 
                <i class="fa fa-thumbs-down fa-1"></i>
                Reject                                      
            </a>                    
            <a ng-click="approveClick($index, e.id)" class="btn btn-success btn-xs btn-block"> 
                <i class="fa fa-thumbs-up fa-1"></i>
                Approve                                     
            </a>                                    
        </div>
    </li>
</ul>

JS

modalInstance.result.then(function(formData) {
            $http.post('./calendar/save.json', formData).then(
                function(response) {                    
                    if(formData.update) {
                        $scope.refresh();
                        console.log('refresh')
                        angular.element(elem[0].querySelector('[data-unsubmitted="' + response.data.id + '"]')).remove();
                    } else {
                        $scope.events.push(response.data);
                        $scope.pendingApprovals.push(response.data);
                    }                                       
                }, function(response) {
                    console.log('failure ' + response)
                    // called asynchronously if an error
                    // occurs
                    // or server returns response with an
                    // error status.
                });
        }, function() {
            $log.info('Modal dismissed at: ' + new Date());
        });

1 个答案:

答案 0 :(得分:3)

似乎过滤器@ ste2425实际上是解决方案。

    modalInstance.result.then(function(formData) {
        $http.post('./calendar/save.json', formData).then(
            function(response) {                    
                if(formData.update) {
                    $scope.refresh();                   

                    var items = $scope.pendingApprovals;                

                    var item = $filter('filter')(items, {id: response.data.id}, true)[0];       

                    var index = items.indexOf(item);        

                    $scope.pendingApprovals.splice(index,1);
                } else {
                    $scope.events.push(response.data);
                    $scope.pendingApprovals.push(response.data);
                }                                       
            }, function(response) {
                console.log('failure ' + response)
                // called asynchronously if an error
                // occurs
                // or server returns response with an
                // error status.
            });
    }, function() {
        $log.info('Modal dismissed at: ' + new Date());
    });

我也从这个问题how to get index position of an array item in controller passing a value in angularjs

中找到了帮助
相关问题