带有checkbox的树形表格懒加载(element 2.13.0)

基于element 2.13.0版本的。

新建一个js,eval.js在此js中写入:
"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;
}
"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;
}
新建vue。table-tree-load.vue在此当中写入:
<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      lazy
      :load="load"
      :tree-props="{children: 'child', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="date"
        label="日期"
        width="180"

      >
        <template slot="header" slot-scope="scope">
          <el-checkbox v-model="allSelected" @change="allSelectedChange"></el-checkbox>
          <span>日期</span>
        </template>
        <template slot-scope="scope">
          <el-checkbox v-model="scope.row.checked"  @change="toselect(scope.row)">
          </el-checkbox>
          <span>{{scope.row.date}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        sortable
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
    import treeToArray from "./eval";
    export default {
        name: "table-tree-load",
        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);
            }
        },
        data() {
            return {
                allSelected:false,
                chooseson: true, //全选
                key: true ,//单个点击直到全部选中
                tableData: [{
                    id: 1,
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄',
                    checked:false,

                }, {
                    id: 2,
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄',
                    checked:false,
                    hasChildren:true,
                }, {
                    id: 3,
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄',
                    checked:false,
                    hasChildren:true,
                }, {
                    id: 4,
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄',
                    checked:false,
                }],
                data:[],
                childrenList:[
                    {
                        id: 31,
                        date: '2016-05-01',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1519 弄',
                        checked:false,
                    }, {
                        id: 32,
                        date: '2016-05-01',
                        name: '王小虎',
                        address: '上海市普陀区金沙江路 1519 弄',
                        checked:false,
                    }
                ]
            }
        },
        mounted(){
            this.data=this.tableData;
        },
        methods: {
            load(tree, treeNode, resolve) {
                console.log(tree,treeNode,resolve)
                resolve(this.childrenList)
                this.$set(tree,"child",this.childrenList)
            },
            //设置表头全选
            allSelectedChange(value){
                if (value == true) {
                    this.setchildtobeselect(this.formatData, true);
                } else {
                    this.setchildtobeselect(this.formatData, false);
                }
            },

            //功能函数:选中部分子集
            setchildtobeselect(arr, key) {
                arr.forEach((v, i) => {
                    v.checked = key;
                    // v._expanded = key;//选中后展开子项
                    if (v.child) {
                        this.setchildtobeselect(v.child, v.checked);
                    }
                });
            },
            //是否所有的都被选中
            isallchecked(arr) {
                arr.forEach((v, i) => {
                    if (!v.checked) {
                        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.checked = false;
                                return false; //终止此次循环,减少循环次数
                            }
                        });
                    }
                    if (v.child) {
                        this.setparentfalse(v.child, id, level);
                    }
                });
            },
            //设置父级为 选中状态
            setparenttrue(arr, id, level) {
                console.log(arr)
                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.checked) {
                                key = false;
                            }
                        });
                        if (key && sameidkey) {
                            v.checked = true;
                        }
                    }
                    if (v.child) {
                        this.setparentfalse(v.child, id, level);
                    }
                });
            },
            //某个复选框被点击时
            toselect(row) {
                // row._expanded = row.checked;//选中后是否展开
                //1、若有子集先让子选中
                if (row.child) {
                    this.setchildtobeselect(row.child, row.checked);
                }
                //2、然后判断是否全选中
                this.key = true; //重置为true,防止上次已经是false的状态
                this.isallchecked(this.formatData);
                //3、设置多选框的状态
                if (!row.checked) {
                    this.setparentfalse(this.formatData, row.id, row._level); //设置父级选中的状态为false
                    //设置全选框的状态
                    this.allSelected=false;
                } else {
                    this.setparenttrue(this.formatData, row.id, row._level); //设置父级选中的状态为true
                }
                if (this.key) {
                    //设置全选框的状态
                    this.allSelected=true;
                }
                this.$emit("selectClick",row)
            },


        },
    }
</script>

<style scoped>
</style>

发布了3 篇原创文章 · 获赞 0 · 访问量 43
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览