一:在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实现树形表格带复选框的示例代码相关推荐

  1. Material UI 带复选框表格获取选中值(索引)

    vue+Material UI 带复选框表格获取选中值(索引) 发现问题 解决 写在最后 发现问题 神坑UI框架–Material,这款框架我不晓得为什么会这么坑,有这么多的问题为什么会被推出来,真的 ...

  2. 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单

    jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单 easyui 的树(Tree)插件允许您创建一个复选框树.如果您点击一个节点的复选框,这个点击的节点信息将向上和向下继承.例如:点击 ...

  3. iview 下拉select样式_Vue.js相关:iview实现select tree树形下拉框的示例代码

    Vue.js相关:iview实现select tree树形下拉框的示例代码 发布于 2020-3-16| 复制链接 摘记: 本文介绍了iview实现select tree树形下拉框的示例代码,分享给大 ...

  4. html 复选框 mysql_Html:实现带复选框的下拉框(一)

    概述 项目中要用到可多选的下拉框(select),发现HTML中无此控件,故手动模拟实现一下. 模拟所用元素:input,ul,li 代码 模拟实现带复选框的下拉列表 body{ margin: 20 ...

  5. HTML复选框里添加下拉框,Html:实现带复选框的下拉框(一)

    概述 项目中要用到可多选的下拉框(select),发现HTML中无此控件,故手动模拟实现一下. 模拟所用元素:input,ul,li 代码 模拟实现带复选框的下拉列表 body{ margin: 20 ...

  6. elementui 表格表头竖着显示_Vue+Element自定义纵向表格表头教程

    如下所示: 代码如下: {{ item.key }} {{ item.value }} 绑定的是 statDatas 属性是一个 json数组,由key value组成的json,如果需要多列就直接增 ...

  7. 带复选框和简单描述的Qt QTreeWidget树形控件的简单使用

    Qt QTreeWidget树形控件的简单使用 具有选择框的树形控件 具有选择框的树形控件 效果:当选中顶层的树形节点时,子节点全部被选中:当取消选中顶层树形节点时,子节点全部被取消:当选中子节点时, ...

  8. html带复选框的表格,Html 表格行 ID 复选框

    我想将每一行的数据库表 id 作为动态 html 表的复选框值 我正在使用 ajax 从 mysql 数据库中获取数据并创建一个新变量作为 html 文本附加到表的 tbody 上 HTML代码 Vi ...

  9. struts2 标签单选框_Struts 2 UI标签–表单,复选框,单选,选择,提交

    struts2 标签单选框 Struts2 UI tags are used to generate HTML form elements in result pages. Earlier we lo ...

最新文章

  1. C++ STL: lower_bound 和 upper_bound
  2. HP-UX B.11.31从安装到VG配置
  3. WCF从理论到实践(15):响应变化
  4. DPI — Application Assurance — Overview
  5. c#/.net 循序渐进理解-委托
  6. Android-----application的学习
  7. 2、MySQL错误日志(Error Log)详解
  8. python include的功能_在Python的Config中增加Include功能
  9. ajax存储单个数据_科学家现在可以使用单个原子存储数据
  10. python __iter____next__
  11. vue-cli@webpack@4打包分析命令
  12. class文件如何在linux下打开_Linux下文件的三个时间属性
  13. 华为手机楷体字体下载_正楷字体下载正楷字体官方下载[字体下载]-华军软件园...
  14. 新大一C语言程序设计与算法入门学习路线
  15. caffe lmdb
  16. 8.使用xshell上传文件
  17. ROS笔记(一)xxx.launch文件详解
  18. 一半嫁妆钱,换了90000张照片,一个老爹的摄影计划
  19. vitrualbox虚拟机64位安装报错解决
  20. pitch yaw roll 最直观的解释

热门文章

  1. Union-Find 算法应用
  2. 【今晚9点】:对话刘连响——web流媒体开发新变化
  3. 一站式体验腾讯云音视频及融合通信技术
  4. LiveVideoStack线上分享第三季(九):《街舞》《长安十二时辰》背后的文娱大脑...
  5. 激进or务实?HEVC、AV1 和私有Codecs现状
  6. SRS流媒体服务器——Forward集群搭建和源码分析
  7. 打通前后端,这款效能提升开源“神器”你一定要了解
  8. 微信「看一看」 推荐排序技术揭秘
  9. 腾讯AI Lab造出中国第一台临床应用智能显微镜!
  10. 腾讯数字生态大会倒计时4天:请收下这份最全的TEG参会攻略~