Plunker

Saya ingin menghapus satu baris dari daftar. Awalnya kode di bawah ini ada

var newArray = _.filter($scope.componentList, function(arrayItem) {
return rowId !== arrayItem.rowId;
});
$scope.componentList = newArray;

Apa yang akan dilakukan filter ini adalah jika nilai pengembaliannya benar maka filter berisi objek itu jika tidak, itu akan menghapus objek itu. Sekarang saya hanya ingin mengembalikan yang sama untuk orang tua dan anak juga

Di sini rowId adalah input untuk fungsi ini. Pada baris $scope.componentList = newArray; di atas kita akan mendapatkan apa pun yang akan dihapus oleh rowId objek yang akan dihapus. Dan rowId yang tersisa akan ada di daftar itu. itu baik-baik saja.

return rowId !== arrayItem.rowId; 

Di baris ini apa yang pernah mengembalikan true itu akan ada di newArray.

Tapi sekarang formatnya diubah. Sekarang formatnya seperti di bawah ini.

[
  {
    "revision": 0,  
    "componentName": "abc",
    "componentIdentification": "abc",
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "214",
    "rowId": "3",
    "items": [
      {
        "revision": 0,
        "componentName": "efg",
        "componentIdentification": "efg",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "215",
        "rowId": "3.1",
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "16",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  },
  {
    "revision": 0,
    "componentName": "hij",
    "componentIdentification": "hij",
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "206",
    "rowId": "1",
    "items": [
      {
        "revision": 0,
        "componentName": "klm",
        "componentIdentification": "klm",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "207",
        "rowId": "1.1",
        "items": [
          {
            "revision": 0,
            "componentName": "nop",
            "componentIdentification": "nop",
            "componentType": "2",
            "componentState": "1",
            "componentUrl": null,
            "componentId": "208",
            "rowId": "1.1.1",
            "items": [
              {
                "revision": 0,
                "componentName": "qrs",
                "componentIdentification": "qrs",
                "componentType": "2",
                "componentState": "1",
                "componentUrl": null,
                "componentId": "209",
                "rowId": "1.1.1.1",
                "items": null,
                "componentStateId": 0,
                "ctastatus": 0,
                "actionId": "26",
                "actionToPerform": "1"
              },
              {
                "revision": 0,
                "componentName": "tuv",
                "componentIdentification": "tuv",
                "componentType": "2",
                "componentState": "1",
                "componentUrl": null,
                "componentId": "210",
                "rowId": "1.1.1.2",
                "items": null,
                "componentStateId": 0,
                "ctastatus": 0,
                "actionId": "5",
                "actionToPerform": "1"
              }
            ],
            "componentStateId": 0,
            "ctastatus": 0,
            "actionId": "25",
            "actionToPerform": "1"
          }
        ],
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "1",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  },
  {
    "revision": 0,
    "componentName": "wxy",
    "componentIdentification": "wxy",  
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "211",
    "rowId": "2",
    "items": [
      {
        "revision": 0,
        "componentName": "zab",
        "componentIdentification": "zab",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "212",
        "rowId": "2.1", 
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "7",
        "actionToPerform": "1"
      },
      {
        "revision": 0,
        "componentName": "cde",
        "componentIdentification": "cde",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "213",
        "rowId": "2.2",
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "12",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  }
]

Sekarang hubungan induk dan anak ada antara satu baris dengan baris lainnya dengan array items[]. Jadi saya mencoba menulis kode seperti di bawah ini tetapi tidak berhasil.

Metode panggilan:-

var newArray = $scope.isRowIdExist($scope.componentList,rowId);

metode

$scope.isRowIdExist = function(list,rowId) {

        var newArray = _.filter(list, function(arrayItem) {
            if(rowId != arrayItem.rowId){
                if (arrayItem.items && arrayItem.items.length > 0){
                    $scope.isRowIdExist(arrayItem.items,rowId); // method calling itself
                }
            }
            return rowId !== arrayItem.rowId;
        });

    }
1
Murali 17 Agustus 2017, 15:33

2 jawaban

Jawaban Terbaik

Coba ini, elemen target diganti dengan "DIHAPUS", hapus dari fungsi sambungan pada produksi

angular.module("myApp", [])
  .controller("ctrl", function($scope){
    
    $scope.data = JSON.parse('[{"revision":0,"componentName":"abc","componentIdentification":"abc","componentType":"1","componentState":"1","componentUrl":null,"componentId":"214","rowId":"3","items":[{"revision":0,"componentName":"efg","componentIdentification":"efg","componentType":"2","componentState":"1","componentUrl":null,"componentId":"215","rowId":"3.1","items":null,"componentStateId":0,"ctastatus":0,"actionId":"16","actionToPerform":"1"}],"componentStateId":0,"ctastatus":0,"actionId":"37","actionToPerform":"1"},{"revision":0,"componentName":"hij","componentIdentification":"hij","componentType":"1","componentState":"1","componentUrl":null,"componentId":"206","rowId":"1","items":[{"revision":0,"componentName":"klm","componentIdentification":"klm","componentType":"2","componentState":"1","componentUrl":null,"componentId":"207","rowId":"1.1","items":[{"revision":0,"componentName":"nop","componentIdentification":"nop","componentType":"2","componentState":"1","componentUrl":null,"componentId":"208","rowId":"1.1.1","items":[{"revision":0,"componentName":"qrs","componentIdentification":"qrs","componentType":"2","componentState":"1","componentUrl":null,"componentId":"209","rowId":"1.1.1.1","items":null,"componentStateId":0,"ctastatus":0,"actionId":"26","actionToPerform":"1"},{"revision":0,"componentName":"tuv","componentIdentification":"tuv","componentType":"2","componentState":"1","componentUrl":null,"componentId":"210","rowId":"1.1.1.2","items":null,"componentStateId":0,"ctastatus":0,"actionId":"5","actionToPerform":"1"}],"componentStateId":0,"ctastatus":0,"actionId":"25","actionToPerform":"1"}],"componentStateId":0,"ctastatus":0,"actionId":"1","actionToPerform":"1"}],"componentStateId":0,"ctastatus":0,"actionId":"37","actionToPerform":"1"},{"revision":0,"componentName":"wxy","componentIdentification":"wxy","componentType":"1","componentState":"1","componentUrl":null,"componentId":"211","rowId":"2","items":[{"revision":0,"componentName":"zab","componentIdentification":"zab","componentType":"2","componentState":"1","componentUrl":null,"componentId":"212","rowId":"2.1","items":null,"componentStateId":0,"ctastatus":0,"actionId":"7","actionToPerform":"1"},{"revision":0,"componentName":"cde","componentIdentification":"cde","componentType":"2","componentState":"1","componentUrl":null,"componentId":"213","rowId":"2.2","items":null,"componentStateId":0,"ctastatus":0,"actionId":"12","actionToPerform":"1"}],"componentStateId":0,"ctastatus":0,"actionId":"37","actionToPerform":"1"}]');

    $scope.toDel = "1.1"
    $scope.removeElement = function (data, toDel, parent) {
      data.forEach(function(row, i) {
        if (row.rowId == toDel) {
          if (parent) {
            parent.items.splice(i, 1, "REMOVED")
          } else {
            data.splice(i, 1, "REMOVED")
          }
        } else if (row.items) {
          $scope.removeElement(row.items, toDel, row)
        }
      })
    }
    
    $scope.removeElement($scope.data, $scope.toDel, null);
  
  
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ctrl">
  <pre>{{data | json}}</pre>
</div>
0
Vanojx1 17 Agustus 2017, 14:50

Saya akan merekomendasikan menghindari penyaringan dalam situasi ini. Kode ini berfungsi untuk menghapus orang tua atau anak:

function removeRow(items, rowId) {
    for (var i=0; i<items.length; i++) {
        var item = items[i];
        if (rowId == item.rowId) {
            items.splice(i, 1);
        } else if (rowId.startsWith(item.rowId)) {
            removeRow(item.items, rowId);
        }
        if (item.items != null && item.items.length == 0) item.items = null;
    }
}
var items = [
  {
    "revision": 0,
    "componentName": "abc",
    "componentIdentification": "abc",
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "214",
    "rowId": "3",
    "items": [
      {
        "revision": 0,
        "componentName": "efg",
        "componentIdentification": "efg",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "215",
        "rowId": "3.1",
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "16",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  },
  {
    "revision": 0,
    "componentName": "hij",
    "componentIdentification": "hij",
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "206",
    "rowId": "1",
    "items": [
      {
        "revision": 0,
        "componentName": "klm",
        "componentIdentification": "klm",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "207",
        "rowId": "1.1",
        "items": [
          {
            "revision": 0,
            "componentName": "nop",
            "componentIdentification": "nop",
            "componentType": "2",
            "componentState": "1",
            "componentUrl": null,
            "componentId": "208",
            "rowId": "1.1.1",
            "items": [
              {
                "revision": 0,
                "componentName": "qrs",
                "componentIdentification": "qrs",
                "componentType": "2",
                "componentState": "1",
                "componentUrl": null,
                "componentId": "209",
                "rowId": "1.1.1.1",
                "items": null,
                "componentStateId": 0,
                "ctastatus": 0,
                "actionId": "26",
                "actionToPerform": "1"
              },
              {
                "revision": 0,
                "componentName": "tuv",
                "componentIdentification": "tuv",
                "componentType": "2",
                "componentState": "1",
                "componentUrl": null,
                "componentId": "210",
                "rowId": "1.1.1.2",
                "items": null,
                "componentStateId": 0,
                "ctastatus": 0,
                "actionId": "5",
                "actionToPerform": "1"
              }
            ],
            "componentStateId": 0,
            "ctastatus": 0,
            "actionId": "25",
            "actionToPerform": "1"
          }
        ],
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "1",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  },
  {
    "revision": 0,
    "componentName": "wxy",
    "componentIdentification": "wxy",
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "211",
    "rowId": "2",
    "items": [
      {
        "revision": 0,
        "componentName": "zab",
        "componentIdentification": "zab",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "212",
        "rowId": "2.1",
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "7",
        "actionToPerform": "1"
      },
      {
        "revision": 0,
        "componentName": "cde",
        "componentIdentification": "cde",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "213",
        "rowId": "2.2",
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "12",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  }
];

/*
	Psuedocode:
	Do a fast recursive search for |item.rowId|=|rowId| and delete the one that matches
*/

function removeRow(items, rowId) {
	for (var i=0; i<items.length; i++) {
		var item = items[i];
		if (rowId == item.rowId) {
      items.splice(i, 1);
    } else if (rowId.startsWith(item.rowId)) {
			removeRow(item.items, rowId);
		}
    if (item.items != null && item.items.length == 0) item.items = null;
	}
}

removeRow(items, "1.1.1.1");
console.log(items);
0
clabe45 17 Agustus 2017, 17:39