2016-07-19 227 views
0

我是新的Angular-js請用編輯動態表格創建.I添加一些記錄幫助Json Ang ng-model但如何刪除當前記錄點擊刪除按鈕。 與此運算符一起傳遞。如何刪除當前記錄當點擊刪除按鈕

var app = angular.module("myApp", []); 
 
app.controller("myCtrl", function($scope) { 
 
\t $scope.emplist = [ 
 
\t     {empname:'samudrala',empsalary:'4.5 - pam',empid:'Emp - 450'}, 
 
\t     {empname:'soujanya',empsalary:'4.5 - pam',empid:'Emp - 451'}, 
 
\t     {empname:'suguna',empsalary:'4.5 - pam',empid:'Emp - 452'}, 
 
\t     {empname:'sangeetha',empsalary:'4.5 - pam',empid:'Emp - 453'}, 
 
\t     {empname:'sadhanandham',empsalary:'4.5 - pam',empid:'Emp - 454'}, 
 
\t     {empname:'jai',empsalary:'4.5 - pam',empid:'Emp - 455'}, 
 
\t     {empname:'vijay',empsalary:'4.5 - pam',empid:'Emp - 456'}, 
 
\t     {empname:'Ajay',empsalary:'4.5 - pam',empid:'Emp - 457'}, 
 
\t     {empname:'Sandya',empsalary:'4.5 - pam',empid:'Emp - 458'}, 
 
\t     {empname:'Raamu',empsalary:'4.5 - pam',empid:'Emp - 459'} 
 
\t     ]; 
 
\t $scope.addItem = function(){ 
 
\t \t $scope.emplist.push({'empname':$scope.empname,'empsalary':$scope.empsalary,'empid':$scope.empid}); 
 
\t \t $scope.empname = ''; 
 
\t \t $scope.empsalary = ''; 
 
\t \t $scope.empid = ''; 
 
\t } 
 
\t $scope.remItem = function(x){ 
 
\t \t $scope.emplist.splice(x,1); 
 
\t } 
 
});
body{ 
 
    \t font-size: 14px; 
 
    \t font-family: Arial; 
 
    \t color:#333; 
 
}
<!DOCTYPE Html> 
 
<html> 
 
<head> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
</head> 
 
<body> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
<table style="width:100%;"> 
 
<tr height="25" style=" background: #99ff00;"> 
 
<th width="5%"></th> 
 
<th width="40%">EMP Name</th> 
 
<th width="30%">EMP Salary</th> 
 
<th width="25%">EMP ID</th> 
 
</tr> 
 
<tr height="25" ng-repeat="x in emplist"> 
 
<td style="text-align: center; background: #99ff00;" ng-if="$odd" >{{$index}}</td> 
 
<td style="text-align: center; background: #00ff00;" ng-if="$even" >{{$index}}</td> 
 
<td style="text-align: center; background: #99ff00;" ng-if="$odd" >{{x.empname}}</td> 
 
<td style="text-align: center; background: #00ff00;" ng-if="$even" >{{x.empname}}</td> 
 
<td style="text-align: center; background: #99ff00;" ng-if="$odd">{{x.empsalary}}</td> 
 
<td style="text-align: center; background: #00ff00;" ng-if="$even">{{x.empsalary}}</td> 
 
<td style="text-align: center; background: #99ff00;" ng-if="$odd">{{x.empid}} <button ng-click="remItem();" style="background:#00ffff; border:0px;">&times; Remove</button></td> 
 
<td style="text-align: center; background: #00ff00;" ng-if="$even">{{x.empid}} <button ng-click="remItem();" style="background:#00ffff; border:0px;">&times; Remove</button></td> 
 
</tr> 
 
<tr height="25"> 
 
<td><button ng-click="addItem();" style="background: #00ffff; border:0px; width:100%; height:100%;">Add</button></td> 
 
<td style="padding:2px;"><input type="text" ng-model="empname" style="width:100%;" ></td> 
 
<td style="padding:2px;"><input type="text" ng-model="empsalary" style="width:100%;" ></td> 
 
<td style="padding:2px;"><input type="text" ng-model="empid" style="width:100%;" ></td> 
 
</tr> 
 
</table> 
 
</div> 
 
</body> 
 
</html>

回答

1

而不是顯示爲每個記錄錶行的刪除行,用NG-重複

<tr ng-repeat="emp in emplist"> 
    <td>{{emp.name}}</td> 
    <td>{{emp.name}}</td> 
    <td>{{emp.name}}</td> 
    <td><button ng-click="remItem($index)">Remove</button></td> 

而控制器內部直接你可以得到$ index

$scope.remItem = function(itemIndex){ 
    $scope.emplist.splice(itemIndex,1); 
} 
0

試試這個

<button ng-click="remItem(x);" > Remove</button> 

和控制器

$scope.remItem = function(item){ 
    var index = $scope.emplist.indexOf(item); 
    $scope.emplist.splice(index,1); 
    } 
+0

在ng-repeat指令中,特殊屬性公開本地作用域包括「$ index」,所以你不需要在remove函數中搜索indexOf項: etiennecrb

+0

如果在'ng-repeat'中使用過濾器,則不能使用'$ index'。 –

1

請繞道當前指數在功能

<button ng-click="remItem(x,$index);" > Remove</button> 

$scope.remItem = function(x,index){ 
    $scope.emplist.splice(index,1); 
} 

它會幫助你!

0

**您可以根據指數**

<button title="Remove" ng-click="removeFile($index)"></button> 


$scope.removeFile = function (index) { 
    if (!confirm("Are you sure you want to remove?")) { 
     return; 
    } 
    $scope.emplist.splice(index, 1);} 
相關問題