How to remove a specific row from a list in angularjs

They searched everyone everywhere, could not find a solution. I had a problem removing the CORRECT line from the list.

For example, I have an array below:

$scope.rows = [{
        "ID": 12,
        "customer": "abc",
        "image": "abc.jpg",
},{
        "ID": 13,
        "customer": "klm",
        "image": "klm.jpg",
},{
        "ID": 14,
        "customer": "xyz",
        "image": "xyz.jpg",
}];     

Trying to delete a line where ID = 13 (ID will be obtained from the node server) with the code as follows:

        Socket.on('delete', function( ID ) {

            var a = $scope.rows.indexOf(ID);
            $scope.rows.splice(a, 1)

        });

But this does not delete the correct line.

How can I specify my option to delete the right line, for example:

remove rows("ID" = ID)
+4
source share
3 answers

indexOf search for a substring in an array (and not in a relational array)

Try the following:

var whatIndex = null;
angular.forEach($scope.rows, function(cb, index) {
  if (cb.ID === ID) {
     whatIndex = index;
  }
});

$scope.rows.splice(whatIndex, 1);
+4
source

Delete the currently selected item:

<a href="#" ng-click="remove($index)">Remove an item</a>                     //this one is dynamically generated link using ng-repeat

 $scope.remove = function (item) {
        $scope.retrieveddata.splice(item, 1);
    }

. ($ scope.retreiveddata - )

+2

pass id

$scope.deleteCurrentId = function (ID) {
    for (var i = 0; i <= $scope.row.length; i++) {
        if ($scope.row[i].id == ID) {
            $scope.row.splice(i, 1);
        }
    }
};

$scope.deleteCurrentId = function (ID) {
    angular.forEach($scope.row, function (cb, index) {
        if (cb.id == ID) {
            $scope.row.splice(index, 1);
        }
    });
};
0
source

Source: https://habr.com/ru/post/1527680/


All Articles