Angularjs:选中要推送和取消选中以从数组中拼接id

时间:2016-10-25 09:51:19

标签: angularjs arrays push splice

我有一个从PHP脚本中获取的表。下面的代码工作完美,当我勾选方框它成功推动ID,但拼接时它没有运作良好。 当我勾选并且只取消单行时,它也拼接数组。但是当我选择多行时,即使我解开也会一次又一次地推动它。请检查代码。



$scope.exampleArray = [];
 	$scope.pushInArray = function(id) {
	 // get the input value
	/* var inputVal = id;*/
	 var index = $scope.exampleArray.indexOf(id);
	 if( index ){
	 	$scope.exampleArray.push(id);
	 }else { $scope.exampleArray.splice(index, 1);  }
	 	$scope.deleteBulk = function(){

	 		if(confirm("Are you sure you want to delete " + $scope.exampleArray.length + " record(s))?")){
		 		$http.post('http://localhost/angular-code-crud/index.php/user/bulk_delete_user',
	        	{'id':$scope.exampleArray}).success(function(){
          			 $scope.displayUsers();
        		});
     		}
    	};	 
 	};

<table class="table table-striped table-hover table-condensed">
	<thead>
		<tr>
			<th></th>
			<th ng-click="sort('name')">Name
			<span class="glyphicon sort-icon" ng-show="sortKey=='name'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span></th>
			<th ng-click="sort('gender')">Gender<span class="glyphicon sort-icon" ng-show="sortKey=='gender'" ng-class="{' glyphicon glyphicon-chevron-up':reverse,' glyphicon glyphicon-chevron-down':!reverse}"></span>
			<th ng-click="sort('email')">Email<span class="glyphicon sort-icon" ng-show="sortKey=='email'" ng-class="{' glyphicon glyphicon-chevron-up':reverse,' glyphicon glyphicon-chevron-down':!reverse}"></span></th>
			<th>Address</th>
			<th>Phone</th>
			<th>Action</th>
		</tr>
	</thead>
	<tbody>
	<!-- <tr ng-repeat="user in users | filter:searchUser | orderBy:sortKey:reverse | filter:paginate"> -->
	<tr ng-repeat="user in users|orderBy:sortKey:reverse|filter:searchUser " >
	
		<td><!-- <input type="checkbox" ng-click="deleteBulk(user.id)" ng-true-value="{{user.id}}" ng-checked="master" checklist-model="user.id" checklist-value="{{user.id}}">{{ user.id }} -->
			<div class="checkbox">
				<label>
					<input type="checkbox" name="arrExample" ng-model="arrInput" ng-change='pushInArray(user.id)' >
				</label>
			</div>
		</td>
		<td>{{ user.name }}</td>
		<td>{{ user.gender }}</td>
		<td>{{ user.email }}</td>
		<td>{{ user.address }}</td>
		<td>{{ user.phone }}</td>
		<td>
		 <button class="btn btn-primary" ng-click="updateData(user.id, user.name, user.gender, user.email, user.address, user.phone)">Edit <i class="material-icons">mode_edit</i></button>
		<button class="btn btn-danger" ng-click="deleteData(user.id)">Delete <i class="material-icons">delete</i></button>
		</td>
	</tr>
	</tbody>
	</table>
&#13;
&#13;
&#13;

抱歉英语不好。

3 个答案:

答案 0 :(得分:0)

没关系,我发现了哪里有虫。

$scope.exampleArray = [];
 	$scope.pushInArray = function(id) {
	 // get the input value
	/* var inputVal = id;*/
  //$scope.exampleArray.push(id);
	 var index = $scope.exampleArray.indexOf(id);
   console.log(index);
	 if(index === -1 ){ //this is where i have bug. match the condition with -1
	 	$scope.exampleArray.push(id); 
	 } else { $scope.exampleArray.splice(index, 1);}
	 	$scope.deleteBulk = function(){

	 		if(confirm("Are you sure you want to delete " + $scope.exampleArray.length + " record(s))?")){
		 		$http.post('http://localhost/angular-code-crud/index.php/user/bulk_delete_user',
	        	{'id':$scope.exampleArray}).success(function(){
          			 $scope.displayUsers();
        		});
     		}
    	};	 
 	};

答案 1 :(得分:0)

希望这有帮助

JS:

$scope.pushInArray = function(data) {
    var index = $scope.exampleArray.indexOf(data.id);
    if(data.checked) {
        if( index === -1 ) {
            $scope.exampleArray.push(data.id);
        }
    } else {
        $scope.exampleArray.splice(index, 1);
    }
}

HTML:将input type="checkbox"行更改为

<input type="checkbox" name="arrExample" ng-model="user.checked" ng-change='pushInArray(user)' >

答案 2 :(得分:0)

更改条件指数

 $scope.pushInArray = function (id) {
        var index = $scope.exampleArray.indexOf(id);
        if (index <= -1) {
            $scope.exampleArray.push(id);
        } else
            $scope.exampleArray.splice(index, 1);
        };
    };