vxe-table removeRows方法加上异步删除

pull/975/head
zhangdaiscott 2023-11-23 22:58:46 +08:00
parent 0e45068cbd
commit 7f6f07f72c
3 changed files with 60 additions and 14 deletions

View File

@ -639,13 +639,45 @@ export function useMethods(props: JVxeTableProps, { emit }, data: JVxeDataProps,
}
/** 删除一行或多行数据 */
async function removeRows(rows) {
async function removeRows(rows, asyncRemove = false) {
// update-begin--author:liaozhiyang---date:20231123---forvxe-table removeRows方法加上异步删除
const xTable = getXTable();
const res = await xTable.remove(rows);
let removeEvent: any = { deleteRows: rows, $table: xTable };
trigger('removed', removeEvent);
await recalcSortNumber();
return res;
const removeEvent: any = { deleteRows: rows, $table: xTable };
if (asyncRemove) {
const selectedRows = Array.isArray(rows) ? rows : [rows];
const deleteOldRows = filterNewRows(selectedRows);
if (deleteOldRows.length) {
return new Promise((resolve) => {
// 确认删除,只有调用这个方法才会真删除
removeEvent.confirmRemove = async () => {
const insertRecords = xTable.getInsertRecords();
selectedRows.forEach((item) => {
// 删除新添加的数据id
if (insertRecords.includes(item)) {
delete item.id;
}
});
const res = await xTable.remove(rows);
await recalcSortNumber();
resolve(res);
};
trigger('removed', removeEvent);
});
} else {
// 全新的行立马删除,不等待。
const res = await xTable.remove(rows);
removeEvent.confirmRemove = () => {};
trigger('removed', removeEvent);
await recalcSortNumber();
return res;
}
} else {
const res = await xTable.remove(rows);
trigger('removed', removeEvent);
await recalcSortNumber();
return res;
}
// update-end--author:liaozhiyang---date:20231123---forvxe-table removeRows方法加上异步删除
}
/** 根据id删除一行或多行 */
@ -781,7 +813,6 @@ export function useMethods(props: JVxeTableProps, { emit }, data: JVxeDataProps,
emit(name, event);
}
/**
* - getSelectionData ID
* onlinePopForm

View File

@ -37,6 +37,10 @@ export function useToolbar(props: JVxeTableProps, data: JVxeDataProps, methods:
// 确认删除,只有调用这个方法才会真删除
removeEvent.confirmRemove = () => methods.removeSelection();
} else {
if (props.asyncRemove) {
// asyncRemove删除的只有新增的数据时防止调用confirmRemove报错
removeEvent.confirmRemove = () => {};
}
methods.removeSelection();
}
methods.trigger('removed', removeEvent);

View File

@ -216,7 +216,7 @@
key: 'action',
type: JVxeTypes.slot,
fixed: 'right',
minWidth: 100,
minWidth: 120,
align: 'center',
slotName: 'myAction',
},
@ -291,12 +291,18 @@
console.log('查看: ', { props });
}
// async function onDeleteRow(props) {
// //
// const res = await tableRef.value?.removeRows(props.row);
// if (res && res.rows.length > 0) {
// createMessage.success('');
// }
// }
async function onDeleteRow(props) {
//
const res = await tableRef.value?.removeRows(props.row);
if (res && res.rows.length > 0) {
createMessage.success('删除成功');
}
//
const res = await tableRef.value?.removeRows(props.row, true);
console.log('删除成功~', res);
}
function handleValueChange(event) {
@ -361,8 +367,13 @@
}
function doDelete(deleteRows) {
let rowId;
return new Promise((resolve) => {
let rowId = deleteRows.filter((row) => row.id);
if (Array.isArray(deleteRows)) {
rowId = deleteRows.filter((row) => row.id);
} else {
rowId = deleteRows.id;
}
console.log('删除 rowId: ', rowId);
setTimeout(() => resolve(true), 1500);
});