作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我这里有两种情况,一种是用户单击单个元素并提交,另一种是用户单击“全选”按钮并提交,以便根据用户的要求,我想在 Controller 中获取项目的详细信息。
这是我的代码
HTML
<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>
Controller
vm.purchaseItems = purchaseItems;
function purchaseItems(item) {
console.log(item);
// I want to log only selected items either single or all based on user call
}
我应该使用指令还是可以简单地在 Controller 本身中完成?
最佳答案
您需要一个变量来保存所选值,然后需要一个过滤函数来返回结果,请参阅 fiddle http://jsfiddle.net/SNF9x/296/
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)
}
关于javascript - Angular 复选框选择全部或从表中选择单个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41973101/
我是一名优秀的程序员,十分优秀!