溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

angular框架如何實(shí)現(xiàn)全選與單選chekbox

發(fā)布時(shí)間:2021-08-04 14:17:03 來源:億速云 閱讀:113 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)angular框架如何實(shí)現(xiàn)全選與單選chekbox,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

1)頁(yè)面內(nèi)容(靜態(tài)頁(yè))

<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>申請(qǐng)人類型</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代碼

首先當(dāng)然有一個(gè)參數(shù)數(shù)組,用于經(jīng)營(yíng)經(jīng)營(yíng)渲染頁(yè)面:

$scope.dataList=[
   {'chineseName':'百度科技','englishName':'baidukeji','abbreviation':'小度','idTell':'bd'},
    {'chineseName':'小??萍?#39;,'englishName':'xiaoniukeji','abbreviation':'小牛','idTell':'xn'},
    {'chineseName':'京東科技','englishName':'jdkeji','abbreviation':'小京','idTell':'xj'},
    {'chineseName':'順風(fēng)科技','englishName':'sfkeji','abbreviation':'小風(fēng)','idTell':'xf'},
    {'chineseName':'阿里科技','englishName':'alkeji','abbreviation':'小里','idTell':'xl'},
    {'chineseName':'淘寶科技','englishName':'tbkeji','abbreviation':'小寶','idTell':'xb'},
    {'chineseName':'天貓科技','englishName':'tmkeji','abbreviation':'小貓','idTell':'xm'}
];

定義一個(gè)數(shù)組用于存儲(chǔ),選中的選項(xiàng)

$scope.targetChoice=[];

定義一個(gè)很簡(jiǎn)單的方法:用于操作單選,全選的效果實(shí)現(xiàn)

$scope.clicktarget=function(isAllChoiceBtn,tmp,dataList){
  //isAllChoiceBtn:用于表示用戶點(diǎn)擊的是否是全選按鈕,tmp:是數(shù)據(jù)集合中的一個(gè)數(shù)據(jù)對(duì)象
  var count=0;//記錄被選中的子選項(xiàng)的個(gè)數(shù)

  //說明用戶點(diǎn)擊的不是“全選”按鈕
  isAllChoiceBtn==false?(function(){
   //當(dāng)前選項(xiàng)取反
   $scope.targetChoice[tmp.idTell]=!$scope.targetChoice[tmp.idTell];
    //遍歷數(shù)據(jù)集合,統(tǒng)計(jì)已經(jīng)被購(gòu)中的單選項(xiàng)
    angular.foreach(dataList,function(v,k){
     $scope.targetChoice[v.idTell]&& count++;
    }); 
    //如果count等于數(shù)據(jù)集合得長(zhǎng)度,說明所有的單選被購(gòu)中
    count==dataList.length && $scope.allHasChoice=true:$scope.allHasChoice==false;
  })()
   //說明點(diǎn)擊的"全選"按鈕
  :(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++;
    });
  })()
}

關(guān)于“angular框架如何實(shí)現(xiàn)全選與單選chekbox”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI