最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+element UI實現(xiàn)樹形表格帶復(fù)選框的示例代碼

 更新時間:2019年04月16日 11:25:03   作者:bamboozjy  
這篇文章主要介紹了vue+element UI實現(xiàn)樹形表格帶復(fù)選框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一:在component文件夾下新建如下treeTable文件夾,里面有2個文件:

eval.js:將數(shù)據(jù)轉(zhuǎn)換成樹形數(shù)據(jù)

/**
 * @Author: jianglei
 * @Date: 2017-10-12 12:06:49
 */
"use strict";
import Vue from "vue";
export default function treeToArray(
 data,
 expandAll,
 parent = null,
 level = null
) {
 let tmp = [];
 Array.from(data).forEach(function(record) {
 if (record._expanded === undefined) {
  Vue.set(record, "_expanded", expandAll);
 }
 let _level = 1;
 if (level !== undefined && level !== null) {
  _level = level + 1;
 }
 Vue.set(record, "_level", _level);
 // 如果有父元素
 if (parent) {
  Vue.set(record, "parent", parent);
 }
 tmp.push(record);
 if (record.child && record.child.length > 0) {
  const child = treeToArray(record.child, expandAll, record, _level);
  tmp = tmp.concat(child);
 }
 });
 return tmp;
}

index.vue:樹形表格組件

<template>
 <el-table ref="multipleTable" :data="formatData" :row-style="showRow" v-bind="$attrs"> <!-- @header-click="chooseall" -->
 <el-table-column :render-header="renderHeader" width="50" align="center">
  <template slot-scope="scope">
  <el-checkbox v-model="scope.row.checks" @change="toselect(scope.row)"></el-checkbox>
  </template>
 </el-table-column>
 <el-table-column v-if="columns.length===0" width="150">
  <template slot-scope="scope">
  <span v-for="space in scope.row._level" :key="space" class="ms-tree-space"/>
  <span v-if="iconShow(0,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)">
   <i v-if="!scope.row._expanded" class="el-icon-plus"/>
   <i v-else class="el-icon-minus"/>
  </span>
  {{ scope.$index }}
  </template>
 </el-table-column>
 <el-table-column v-for="(column, index) in columns" v-else :key="column.value" :label="column.text" :width="column.width">
  <template slot-scope="scope">
  <!-- Todo -->
  <!-- eslint-disable-next-line vue/no-confusing-v-for-v-if -->
  <span v-for="space in scope.row._level" v-if="index === 0" :key="space" class="ms-tree-space"/>
  <span v-if="iconShow(index,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)">
   <i v-if="!scope.row._expanded" class="el-icon-plus"/>
   <i v-else class="el-icon-minus"/>
  </span>
  {{ scope.row[column.value] }}
  </template>
 </el-table-column>
 <slot/>
 </el-table>
</template>
 
<script>
/**
 Auth: Lei.j1ang
 Created: 2018/1/19-13:59
*/
import treeToArray from "./eval";
export default {
 name: "TreeTable",
 data() {
 return {
  chooseson: true, //全選
  key: true //單個點擊直到全部選中
 };
 },
 props: {
 /* eslint-disable */
 data: {
  type: [Array, Object],
  required: true
 },
 columns: {
  type: Array,
  default: () => []
 },
 evalFunc: Function,
 evalArgs: Array,
 expandAll: {
  type: Boolean,
  default: false
 }
 },
 computed: {
 // 格式化數(shù)據(jù)源
 formatData: function() {
  let tmp;
  if (!Array.isArray(this.data)) {
  tmp = [this.data];
  } else {
  tmp = this.data;
  }
  const func = this.evalFunc || treeToArray;
  const args = this.evalArgs
  ? [tmp, this.expandAll].concat(this.evalArgs)
  : [tmp, this.expandAll];
  return func.apply(null, args);
 }
 },
 methods: {
 showRow: function(row) {
  const show = row.row.parent
  ? row.row.parent._expanded && row.row.parent._show
  : true;
  row.row._show = show;
  return show
  ? "animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;"
  : "display:none;";
 },
 // 切換下級是否展開
 toggleExpanded: function(trIndex) {
  const record = this.formatData[trIndex];
  record._expanded = !record._expanded;
 },
 // 圖標(biāo)顯示
 iconShow(index, record) {
  return index === 0 && record.child && record.child.length > 0;
 },
 
 //設(shè)置表頭全選
 renderHeader(h, data) {
  return h("span", [
  h("input", {
   attrs: {
   id: "chooseall",
   type: "checkbox",
   style:
    "border: 1px solid #dcdfe6;border-radius: 2px;box-sizing: border-box;width: 14px;height: 14px;background-color: #fff;"
   }
  })
  ]);
 },
 //功能函數(shù):選中部分子集
 setchildtobeselect(arr, key) {
  arr.forEach((v, i) => {
  v.checks = key;
  // v._expanded = key;//選中后展開子項
  if (v.child) {
   this.setchildtobeselect(v.child, v.checks);
  }
  });
 },
 //是否所有的都被選中
 isallchecked(arr) {
  arr.forEach((v, i) => {
  if (!v.checks) {
   this.key = false;
  }
  if (v.child) {
   this.isallchecked(v.child);
  }
  });
 },
 //設(shè)置父級為 未選中狀態(tài)(父級的父級沒改變-有bug)
 setparentfalse(arr, id, level) {
  arr.forEach((v, i) => {
  if (v._level == level - 1 && v.child) {
   v.child.forEach((val, ind) => {
   if (val.id == id) {
    v.checks = false;
    return false; //終止此次循環(huán),減少循環(huán)次數(shù)
   }
   });
  }
  if (v.child) {
   this.setparentfalse(v.child, id, level);
  }
  });
 },
 //設(shè)置父級為 選中狀態(tài)
 setparenttrue(arr, id, level) {
  arr.forEach((v, i) => {
  if (v._level == level - 1 && v.child) {
   let key = true;
   let sameidkey = false;
   v.child.forEach((val, ind) => {
   if (val.id == id) {
    //確保當(dāng)前點擊的在該父級內(nèi)
    sameidkey = true;
   }
   if (!val.checks) {
    key = false;
   }
   });
   if (key && sameidkey) {
   v.checks = true;
   }
  }
  if (v.child) {
   this.setparentfalse(v.child, id, level);
  }
  });
 },
 //某個復(fù)選框被點擊時
 toselect(row) {
  console.log(row);
  // row._expanded = row.checks;//選中后是否展開
  //1、若有子集先讓子選中
  if (row.child) {
  this.setchildtobeselect(row.child, row.checks);
  }
  //2、然后判斷是否全選中
  this.key = true; //重置為true,防止上次已經(jīng)是false的狀態(tài)
  this.isallchecked(this.formatData);
  //3、設(shè)置多選框的狀態(tài)
  if (!row.checks) {
  this.setparentfalse(this.formatData, row.id, row._level); //設(shè)置父級選中的狀態(tài)為false
  document.getElementById("chooseall").checked = false; //設(shè)置全選框的狀態(tài)
  } else {
  this.setparenttrue(this.formatData, row.id, row._level); //設(shè)置父級選中的狀態(tài)為true
  }
  if (this.key) {
  document.getElementById("chooseall").checked = true; //設(shè)置全選框的狀態(tài)
  }
 }
 },
 mounted() {
 this.$nextTick(() => {
  var that = this;
  const all = document.getElementById("chooseall");
  all.onchange = function(e) {
  console.log(all.checked);
  if (all.checked == true) {
   that.setchildtobeselect(that.formatData, true);
  } else {
   that.setchildtobeselect(that.formatData, false);
  }
  };
 });
 }
};
</script>
<style rel="stylesheet/css">
@keyframes treeTableShow {
 from {
 opacity: 0;
 }
 to {
 opacity: 1;
 }
}
@-webkit-keyframes treeTableShow {
 from {
 opacity: 0;
 }
 to {
 opacity: 1;
 }
}
</style>
 
<style scoped>
.ms-tree-space {
 position: relative;
 top: 1px;
 display: inline-block;
 font-style: normal;
 font-weight: 400;
 line-height: 1;
 width: 18px;
 height: 14px;
}
.ms-tree-space::before {
 content: "";
}
.processContainer {
 width: 100%;
 height: 100%;
}
table td {
 line-height: 26px;
}
.tree-ctrl {
 position: relative;
 cursor: pointer;
 color: #2196f3;
 margin-left: -18px;
}
</style>

二:在需要的地方引入該組件:

例如:在component文件夾下新建a.vue:

<tree-table :data="data" :columns="columns" border/>
import treeTable from "./TreeTable";
components: { treeTable },
data() {
 return {
  columns: [
  {
   text: "事件",
   value: "event",
   width: 200
  },
  {
   text: "ID",
   value: "id"
  }
  ],
  data: [
  {
   id: 0,
   event: "事件1",
   timeLine: 50,
   comment: "無"
  },
  {
   id: 1,
   event: "事件1",
   timeLine: 100,
   comment: "無",
   children: [
   {
    id: 2,
    event: "事件2",
    timeLine: 10,
    comment: "無"
   },
   {
    id: 3,
    event: "事件3",
    timeLine: 90,
    comment: "無",
    children: [
    {
     id: 4,
     event: "事件4",
     timeLine: 5,
     comment: "無"
    },
    {
     id: 5,
     event: "事件5",
     timeLine: 10,
     comment: "無"
    },
    {
     id: 6,
     event: "事件6",
     timeLine: 75,
     comment: "無",
     children: [
     {
      id: 7,
      event: "事件7",
      timeLine: 50,
      comment: "無",
      children: [
      {
       id: 71,
       event: "事件71",
       timeLine: 25,
       comment: "xx"
      },
      {
       id: 72,
       event: "事件72",
       timeLine: 5,
       comment: "xx"
      },
      {
       id: 73,
       event: "事件73",
       timeLine: 20,
       comment: "xx"
      }
      ]
     },
     {
      id: 8,
      event: "事件8",
      timeLine: 25,
      comment: "無"
     }
     ]
    }
    ]
   }
   ]
  }
  ]
 };
 },

最終效果:

這樣就大工告成了,想要了解更多,可以關(guān)注 vue-element-admin,一個很不錯的后臺管理模版

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

相關(guān)文章

  • Vue3.0靜態(tài)文件存放路徑與引用方式

    Vue3.0靜態(tài)文件存放路徑與引用方式

    這篇文章主要介紹了Vue3.0靜態(tài)文件存放路徑與引用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue.js入門(3)——詳解組件通信

    vue.js入門(3)——詳解組件通信

    這篇文章主要介紹了vue.js入門(3)——詳解組件通信 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。
    2016-12-12
  • Vue項目中配置pug解析支持

    Vue項目中配置pug解析支持

    這篇文章主要介紹了Vue項目中配置pug解析支持的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • vue+elementUi中的table實現(xiàn)跨頁多選功能(示例詳解)

    vue+elementUi中的table實現(xiàn)跨頁多選功能(示例詳解)

    最近在開發(fā)工業(yè)品超市的后臺系統(tǒng),遇到一個需求,就是實現(xiàn)在一個table表格中多選數(shù)據(jù),在網(wǎng)上查了好多,有些方法真的是無語,下面通過本文給大家分享vue+elementUi中的table實現(xiàn)跨頁多選功能,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Vue3中g(shù)etCurrentInstance、頁面中route和router的獲取實現(xiàn)方式

    Vue3中g(shù)etCurrentInstance、頁面中route和router的獲取實現(xiàn)方式

    這篇文章主要介紹了Vue3中g(shù)etCurrentInstance、頁面中route和router的獲取實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • vue項目首屏加載時間優(yōu)化實戰(zhàn)

    vue項目首屏加載時間優(yōu)化實戰(zhàn)

    單頁面應(yīng)用的一個問題就是首頁加載東西過多,加載時間過長。特別在移動端,單頁面應(yīng)用的首屏加載優(yōu)化更是繞不開的話題,這篇文章主要介紹了vue項目首屏加載時間優(yōu)化實戰(zhàn),感興趣的小伙伴們可以參考一下
    2019-04-04
  • Vue3中Composition?API和Options?API的區(qū)別

    Vue3中Composition?API和Options?API的區(qū)別

    Vue3的Composition API和Options API是Vue.js框架中的兩種不同的API,本文主要介紹了Vue3中Composition?API和Options?API的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 如何把vuejs打包出來的文件整合到springboot里

    如何把vuejs打包出來的文件整合到springboot里

    這篇文章主要介紹了如何把vuejs打包出來的文件整合到springboot里,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • vue 如何處理防止按鈕重復(fù)點擊問題

    vue 如何處理防止按鈕重復(fù)點擊問題

    這篇文章主要介紹了vue 如何處理防止按鈕重復(fù)點擊問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • django+vue項目搭建實現(xiàn)前后端通信

    django+vue項目搭建實現(xiàn)前后端通信

    本文主要介紹了django+vue項目搭建實現(xiàn)前后端通信,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評論

马鞍山市| 崇文区| 吉首市| 阿尔山市| 江都市| 麟游县| 厦门市| 绥滨县| 积石山| 开平市| 鄱阳县| 宜春市| 南康市| 新绛县| 洪洞县| 清涧县| 阿坝| 靖远县| 安龙县| 色达县| 罗定市| 沙坪坝区| 寿光市| 大理市| 景谷| 泸西县| 清河县| 乌拉特中旗| 巩留县| 黄骅市| 曲沃县| 田林县| 利川市| 沂南县| 南平市| 城固县| 阿克| 巧家县| 阳曲县| 崇文区| 周宁县|