Angularjs Splice in Nested Array -


hi can removing element nested json array this

json

[{     "id": 1,     "name": "furniture & fixture",     "choice": {         "0": {             "req_goods": "table",             "qty": "10"         },         "1": {             "req_goods": "chair",             "qty": "5"         }     } }, {     "id": 2,     "name": "miscellaneous property",     "choice": {         "0": {             "req_goods": "office rent",             "qty": "1"         }     } }] 

here how remove choice 1 of id 1 .

html

<div ng-repeat="cb in capital_budgets">     <div ng-repeat="choice in choices[$index]">         <input ng-model="cb.choice[$index].req_goods">         <input ng-model="cb.choice[$index].qty">         <button ng-hide="$first" ng-click="removechoice($parent.$index,$index)">-</button>     </div>     <button ng-click="addnewchoice($index)">+</button> </div> 

js

$scope.capital_budgets = [{"id":1,"name":"furniture & fixture"},                           {"id":2,"name":"miscellaneous property"}];     $scope.choices = [{}];     $scope.choices[0] = [{}];     $scope.choices[1] = [{}];     $scope.choices[2] = [{}];     $scope.choices[3] = [{}];     $scope.choices[4] = [{}];      $scope.addnewchoice = function(id) {         $scope.choices[id].push({});     };      $scope.removechoice = function(parent_id, id) {         $scope.choices[parent_id].splice(id, 1);     }; 

the above removechoice() remove last element want remove element user choose remove. please have been trying 2 days.

you can make 'choice' of array type follows , use index of particular choice in ng-repeat directive remove choice choices array.

angular    .module('demo', [])    .controller('defaultcontroller', defaultcontroller);        function defaultcontroller() {      var vm = this;      vm.items = [      {          "id": 1,          "name": "furniture & fixture",          "choices": [          {            "id": 1,            "req_goods": "table",            "qty": "10"          },          {            "id": 2,            "req_goods": "chair",            "qty": "5"          }]      }, {          "id": 2,          "name": "miscellaneous property",          "choices": [          {            "id": 1,            "req_goods": "office rent",            "qty": "1"          }]      }];            vm.removechoice = removechoice;      vm.addchoice = addchoice;            function removechoice(itemid, index) {        (var = 0; < vm.items.length; i++) {          if (vm.items[i].id === itemid) {            vm.items[i].choices.splice(index, 1);            break;          }        }      }            function addchoice(index) {        var id = vm.items[index].choices.length + 1;        vm.items[index].choices.push({          id: id,          req_goods: "",          qty: 0        });      }    }    
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <div ng-app="demo">    <div ng-controller="defaultcontroller ctrl">      <div ng-repeat="item in ctrl.items">        <h3>{{item.name}}</h3>        <div ng-repeat="choice in item.choices">          <input type="text" ng-model="choice.req_goods" />          <input type="text" ng-model="choice.qty" />          <button type="button" ng-click="ctrl.removechoice(item.id, $index)">remove</button>        </div>        <button type="button" ng-click="ctrl.addchoice($index)">add</button>      </div>    </div>  </div>


Comments