JavaScript

超轻量级php框架startmvc

angular框架实现全选与单选chekbox的自定义

更新时间:2020-05-22 23:06:01 作者:startmvc
2017年7月4日,我原本可以像其他同时一样早点回家,玩几把王者荣耀,但是我没有,因为我

2017年7月4日,我原本可以像其他同时一样早点回家,玩几把王者荣耀,但是我没有,因为我选择留下来,写一篇博客。

项目中经常性的会遇到什么点击“全选”按钮,勾中所有“单选按钮”,当所有单选按钮勾选后,全选按钮自动勾选,这里我并不是想说这是多么难的一个事情,我只是想炫耀下自己写的东西。

(勾选与否,是切换类名来实现的)换一个背景图片而已

1)页面内容(静态页)


<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代码

首先当然有一个参数数组,用于经营经营渲染页面:


$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'}
];

定义一个数组用于存储,选中的选项


$scope.targetChoice=[];

定义一个很简单的方法:用于操作单选,全选的效果实现


$scope.clicktarget=function(isAllChoiceBtn,tmp,dataList){
 //isAllChoiceBtn:用于表示用户点击的是否是全选按钮,tmp:是数据集合中的一个数据对象
  var count=0;//记录被选中的子选项的个数

  //说明用户点击的不是“全选”按钮
 isAllChoiceBtn==false?(function(){
   //当前选项取反
   $scope.targetChoice[tmp.idTell]=!$scope.targetChoice[tmp.idTell];
    //遍历数据集合,统计已经被购中的单选项
    angular.foreach(dataList,function(v,k){
 $scope.targetChoice[v.idTell]&& count++;
 }); 
 //如果count等于数据集合得长度,说明所有的单选被购中
 count==dataList.length && $scope.allHasChoice=true:$scope.allHasChoice==false;
  })()
  //说明点击的"全选"按钮
 :(function(){
 $scope.allHasChoice=!$scope.allHasChoice;//状态取反
 $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 checkbox全选 angular 全选 angularjs 全选单选