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

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

效果如下:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现elementui的表格多选框单选: 1. 隐藏多选框:在样式添加以下代码:`.el-table__header .el-checkbox { display: none; }` 2. 监听selection-change事件:在el-table标签添加`@selection-change="handleSelectionChange"`,并在methods添加`handleSelectionChange`方法。 3. 在handleSelectionChange方法判断选的行数,如果大于1,则清除所有选项,只选最后一个的项;否则,将选项赋值给tableSelect。 4. 在table添加`:row-class-name="tableRowClassName"`,并在methods添加`tableRowClassName`方法,用于判断当前行是否被选,如果被选,则添加`row-selected`类名。 5. 在样式添加以下代码:`.row-selected .el-checkbox { display: none; }`,用于隐藏多选框。 代码如下: ```html <template> <el-table :data="tableData" :row-class-name="tableRowClassName" @selection-change="handleSelectionChange"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column label="角色名称" min-width="120" prop="name"></el-table-column> <el-table-column prop="num" label="成员数量" min-width="120"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [], tableSelect: [] } }, methods: { handleSelectionChange(val) { if (val.length > 1) { this.$refs.multipleTable.clearSelection(); this.$refs.multipleTable.toggleRowSelection(val.pop()); } else { this.tableSelect = val; } }, tableRowClassName({ row }) { if (this.tableSelect.indexOf(row) !== -1) { return 'row-selected'; } return ''; } } } </script> <style scoped> .el-table__header .el-checkbox { display: none; } .row-selected .el-checkbox { display: none; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值