feat: 表格组件编辑项失去焦点后恢复初始值

This commit is contained in:
RubyLiu 2023-09-22 11:25:41 +08:00 committed by 刘瑞斌
parent 2f54200f68
commit a55c01c9c8
1 changed files with 27 additions and 13 deletions

View File

@ -98,8 +98,8 @@
"
ref="currentInputRef"
v-model="record[item.dataIndex as string]"
@blur="handleEditInputBlur()"
@press-enter="handleEditInputEnter(record)"
@blur="handleEditInputBlur(record, item.dataIndex as string, true)"
@keydown.enter="handleEditInputBlur(record, item.dataIndex as string, false)"
/>
<a-tooltip v-else placement="top" :content="String(record[item.dataIndex as string])">
<div class="one-line-text max-w-[300px]">
@ -119,7 +119,7 @@
class="ml-2 cursor-pointer"
:class="{ 'ms-table-edit-active': editActiveKey === rowIndex }"
type="icon-icon_edit_outlined"
@click="handleEdit(item.dataIndex as string, rowIndex)"
@click="handleEdit(item.dataIndex as string, rowIndex, record)"
/>
</div>
<div>
@ -238,8 +238,13 @@
// Active
const editActiveKey = ref<string>('');
// inputRef
// Ref
const currentInputRef = ref();
// blur
const currentEditValue = ref<string>('');
// enter
const isEnter = ref<boolean>(false);
const { rowKey }: Partial<MsTableProps<any>> = attrs;
//
const currentSpanMethod = ({
@ -312,14 +317,22 @@
emit('batchAction', value, queryParams);
};
const handleEditInputEnter = (record: TableData) => {
editActiveKey.value = '';
emit('rowNameChange', record);
};
const handleEditInputBlur = () => {
currentInputRef.value = null;
editActiveKey.value = '';
const handleEditInputBlur = (record: TableData, dataIndex: string, isBlur: boolean) => {
if (isBlur) {
if (!isEnter.value) {
// Enterblur
record[dataIndex] = currentEditValue.value;
}
isEnter.value = false;
currentInputRef.value = null;
editActiveKey.value = '';
currentEditValue.value = '';
} else {
// Enter
emit('rowNameChange', record);
isEnter.value = true;
editActiveKey.value = '';
}
};
// change
@ -341,8 +354,9 @@
};
// input
const handleEdit = (dataIndex: string, rowIndex: number) => {
const handleEdit = (dataIndex: string, rowIndex: number, record: TableData) => {
editActiveKey.value = dataIndex + rowIndex;
currentEditValue.value = record[dataIndex];
if (currentInputRef.value) {
currentInputRef.value[0].focus();
} else {