gpt4 book ai didi

javascript - Angular 复选框选择全部或从表中选择单个元素

转载 作者:行者123 更新时间:2023-12-03 05:06:59 26 4
gpt4 key购买 nike

我这里有两种情况,一种是用户单击单个元素并提交,另一种是用户单击“全选”按钮并提交,以便根据用户的要求,我想在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com