ElementUI中el-table多选表格实现单选效果

HTML代码:

<el-table :data="tableData" ref="singleTable" @selection-change="handleSelectionChange">
	<el-table-column type="selection" width="30">
	</el-table-column>
	<el-table-column prop="measureTime" label="MeasureTime" min-width="120">
	</el-table-column>
	<el-table-column prop="line" label="Line" min-width="60">
	</el-table-column>
	<el-table-column prop="series" label="Car Model" min-width="100">
	</el-table-column>
	<el-table-column prop="color" label="Color" min-width="100">
	</el-table-column>
	<el-table-column prop="igefNo" label="IGEF" min-width="100">
	</el-table-column>
	<el-table-column prop="updateBy" min-width="120" label="Update BY">
	</el-table-column>
	<el-table-column prop="updateTime" min-width="120" label="Update Time">
	</el-table-column>
</el-table>

js代码:

multipleSelection: null //data中记录选中值

handleSelectionChange(selection) {
        if (selection.length > 1) {
            // 清空所有选中
            this.$refs.singleTable.clearSelection();
            this.$refs.singleTable.toggleRowSelection(selection.pop());
        } else {
            this.multipleSelection = selection.pop();
        }
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`el-table` 是 Element UI 提供的一个用于展示表格数据的组件,它支持丰富的功能,包括多功能。要实现互斥项(即一次只能择一项或多项,但不能同时择多项),你可以利用 `row-key` 和 `selection-method` 属性来定制择行为。 **1. row-key**:设置每一行的唯一标识,当使用多时,这个属性很重要,因为它是确定哪一行被的依据。如果你的表格数据每个对象有一个独特的 ID,可以设置为 `row-key="id"`。 **2. selection-method**:这是一个自定义方法,用于控制如何根据用户的择更新行的状态。你可以在这个方法添加互斥逻辑,例如: ```javascript methods: { selectRow(row) { if (this.selectedRows.length > 0) { // 如果已其他项,取消当前项的择 this.$refs.table.clearSelection(); this.selectedRows.forEach(row => row.isSelected = false); } // 添加新的项 row.isSelected = true; this.selectedRows.push(row); }, deselectRow(row) { // 如果当前项被,移除它 row.isSelected = false; const index = this.selectedRows.indexOf(row); if (index !== -1) { this.selectedRows.splice(index, 1); } }, } ``` **3. 在模板上绑定事件**: 在表格的每行单元格上绑定 `@click` 事件,调用对应的 `selectRow` 或 `deselectRow` 方法。 ```html <template> <el-table :data="tableData" ref="table" :row-key="rowKey" selection-method="selectRow" @select="handleSelect" @row-click="handleRowClick" > <!-- 表格列 --> </el-table> </template> ``` **
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值