日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

element表格实现树形全选_vue+element UI实现树形表格带复选框的示例代码

發布時間:2024/2/28 vue 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 element表格实现树形全选_vue+element UI实现树形表格带复选框的示例代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

eval.js:將數據轉換成樹形數據

/**

* @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:樹形表格組件

{{ scope.$index }}

{{ scope.row[column.value] }}

/**

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: {

// 格式化數據源

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;

},

// 圖標顯示

iconShow(index, record) {

return index === 0 && record.child && record.child.length > 0;

},

//設置表頭全選

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;"

}

})

]);

},

//功能函數:選中部分子集

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);

}

});

},

//設置父級為 未選中狀態(父級的父級沒改變-有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; //終止此次循環,減少循環次數

}

});

}

if (v.child) {

this.setparentfalse(v.child, id, level);

}

});

},

//設置父級為 選中狀態

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) {

//確保當前點擊的在該父級內

sameidkey = true;

}

if (!val.checks) {

key = false;

}

});

if (key && sameidkey) {

v.checks = true;

}

}

if (v.child) {

this.setparentfalse(v.child, id, level);

}

});

},

//某個復選框被點擊時

toselect(row) {

console.log(row);

// row._expanded = row.checks;//選中后是否展開

//1、若有子集先讓子選中

if (row.child) {

this.setchildtobeselect(row.child, row.checks);

}

//2、然后判斷是否全選中

this.key = true; //重置為true,防止上次已經是false的狀態

this.isallchecked(this.formatData);

//3、設置多選框的狀態

if (!row.checks) {

this.setparentfalse(this.formatData, row.id, row._level); //設置父級選中的狀態為false

document.getElementById("chooseall").checked = false; //設置全選框的狀態

} else {

this.setparenttrue(this.formatData, row.id, row._level); //設置父級選中的狀態為true

}

if (this.key) {

document.getElementById("chooseall").checked = true; //設置全選框的狀態

}

}

},

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);

}

};

});

}

};

@keyframes treeTableShow {

from {

opacity: 0;

}

to {

opacity: 1;

}

}

@-webkit-keyframes treeTableShow {

from {

opacity: 0;

}

to {

opacity: 1;

}

}

.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;

}

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

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

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: "無"

}

]

}

]

}

]

}

]

};

},

最終效果:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。

本文標題: vue+element UI實現樹形表格帶復選框的示例代碼

本文地址: http://www.cppcns.com/wangluo/javascript/257270.html

總結

以上是生活随笔為你收集整理的element表格实现树形全选_vue+element UI实现树形表格带复选框的示例代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。