angular框架實現(xiàn)全選與單選chekbox的自定義
2017年7月4日,我原本可以像其他同時一樣早點回家,玩幾把王者榮耀,但是我沒有,因為我選擇留下來,寫一篇博客。
項目中經(jīng)常性的會遇到什么點擊“全選”按鈕,勾中所有“單選按鈕”,當所有單選按鈕勾選后,全選按鈕自動勾選,這里我并不是想說這是多么難的一個事情,我只是想炫耀下自己寫的東西。
(勾選與否,是切換類名來實現(xiàn)的)換一個背景圖片而已
1)頁面內容(靜態(tài)頁)
<ul class='list-inline my-list-inline'>
<li class="action-check" ng-class="{'active':allHasChoice}" ng-click="clicktarget(true,tmp,dataList)">
<li>中文名稱</li>
<li>英文名稱</li>
<li>申請人類型</li>
<ul>
<div class="list-data">
<table>
<tbody>
<tr ng-repeat="tmp in dataList track by tmp.idTell"
ng-class="{true:'active',false:''}[targetChoice[tmp.idTell]==true]"
ng-click="ctmlicktarget(false,tmp,dataList)"
>
<td>
<span class="choice-icon"></span>
<span class="company-name over-flow-handel-style" title="{{tmp.chineseName}}"
ng-bind="tmp.cineseName"></span>
</td>
<td>
<span class="english-name over-flow-handel-style" title="{{tmp.englishName}}"
ng-bind="tmp.englishName"></span>
</td>
<td>
<span class="english-name over-flow-handel-style" title="{{tmp.abbreviation}}"
ng-bind="tmp.abbreviation"></span>
</td>
</tr>
</tbody>
</table>
2)js代碼
首先當然有一個參數(shù)數(shù)組,用于經(jīng)營經(jīng)營渲染頁面:
$scope.dataList=[
{'chineseName':'百度科技','englishName':'baidukeji','abbreviation':'小度','idTell':'bd'},
{'chineseName':'小??萍?,'englishName':'xiaoniukeji','abbreviation':'小牛','idTell':'xn'},
{'chineseName':'京東科技','englishName':'jdkeji','abbreviation':'小京','idTell':'xj'},
{'chineseName':'順風科技','englishName':'sfkeji','abbreviation':'小風','idTell':'xf'},
{'chineseName':'阿里科技','englishName':'alkeji','abbreviation':'小里','idTell':'xl'},
{'chineseName':'淘寶科技','englishName':'tbkeji','abbreviation':'小寶','idTell':'xb'},
{'chineseName':'天貓科技','englishName':'tmkeji','abbreviation':'小貓','idTell':'xm'}
];
定義一個數(shù)組用于存儲,選中的選項
$scope.targetChoice=[];
定義一個很簡單的方法:用于操作單選,全選的效果實現(xiàn)
$scope.clicktarget=function(isAllChoiceBtn,tmp,dataList){
//isAllChoiceBtn:用于表示用戶點擊的是否是全選按鈕,tmp:是數(shù)據(jù)集合中的一個數(shù)據(jù)對象
var count=0;//記錄被選中的子選項的個數(shù)
//說明用戶點擊的不是“全選”按鈕
isAllChoiceBtn==false?(function(){
//當前選項取反
$scope.targetChoice[tmp.idTell]=!$scope.targetChoice[tmp.idTell];
//遍歷數(shù)據(jù)集合,統(tǒng)計已經(jīng)被購中的單選項
angular.foreach(dataList,function(v,k){
$scope.targetChoice[v.idTell]&& count++;
});
//如果count等于數(shù)據(jù)集合得長度,說明所有的單選被購中
count==dataList.length && $scope.allHasChoice=true:$scope.allHasChoice==false;
})()
//說明點擊的"全選"按鈕
:(function(){
$scope.allHasChoice=!$scope.allHasChoice;//狀態(tài)取反
$scope.allHasChoice==false?
//說明取消全選
:angular.forEach(dataList,function(v,k){
$scope.targetChoice[v.idTell]=$scope.allHasChoice;
count=0;
})
//說明選中全選
:angular.forEach(dataList,function(v,k){
$scope.targetChioce[v.idTell]=$scope.allHasChoice;
count++;
});
})()
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Angular 4.x 動態(tài)創(chuàng)建表單實例
本篇文章主要介紹了Angular 4.x 動態(tài)創(chuàng)建表單實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Angularjs使用directive自定義指令實現(xiàn)attribute繼承的方法詳解
這篇文章主要介紹了Angularjs使用directive自定義指令實現(xiàn)attribute繼承的方法,結合實例形式較為詳細的分析了基于directive自定義指令實現(xiàn)attribute繼承的具體步驟與相關技巧,需要的朋友可以參考下2016-08-08
Angular中使用Intersection Observer API實現(xiàn)無限滾動效果
這篇文章主要介紹了Angular中使用Intersection Observer API實現(xiàn)無限滾動,實現(xiàn)原理為 在data下面加一個loading元素 如果此元素進入視窗 則調用api獲取新的數(shù)據(jù)加到原來的數(shù)據(jù)里面,這時loading就會被新數(shù)據(jù)頂下去,感興趣的朋友一起看看吧2023-12-12
詳解AngularJS controller調用factory
本篇文章主要介紹了詳解AngularJS controller調用factory,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
在 Angular2 中實現(xiàn)自定義校驗指令(確認密碼)的方法
這篇文章給大家探索 Angular 2 內建的自定義驗證,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01
詳解關于Angular4 ng-zorro使用過程中遇到的問題
這篇文章主要介紹了詳解關于Angular4 ng-zorro使用過程中遇到的問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

