Skip to content

Commit

Permalink
perf: 增加afterRemove判定,delRequest返回false则不执行后面的逻辑
Browse files Browse the repository at this point in the history
Closes #333
  • Loading branch information
greper committed Jan 25, 2024
1 parent 44d127f commit 448b2d8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
20 changes: 20 additions & 0 deletions docs/zh/api/crud-options/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@
* 说明:取消后的操作
* 类型:`(context)=>Promise<any>`

## remove.afterRemove
* 说明:删除请求提交后的操作,返回false,不执行后面的逻辑
* 类型:`(context:{index,row,res})=>Promise<any>`

```ts
{
table:{
remove:{
afterRemove:async (context)=>{
//context参数中带请求返回值(res),你可以在此处处理一些业务逻辑,比如校验是否失败;如果此方法返回false,不执行后面的逻辑,比如弹出删除成功消息
if(context.res.code !== 200){
return false
}
}
}
}
}
```

```
## remove.showSuccessNotification
* 说明:是否显示成功删除提示
Expand Down
8 changes: 7 additions & 1 deletion packages/fast-crud/src/d/crud.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, Ref, ShallowRef } from "vue";
import { ComputeContext } from "./compute";
import { CrudExpose, DoRemoveContext } from "../d/expose";
import { CrudExpose, DoRemoveContext, OnAfterRemoveContext } from "../d/expose";

import { RuleItem } from "async-validator";
import { UiSlot, UiSlotRet } from "@fast-crud/ui-interface";
Expand Down Expand Up @@ -457,6 +457,12 @@ export type RemoveProps = {
*/
refreshTable?: boolean;

/**
* 删除请求之后的操作,如果返回false,终止后续的执行,比如显示删除成功通知等
* @param context
*/
afterRemove?: (context: OnAfterRemoveContext) => Promise<any>;

/**
* 显示成功提示
*/
Expand Down
1 change: 1 addition & 0 deletions packages/fast-crud/src/d/expose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export type SetSearchFormDataProps = { form: any; mergeForm?: boolean; triggerSe
* crudExpose.doRemove参数
*/
export type DoRemoveContext = { index?: number; row?: any };
export type OnAfterRemoveContext = DoRemoveContext & { res: any };
/**
* crudExpose.doSearch参数
*/
Expand Down
18 changes: 13 additions & 5 deletions packages/fast-crud/src/use/use-expose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ function useEditable(props: UseEditableProps) {
} else {
if (crudBinding.value.mode.name === "local") {
editable.removeRow(editableId);
return { isLocal: true };
} else {
await crudBinding.value.request.delRequest({ row: rowData });
return res;
}
}
}
Expand Down Expand Up @@ -652,17 +653,24 @@ export function useExpose(props: UseExposeProps): UseExposeRet {
let res = null;
const isLocal = crudBinding.value.mode?.name === "local";
if (opts?.handle) {
const ctn = await opts.handle(context);
if (ctn === false) {
return;
}
res = await opts.handle(context);
} else {
if (isLocal) {
crudExpose.removeTableRow(context?.index);
} else {
res = await crudBinding.value.request.delRequest(context);
}
}
if (res === false) {
return;
}
const removeScope = { ...context, res };
if (removeBinding.afterRemove) {
const success = await removeBinding.afterRemove(removeScope);
if (success === false) {
return false;
}
}

if (removeBinding.showSuccessNotification !== false) {
ui.notification.success(t("fs.rowHandle.remove.success"));
Expand Down

0 comments on commit 448b2d8

Please sign in to comment.