angularjs實現多選框分段全選效果實現
angularjs 實現多選框分段全選效果
在前臺開發(fā)過程中,列表批量選擇是一個開發(fā)人員經常遇到的功能,列表批量選擇的實現方式很多,但是原理基本相同,本文主要來講AngularJs如何簡單的實現列表批量選擇功能。
效果如下
html代碼
<div class="form-group1" style="margin-top: 15px;"> <div class="input-group"> <div class="input-group-addon no-border" style="padding-left: 0px;float: left;"> <p>角色權限:</p> </div> </div> </div> <div class="" ng-repeat="todotype in todolist" style="margin-bottom: 30px;"> <div class="" style="display: flex;"> <div class="" style="background-color: rgb(229,172,78);width: 10%;padding: 1px 5px;line-height: 25px;height: 28px;"> <input style="margin-bottom: 8px;" type="checkbox" ng-model="$parent.selectAll[$index]" ng-click="changeAll($index)" /> <span style="color: white;font-weight: 600;"> {{todotype.type}} </span> </div> <div class="" style="padding-left: 24px;width: 100%;"> <div class="row"> <div class="col-md-2 col-sm-2" ng-repeat="obj in todolist[$index].permissionList" style="margin-bottom: 10px;"> <input type="checkbox" ng-click="funcChange($parent.$index)" ng-model="obj.isSelected" /> <span style="line-height: 25px;">{{obj.name}}</span> </div> </div> </div> </div> </div>
html里面簡單建立一個表格,與批量選擇相關的只有兩處。
一處是ng-click="changeAll($index)
,用來做全選的操作;
ng-click="funcChange($parent.$index)"
用來判斷當前大類里內容是否被全選。
判斷是否全選是用ng-model
進行判斷 第一處ng-model="$parent.selectAll[$index]"
是判斷這一大類是否全選;
ng-model="obj.isSelected"
是判斷每一大類的每一項的選擇;
控制器里的代碼js文件
// 獲取數據 這里具體post請求就不寫了 $scope.todolist = res.data.data; for(var i = 0; i < $scope.todolist.length; i++) { angular.forEach($scope.todolist[i].permissionList, function(i) { i.isSelected = false; }) } // 數據類型如下 $scope.list = [{ permissionList: [{ name: 'Golde', // birthday: '2000-01-10', isSelected: false }, { name: 'King', // birthday: '1990-01-10', isSelected: false }, { name: 'Mark', // birthday: '19890-01-10', isSelected: false }, { name: 'Marie', // birthday: '2010-01-10', isSelected: false } ], type: "制造資源管理" }, { permissionList: [{ name: 'Golde1', // birthday: '2000-01-10', isSelected: false }, { name: 'King1', // birthday: '1990-01-10', isSelected: false }, { name: 'Mark1', // birthday: '19890-01-10', isSelected: false }, { name: 'Marie1', // birthday: '2010-01-10', isSelected: false } ], type: "制造資源管理2" }, { permissionList: [{ name: 'Golde2', // birthday: '2000-01-10', isSelected: false }, { name: 'King2', // birthday: '1990-01-10', isSelected: false }, { name: 'Mark2', // birthday: '19890-01-10', isSelected: false }, { name: 'Marie2', // birthday: '2010-01-10', isSelected: false } ], type: "制造資源管理3" }]; // 具體方法如下所示 // 這里初始化數組 上來全為空 $scope.selectAll = []; // 對于對象進行操作的時候(點擊),會執(zhí)行funcChange // 判斷對象數組中isSelected 是否為 true或false,在決定select是否為true $scope.changeAll = function(index) { //全選/取消全選 angular.forEach($scope.todolist[index].permissionList, function(v, k) { v.isSelected = $scope.selectAll[index]; }) }; $scope.funcChange = function(index) { // 當所有都選中時 $scope.selectAll[index] = true; angular.forEach($scope.todolist[index].permissionList, function(v, k) { $scope.selectAll[index] = $scope.selectAll[index] && v.isSelected; }); };
具體邏輯已經寫得很清楚
以上就是angularjs實現多選框分段全選效果實現的詳細內容,更多關于angularjs多選框分段全選的資料請關注腳本之家其它相關文章!
相關文章
解決angularjs WdatePicker ng-model的問題
今天小編就為大家分享一篇解決angularjs WdatePicker ng-model的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09angularjs封裝bootstrap時間插件datetimepicker
這篇文章主要介紹了angularjs封裝bootstrap時間插件datetimepicker 的相關資料,需要的朋友可以參考下2016-06-06Angular指令封裝jQuery日期時間插件datetimepicker實現雙向綁定示例
這篇文章主要介紹了Angular指令封裝jQuery日期時間插件datetimepicker實現雙向綁定示例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01Angular中ng?update命令force參數含義詳解
這篇文章主要為大家介紹了Angular中ng?update命令force參數含義詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Angular中封裝fancyBox(圖片預覽)遇到問題小結
這篇文章主要介紹了Angular中封裝fancyBox(圖片預覽)遇到的問題小結,需要的朋友可以參考下2017-09-09