效果图

代码部分:

<template>

  <div>

    <eltable

      ref=”multipleTable”

      :data=”listTreeData

      style=”width: 100%; margin-bottom: 20px

      rowkey=”id

      border

      defaultexpandall

      :treeprops=”{

        children: ‘children’

      }”

    &gt;

      <eltablecolumn width=”105″&gt;

        <template slot=”headerslot-scope=”scope“&gt;

          <span&gt;全选</span&gt;

          <elcheckbox

            vmodel=”selecteds

            style=”paddingleft: 10px

            @change=”checkAllIn(scope)”

          />

        </template>

        <template slot-scope=”scope”>

          <elcheckbox

            vmodel=”scope.row.selected

            style=”padding-left: 10px

            @change=”checkChange(scope.row)”

          /> </template

      ></el-tablecolumn>

      <el-tablecolumn prop=”label” label=”名字” headeralign=”center” align=”center” />

    </el-table>

  </div>

</template>

<script>

export default {

  data () {

    return {

      listTreeData: [

        {

          id: 1,

          label: ‘一级 1’,

          parentId: 0,

          selected: false,

          children: [

            {

              id: 4,

              label: ‘二级 1-1’,

              parentId: 1,

              selected: false,

              children: [

                {

                  id: 9,

                  parentId: 4,

                  selected: false,

                  label: ‘三级 1-1-1’

                },

                {

                  id: 10,

                  parentId: 4,

                  selected: false,

                  label: ‘三级 1-1-2’

                }

              ]

            },

            {

              id: 7,

              parentId: 1,

              selected: false,

              label: ‘二级 1-2’

            }

          ]

        },

        {

          id: 2,

          label: ‘一级 2’,

          parentId: 0,

          selected: false,

          children: [

            {

              id: 5,

              parentId: 2,

              selected: false,

              label: ‘二级 2-1’

            },

            {

              id: 6,

              parentId: 2,

              selected: false,

              label: ‘二级 2-2’

            }

          ]

        }

      ],

      selecteds: ”, // 全选按钮是否选中

      menuData: [], // 数据二维数组形式

      curParentId: ”, // 当前选中节点的父id

      childrenArr: [] // 当前选中节点的孩子数组

    }

  },

  mounted () {

    this.treeToArr(this.listTreeData)

  },

  methods: {

    // 把树结构数据变成二维数组

    treeToArr (data) {

      data.forEach((item) => {

        this.menuData.push(item)

        if (item.children &amp;&amp; item.children.length > 0) {

          this.treeToArr(item.children)

        }

      })

    },

    // 全选复选框

    checkAllIn () {

      this.$refs.multipleTable.data.forEach((items) => {

        this.$set(items, ‘selected’, this.selecteds)

        if (items.children &amp;&amp; items.children.length > 0) {

          this.deepCheck(items.children, this.selecteds)

        }

      })

    },

    // 递归查找符合条件数据

    deepCheck (data, val) {

      data.forEach((item) => {

        this.$set(item, ‘selected’, val)

        if (item.children &amp;&amp; item.children.length > 0) {

          this.deepCheck(item.children, val)

        }

      })

    },

    // 处理树表中子带父的情况

    handlerSonToParent (row) {

      if (this.curParentId !== row.parentId) {

        this.childrenArr.length = 0

        this.menuData.forEach((item) => {

          if (item.parentId === row.parentId) {

            this.childrenArr.push(item)

          }

        })

      }

      this.curParentId = row.parentId

      const filter = this.childrenArr.filter((i) => i.selected)

      if (filter.length === this.childrenArr.length) {

        this.menuData.forEach((item) => {

          if (item.id === this.curParentId) {

            this.$set(item, ‘selected’, true)

          }

        })

      } else {

        this.menuData.forEach((item) => {

          if (item.id === this.curParentId) {

            this.$set(item, ‘selected’, false)

          }

        })

      }

    },

    // 复选框触发事件

    checkChange (row) {

      // 父选子的情况

      if (row.children) {

        if (row.selected) {

          row.children.forEach((item) => {

            this.$set(item, ‘selected’, true)

            if (item.children &amp;&amp; item.children.length > 0) {

              this.deepCheck(item.children, true)

            }

          })

          this.$set(row, ‘selected’, true)

        } else {

          row.children.forEach((item) => {

            this.$set(item, ‘selected’, false)

            if (item.children &amp;&amp; item.children.length > 0) {

              this.deepCheck(item.children, false)

            }

          })

        }

      }

      // 子带父的情况

      if (row.selected) {

        this.handlerSonToParent(row)

      } else {

        this.handlerSonToParent(row)

      }

    }

  }

}

</script>

<style>

</style>

说明:代码中的数据以及字段可根据实际情况修改,此处仅作为例子参考。 

原文地址:https://blog.csdn.net/m0_46318298/article/details/128341724

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_7129.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注