ElementPlus el-table点击单元格变为输入框

开发项目的时候遇到一个要求是当点击el-table的单元格时要对应将其变为可输入的输入框,研究了文档和网上的博客后,实现了这个效果

首先是表格的html部分,通过插槽scope进行表格的自定义,方便后续的元素格式转换

<el-table :data="Table.printData.data" stripe :border="true">
	<el-table-column label="商品编号">
		<template #default="scope">
			<template v-if="scope.cellIndex == Table.cellIndex.value && scope.$index == Table.tableIndex.value">
                <el-input v-model="scope.row.goodsid" @blur="Table.saveCell(scope,scope.$index)" v-focus />
			</template>
	        <template v-else>
				<div class="box" @click="Table.editCell(scope, scope.$index)">{{ scope.row.goodsid }}
                </div>
		    </template>
		</template>
	</el-table-column>
	<el-table-column label="打印数量">
		<template #default="scope">
			<template v-if="scope.cellIndex == Table.cellIndex.value && scope.$index == Table.tableIndex.value">
			    <el-input v-model="scope.row.PrintCount" @blur="Table.saveCell" v-focus />
			</template>
			<template v-else>
				<div class="box" @click="Table.editCell(scope, scope.$index)">{{ scope.row.PrintCount }}</div>
			</template>
		</template>
	</el-table-column>
</el-table>

然后是function部分,通过传入的scope和scope.$index来确定单元格位置以及所处的表格行数,并定义两个行和格变量进行条件判断,当行和格的值等于单元格的行和高的值时就把那个单元格的元素改为input,并通过自定义指令v-focus进行焦点的自动获取,当焦点失去时,将变量的值还原,同时绑定的单元格的值也会被随之改变

printTable是数组内容,对应table的字段添加就好了,这里不赘述

let Table = {
printData:reactive({data:[]}),
cellIndex: ref(-1),
tableIndex: ref(-1),
editCell: function (row, ind) {
		Table.cellIndex.value = row.cellIndex;
		Table.tableIndex.value = ind;
    },
saveCell: function (type, id, index) {
		Table.cellIndex.value = -1;
		Table.tableIndex.value = -1;
	}
}

在main.ts文件注册自定义指令

app.directive('focus', {
	mounted(el) {
		el.querySelector('input').focus();
	}
});

到此就完成功能的开发了

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在 Element UI 的表格(el-table实现内容可编辑并包含下拉框选择的功能,可以按照以下步骤进行操作: 1. 在 `el-table` 设置 `:show-summary="false"`,以确保表格不使用汇总行。 2. 在表格的列定义,使用 `:editable="true"` 属性来标记可编辑的列,并使用 `:formatter` 属性来定义单元格的显示内容。例如: ```html <el-table :data="tableData" :show-summary="false"> <el-table-column prop="name" label="Name" :editable="true"> <!-- 列的其他配置 --> </el-table-column> <el-table-column prop="age" label="Age" :editable="true"> <!-- 列的其他配置 --> </el-table-column> <el-table-column prop="gender" label="Gender" :editable="true" :formatter="formatGender"> <!-- 列的其他配置 --> </el-table-column> <!-- 其他列定义 --> </el-table> ``` 3. 在 Vue 组件定义下拉框的选项数据,并创建一个方法来格式化下拉框列的显示内容。例如: ```javascript data() { return { tableData: [ { name: 'John', age: 25, gender: 'Male' }, { name: 'Jane', age: 30, gender: 'Female' }, // 其他行数据 ], genderOptions: ['Male', 'Female'] // 下拉框选项数据 }; }, methods: { formatGender(row) { return row.gender; } } ``` 4. 在表格列的模板,根据 `editable` 的值来决定是否显示输入框或下拉框。 ```html <el-table-column prop="name" label="Name" :editable="true"> <template slot-scope="scope"> <span v-if="scope.row !== editingRow">{{ scope.row.name }}</span> <el-input v-else v-model="scope.row.name"></el-input> </template> </el-table-column> <el-table-column prop="gender" label="Gender" :editable="true" :formatter="formatGender"> <template slot-scope="scope"> <span v-if="scope.row !== editingRow">{{ formatGender(scope.row) }}</span> <el-select v-else v-model="scope.row.gender" placeholder="Select"> <el-option v-for="option in genderOptions" :key="option" :label="option" :value="option"></el-option> </el-select> </template> </el-table-column> ``` 在这个示例,如果当前行不是正在编辑的行,则显示文本或下拉框的选项。如果当前行是正在编辑的行,则显示输入框或下拉框,并使用 `v-model` 指令绑定输入框或下拉框的值到数据。 5. 可以根据需要添加其他编辑操作,例如保存编辑、取消编辑等。 这样,你就可以在 Element UI 的表格实现内容可编辑且包含下拉框选择的功能。根据你的实际需求,可以进一步自定义编辑行为和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值