el-table树形结构,带复选框半选择状态(indeterminate)

<el-table
        ref="processData"
        :data="processData"
        highlight-current-row
        row-key="id"
        :tree-props="{children: 'children'}"
      >
        >
        <el-table-column label="字段名称" align="center" class="name">
          <template slot-scope="scope">
            <span>{{ scope.row.Name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否可调整" align="center">
          <template slot-scope="scope">
            <el-checkbox v-model="scope.row.IsAdjust" :indeterminate="scope.row.isIndeterminate" @change="adjustCheck(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="是否显示" align="center">
          <template slot-scope="scope">
            <el-checkbox v-model="scope.row.IsShow" :indeterminate="scope.row.Indeterminate" @change="showCheck(scope.row)" />
          </template>
        </el-table-column>
 </el-table>

 

processData: [
        {
          id: 1,
          Name: '百里',
          parentId: 0,
          IsAdjust: false,
          IsShow: false,
          children: [
            {
              id: 11,
              parentId: 1,
              Name: '守约',
              IsAdjust: false,
              IsShow: false
            },
            {
              id: 12,
              parentId: 1,
              Name: '玄策',
              IsAdjust: false,
              IsShow: false
            }
          ]
        }
],
adjustCheck(row) {
      // 全选、全不选
      if (row.children) {
        if (row.IsAdjust) {
          row.children.map(item => {
            item.IsAdjust = true
          })
        } else {
          row.children.map(item => {
            item.IsAdjust = false
          })
        }
      }
      this.processData.map(item => {
        if (item.children) {
          item.children.map(it => {
            if (it.parentId === row.parentId) {
              item.IsAdjust = true
            }
          })
          // 子项都选中
          const all = item.children.every(flag => flag.IsAdjust)
          if (all) {
            item.IsAdjust = true
            item.isIndeterminate = false
          }
          const some = item.children.some(flag => flag.IsAdjust)
          // 部分选中半选择状态
          if (some) {
            if (!all) {
              item.isIndeterminate = true
            }
          } else {
            item.IsAdjust = false
            item.isIndeterminate = false
          }
        }
      })
    },
showCheck(row) {
      if (row.children) {
        if (row.IsShow) {
          row.children.map(item => {
            item.IsShow = true
          })
        } else {
          row.children.map(item => {
            item.IsShow = false
          })
        }
      }
      this.processData.map(item => {
        if (item.children) {
          item.children.map(it => {
            if (it.parentId === row.parentId) {
              item.IsShow = true
            }
          })
          // 子项都选中则父选
          const all = item.children.every(flag => flag.IsShow)
          if (all) {
            item.IsShow = true
            item.Indeterminate = false
          }
          const some = item.children.some(flag => flag.IsShow)
          // 部分选中半选择状态
          if (some) {
            if (!all) {
              item.Indeterminate = true
            }
          } else {
            item.IsShow = false
            item.Indeterminate = false
          }
        }
      })
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值