2017-01-12 41 views
0

我有一個用戶添加示例數據的情況,如圖所示。我怎樣才能添加刪除按鈕,只刪除特定的樣本。從圖中,我想「刪除樣品1" 刪除樣品1個記錄。angularjs ng-repeater動態刪除數據

感謝

enter image description here

代碼觸發時添加樣品按鈕被點擊

$ scope.addSample =功能( ){

$scope.partdetails = [ 
    { 
     "sampleseq": 0, 
     "sampleid": 1, 
     "attribute": "Height", 
     "measureunit": "Centimeter", 
     "actualvalue": null, 
     "lowerspec": 1.23, 
     "upperspec": 3.23, 
     "notes": null 
    }, 
    { 
     "sampleseq": 0, 
     "sampleid": 2, 
     "attribute": "Diameter", 
     "measureunit": "Inches", 
     "actualvalue": null, 
     "lowerspec": 1.23, 
     "upperspec": 3.23, 
     "notes": null 
    } 
    ]; 

    $scope.partdetails[0].sampleseq = $scope.sampleCount; 
    $scope.partdetails[1].sampleseq = $scope.sampleCount; 

    $scope.partdetailsList.push($scope.partdetails[0]); 
    $scope.partdetailsList.push($scope.partdetails[1]); 

    $scope.sampleCount += 1; 
} 

enter image description here

+0

請包含數據結構的示例和視圖的HTML。 – georgeawg

+0

你想在行或表頂部添加刪除按鈕的位置?當用戶點擊delete-1時,你想刪除所有ID爲1的樣本?那是對的嗎?基本的例子是在這裏http://jsfiddle.net/crrypdLj/1/ – user3249448

+0

當我說樣本1,這意味着前兩個頂部的行。只有這些行需要刪除,因爲它們屬於樣本1。不是所有的行。不幸的是,jsfiddle鏈接一次只刪除一行。 – bp581

回答

0

您可以使用Array.prototype.filter得到一個新的數據記錄,而不未能斷言功能

比方說你的刪除按鈕調用一個函數的項目稱爲deleteSample並傳遞它的樣品ID:

HTML

... ng-repeat="row in records" ... 

<button ng-click="deleteSample(row.sampleid)">Delete sample</button> 

控制器

$scope.deleteSample = function(sampleId) { 
    $scope.records = $scope.records.filter((row)=> { 
     return !angular.equals(sampleId, row.sampleid) 
    }); 
} 

當然,這並不解決您可能需要在你的數據庫中刪除行過這個問題。