其他分享
首页 > 其他分享> > ant-vue表格动态控制复选框状态

ant-vue表格动态控制复选框状态

作者:互联网

需求:根据表格选中条数去动态的控制表格复选框状态;
思路:1.列表数据增加可选标识2.监听条件去控制标识状态
实现依赖getCheckboxProps组件方法;

具体实现如下:

//template代码
   <a-table
          :rowKey="
            (record, index) => {
              return record.id
            }
          "
          :row-selection="rowSelection"
          :scroll="{ y: 300 }"
          :columns="columns"
          :data-source="data"
          :pagination="paginationP"
        >
        </a-table>

//计算表格行选中属性-getCheckboxProps控制是否可选
  computed: {
    rowSelection() {
      const { selectedRowKeys } = this
      return {
        columnTitle: ' ',
        selectedRowKeys,
        onChange: this.onSelectChange,
        hideDefaultSelections: true,
        getCheckboxProps: record => ({
          props: {
            disabled: record.disabled
          }
        })
      }
    }
  },
//监听表格选中的数据
 watch: {
    selectedRowKeys: {
      handler: function (val) {
        let count = 5 - this.formData.dataDisk.length
        if (count <= val.length) {
          this.data.forEach(item => {
            if (this.selectedRows.indexOf(item) == -1) {
              item.disabled = true
            }
          })
        } else {
          this.data.forEach(item => {
            if (this.selectedRows.indexOf(item) == -1) {
              item.disabled = false
            }
          })
        }
        this.data = [...this.data]
        this.$forceUpdate()
      },
      deep: true,
      immediate: true
    }
  }
  methods:{
    //获取磁盘列表
    async getEngineDisks() {
      const res = await APIservice.getEngineDisks()
      res.disks.forEach(item => {
        item.disabled = false
        item.status == 'available' && this.data.push(item)
      })
      this.dataFilter = JSON.parse(JSON.stringify(this.data))
    },
  }

标签:vue,表格,true,复选框,disabled,item,forEach,动态控制,data
来源: https://blog.csdn.net/qq_32944491/article/details/123583540