欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實(shí)現(xiàn)全選、反選功能

 更新時(shí)間:2020年11月17日 11:10:35   作者:前端交流ing  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)全選、反選功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用vue寫業(yè)務(wù)代碼時(shí)候,后端大神丟給我一堆數(shù)據(jù),要求是做全選,反選功能,然后把用戶更改的數(shù)據(jù)全部返回給他

基本思路

如果父級選中了,那么父級下面的子集全部選中checked=true;

如果子集中選中了一個(gè),那么父級應(yīng)該被勾選中

如果子集一個(gè)都沒有選中,那么父級此時(shí)應(yīng)該沒有選中

最后提交用戶改變后的數(shù)組

(大神原諒我的啰嗦哈)

開始上代碼

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
 <title>Document</title>
 <link rel="stylesheet" href="css/role.css" rel="external nofollow" >
</head>
<body>
 <div id="content" v-cloak>
 <form @submit.prevent="submit">
  <div class="list" v-for="item in addList">
   <h3 class="title">
     <label for=""><input type='checkbox' class='item-check-btn' v-model="item.checked" @click="selectAll(item)"></label>
    {{item.name}}
   </h3>
    <ul class="menu">
     <li v-for="(s,index) in item.children">
      <label for="">
       <input type='checkbox' :value="index" v-model="s.checked" class='item-check-btn' @click="selectItem(item,s,index)">
      </label>
      {{s.name}}
     </li>
    </ul>
  </div>
  <div class="bottom_btn">
   <input type="submit" value="確認(rèn)選擇" class="btn">
  </div>
 </form>
 </div>

</body>
<script src="js/lib/vue.js"></script>
<script src="1.js"></script>
</html>

這是html結(jié)構(gòu)部分

基本上沒有啥好講的

只能說一句(我曹,v-for和v-model,真他M的方便呀,哈哈哈哈😁)

里面用到v-for嵌套v-for做數(shù)據(jù)渲染,看效果(效果看起來很low,大神不要在意)

下面開始上js代碼了

new Vue({
 el: "#content",
 data: {
  addList: [
  {
  "name":"用戶管理",
  "checked":true,
  "children":[
  {"name":"添加","checked":true},
  {"name":"刪除","checked":false},
  {"name":"修改","checked":true}
  ]
  }
 ]
 },
 methods: {
  selectAll: function(item) {
   //如果父級被選中,那么子集循環(huán),全被給checked=true
   if(item.checked){
     item.children.forEach(function(item){
      item.checked=true;
     });
   }else{
   //相反,如果沒有被選中,子集應(yīng)該全部checked=false
     item.children.forEach(function(item){
    item.checked=false;
  
    });
   }
  },
 selectItem: function(item,s,index) {
  //父級選中條件
  //子集有選中的,那么父級checked=true
  //some檢測的就是有一個(gè)滿足的就為true
  item.checked=s.checked ? true : item.children.some(a => a.checked);
  },
  submit: function(item,s) {
  //去拿所有的數(shù)據(jù),返回給后臺
  var formData=this.addList;
  console.log(JSON.stringify(formData));
 }
 }
 });

最后點(diǎn)擊改變的結(jié)果

返回給后臺就是this.addList,此時(shí)已經(jīng)是改變的結(jié)果

在項(xiàng)目中,后臺給我的json就是this.addList的樣子

關(guān)于vue.js組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

更多vue學(xué)習(xí)教程請閱讀專題《vue實(shí)戰(zhàn)教程》

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論