Javascript 角度复选框选择全部或从表中选择单个图元
我这里有两个场景,一个是用户单击单个元素并提交,另一个是用户单击select all按钮并提交,以便根据用户的要求,我希望在控制器中获取项目的详细信息 这是我的密码 HTMLJavascript 角度复选框选择全部或从表中选择单个图元,javascript,angularjs,checkbox,Javascript,Angularjs,Checkbox,我这里有两个场景,一个是用户单击单个元素并提交,另一个是用户单击select all按钮并提交,以便根据用户的要求,我希望在控制器中获取项目的详细信息 这是我的密码 HTML <div > <label class="btn btn-info"> <input ng-model="vm.selectAll" type="checkbox" name="selectAll" value="allitems"
<div >
<label class="btn btn-info">
<input ng-model="vm.selectAll" type="checkbox" name="selectAll" value="allitems" > Select all Items
</label>
<button ng-click="vm.purchaseItems(item)" class="btn btn-danger" type="submit" >Purchase selected Items</button>
<table ng-repeat="item in vm.items">
<tbody>
<thead>
<th ><input type="checkbox" ng-checked="vm.selectAll"/></th>
<th >Student Id</th>
<th >Name</th>
<th >School</th>
</thead>
<tr>
<td></td>
<td >{{item.studentId}}</td>
<td >{{item.name}}</td>
<td >{{item.schoolName}}</td>
</tr>
</tbody>
</table>
</div>
我应该使用指令还是只在控制器本身中执行?您需要一个变量来保存所选的值,然后是一个过滤函数来返回结果请参见fiddle
您需要为复选框模型筛选项目才能真正使用Github的这个演示:@Abhishek是一个很好的示例,但有很多bugtoo@VinodLouis请给出更具描述性的答案,谢谢。您应该在数据行的第一列添加一个复选框,
ng repeat
应该位于tr
中。尝试并更新您的代码。若要删除部分选择,以防在所有选择之后选择任何一个,则需要删除“按此处所做的全部选择”
vm.purchaseItems = purchaseItems;
function purchaseItems(item) {
console.log(item);
// I want to log only selected items either single or all based on user call
}
this.setUnsetAll = function(selected){
this.items.forEach(function(ele){
ele.isSelected = selected;
});
}
this.purchaseItems = function(){
var selectedItems = this.items.filter(function(ele){
return (ele.isSelected);
});
console.log(selectedItems)
}