<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180">
<template #default="scope">
<el-input v-model="scope.row.date" size="small"></el-input>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="180">
<template #default="scope">
<el-input v-model="scope.row.name" size="small"></el-input>
</template>
</el-table-column>
<el-table-column prop="address" label="地址">
<template #default="scope">
<el-input v-model="scope.row.address" size="small"></el-input>
</template>
</el-table-column>
</el-table>
</template>
<script setup>
import { reactive } from 'vue';
const tableData = reactive([
{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
},
{
date: '2016-05-04',
name: '张小刚',
address: '上海市普陀区金沙江路 1517 弄'
},
]);
</script>
![]()
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名">
<template #default="{ row }">
<span v-if="!isEditing" @dblclick="editRow(row, 'name')">
{{ row.name }}
<i class="el-icon-edit-outline" @click="editRow(row, 'name')"></i>
</span>
<el-input v-else v-model="row.name" @blur="endEdit(row, 'name')"></el-input>
</template>
</el-table-column>
<!-- 其他列 -->
</el-table>
</template>
<script>
export default {
data() {
return {
isEditing: false,
tableData: [
{ date: '2016-05-02', name: '王小虎' },
// ...其他数据
],
};
},
methods: {
editRow(row, prop) {
this.tableData = this.tableData.map((item) => {
this.isEditing = true;
return item;
});
this.$nextTick(() => {
this.$refs[`input-${prop}`].focus();
});
},
endEdit(row, prop) {
this.tableData = this.tableData.map((item) => {
this.isEditing = false;
return item;
});
},
},
};
</script>