如何在angularjs中删除ng重复数据

时间:2017-01-07 18:44:39

标签: javascript html angularjs

我有两个表,我想在$ scope.notiData中附加第二个表格数据,如果我点击删除符号X,我该怎样才能删除重复数据。我有一些代码,但它不起作用。请帮助任何人

http://jsfiddle.net/A6bt3/118/

var app = angular.module('myApp', []);
function checkBoxCtrl($scope) {
$scope.tableOne = [{
        firstname: 'robert',
        value: 'a'
    }, {
        firstname: 'raman',
        value: 'b'
    }, {
        firstname: 'kavi',
        value: 'c'
    }, {
        firstname: 'rorank',
        value: 'd'
    }
];
$scope.tableTwo = [];//the table to be submitted
function removeitems(tableRef) { //revmove items from tableRef
    var i;
    for (i = tableRef.length - 1; i >= 0; i -= 1) {
        if (tableRef[i].checked) {
            tableRef.splice(i, 1);
        }
    }
}
$scope.btnRight = function () {
   //Loop through tableone
    $scope.tableOne.forEach(function (item, i) {
       // if item is checked add to tabletwo
        if (item.checked) {
            $scope.tableTwo.push(item);
        }
    })
    removeitems($scope.tableOne);
}
$scope.btnAllRight = function () {
    $scope.tableOne.forEach(function (item, i) {
        item.checked = true;
        $scope.tableTwo.push(item);
    })
    removeitems($scope.tableOne);
}
$scope.btnLeft = function () {
    $scope.tableTwo.forEach(function (item, i) {
        if (item.checked) {
            $scope.tableOne.push(item);
        }
    })
    removeitems($scope.tableTwo);
}
$scope.btnAllLeft = function () {
    $scope.tableTwo.forEach(function (item, i) {
        item.checked = true;
        $scope.tableOne.push(item);
    })
    removeitems($scope.tableTwo);
}
$scope.done = function () {
    //alert(angular.toJson($scope.tableTwo));
        $scope.notiData = $scope.tableTwo;  
}
    $scope.removeRow = function () {

}

};

3 个答案:

答案 0 :(得分:0)

这样做:

$scope.removeRow = function (item) {
  var index = $scope.notiData.indexOf(item);
  $scope.notiData.splice(index, 1); 
}

并合并数组:

$scope.done = function () {
    angular.extend($scope.notiData, $scope.tableTwo);
}

别忘了初始化notiData:

$scope.notiData = [];

解决重定向问题,删除target="_blank"并更改href

<a ng-repeat="data in notiData" class="emailButton" href="#">{{data.firstname}}<div class="" ng-click="removeRow()">X</div></a>

http://jsfiddle.net/A6bt3/122/

答案 1 :(得分:0)

html元素

$scope.removeRow = function (index) {
        $scope.notiData.splice(index,1);
}

删除功能

JSONObjects

答案 2 :(得分:0)

从视图中传递removeRow()函数中数据元素的索引。

<a ng-repeat="data in notiData" class="emailButton" href="#">{{data.firstname}}
  <div class="" ng-click="removeRow($index)">X</div>
</a>

并在控制器中过滤列表中传递的索引元素。

$scope.removeRow = function (index) {

     $scope.notiData = $scope.notiData.filter(function(elem){      
               return elem !== $scope.notiData[index]
        })
 }

小提琴:http://jsfiddle.net/w5upkawt/