单击“删除”按钮时如何删除当前记录

时间:2016-07-19 07:24:08

标签: angularjs

我是Angular-js的新手请使用动态表创建进行编码。我添加了一些记录帮助Json Ang ng-model但如何在单击删除按钮时删除当前记录。 通过此操作员。



var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
	$scope.emplist = [
	                  {empname:'samudrala',empsalary:'4.5 - pam',empid:'Emp - 450'},
	                  {empname:'soujanya',empsalary:'4.5 - pam',empid:'Emp - 451'},
	                  {empname:'suguna',empsalary:'4.5 - pam',empid:'Emp - 452'},
	                  {empname:'sangeetha',empsalary:'4.5 - pam',empid:'Emp - 453'},
	                  {empname:'sadhanandham',empsalary:'4.5 - pam',empid:'Emp - 454'},
	                  {empname:'jai',empsalary:'4.5 - pam',empid:'Emp - 455'},
	                  {empname:'vijay',empsalary:'4.5 - pam',empid:'Emp - 456'},
	                  {empname:'Ajay',empsalary:'4.5 - pam',empid:'Emp - 457'},
	                  {empname:'Sandya',empsalary:'4.5 - pam',empid:'Emp - 458'},
	                  {empname:'Raamu',empsalary:'4.5 - pam',empid:'Emp - 459'}
	                  ];
	$scope.addItem = function(){
		$scope.emplist.push({'empname':$scope.empname,'empsalary':$scope.empsalary,'empid':$scope.empid});
		$scope.empname = '';
		$scope.empsalary = '';
		$scope.empid = '';
	}
	$scope.remItem = function(x){
		$scope.emplist.splice(x,1);
	}
});

body{
 	font-size: 14px;
 	font-family: Arial;
 	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>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

不要显示每条记录的表格行,而是使用ng-repeat

<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);
}

答案 1 :(得分:1)

请在您的功能中传递当前索引

$html .= '<option value=""></option>';

它会帮助你!!!

答案 2 :(得分:0)

试试这个

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

并在控制器中

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

答案 3 :(得分: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);}