elementUI中el-table多选框样式的单选框

本文介绍了如何修改Vue Element UI中的el-table组件,使其在单选模式下采用多选框样式,并展示了相关的代码实现和CSS调整。通过实例说明了如何在主js中注册组件并展示最终效果。

el-table中单选样式不太明显,所以改了个多选框样式的单选功能。

代码如下:

<template>
  <el-table
    ref="elTable"
    v-bind="$attrs"
    class="custom-table"
    :class="{'custom-table-radio': type == 'radio', 'is-row-click': isRowClick}"
    v-on="{...$listeners, 'selection-change': () => {}, 'row-click': ()=>{}}"
    @selection-change="handleSelectionChange"
    @row-click="setCurrent"
  >
    <el-table-column
      v-if="type == 'radio' || type == 'checkbox'"
      type="selection"
      column-key="index"
      width="50"
    />
    <slot />
  </el-table>
</template>
<script>
export default {
  name: 'CheckboxTable',
  props: {
    type: {
      type: String, // checkbox or radio
      default: 'none'
    },
    isRowClick: { // 是否点击一行勾选
      type: Boolean,
      default: true
    }
  },
  methods: {
    toggleRowExpansion(a, b) {
      this.$refs.elTable.toggleRowExpansion(a, b)
    },
    clearSelection() {
      this.$refs.elTable.clearSelection()
    },
    toggleRowSelection(data, selected) {
      this.$refs.elTable.toggleRowSelection(data, selected)
    },
    setCurrentRow(row) {
      this.$refs.elTable.setCurrentRow(row)
    },
    // setCurrent(row, event, column) {//2.4.11写法
    setCurrent(row, column, event) { // 2.8.2写法
      if (this.isRowClick) {
        if (this.type === 'radio' || this.type === 'checkbox') {
          this.$refs.elTable.toggleRowSelection(
            row
            // this.data[event.currentTarget.rowIndex]
          )
        }
      }
      this.$emit('row-click', row, event, column)
    },
    handleSelectionChange(selection) {
      if (this.type === 'radio' && selection.length > 1) {
        this.$refs.elTable.toggleRowSelection(selection[0])
        return
      }
      this.$emit('selection-change', selection)
    }
  }
}
</script>
 // element-ui.scss
.custom-table.is-row-click td.el-table-column--selection .cell::after {
  content: " ";
  cursor: pointer;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 10;
}
.custom-table-radio th.el-table-column--selection .el-checkbox {
  display: none;
}

main.js中注册

Vue.component('checkbox-table', () => import('@/components/CheckboxTable')) // checkbox-table

效果如下:

在这里插入图片描述

可以通过监听 el-table 的 @row-click 事件来实现 el-table 多选框的单功能。具体步骤如下: 1.在 el-table 中添加 type="selection",开启多选框功能。 2.在 el-table 中添加 @row-click="handleRowClick" 监听行点击事件。 3.在 methods 中添加 handleRowClick 方法,通过 $refs 获取到 el-table 的 selection 组件,然后将当前行的数据设置为中项。 下面是示例代码: ```html <template> <el-table ref="table" :data="tableData" @row-click="handleRowClick" style="width: 100%"> <el-table-column type="selection" width="55"> </el-table-column> <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [{ date: '2021-01-01', name: '张三', address: '北京市朝阳区' }, { date: '2021-01-02', name: '李四', address: '上海市浦东新区' }, { date: '2021-01-03', name: '王五', address: '广州市天河区' }], selection: [] } }, methods: { handleRowClick(row) { this.$refs.table.toggleRowSelection(row) this.selection = this.$refs.table.selection } } } </script> ``` 在上面的代码中,我们通过监听 @row-click 事件来实现了 el-table 多选框的单功能。在 handleRowClick 方法中,我们通过 $refs 获取到 el-table 的 selection 组件,然后将当前行的数据设置为中项。最后,我们将中的数据保存在 selection 变量中,以便后续使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值