Skip to content

Commit

Permalink
perf: 增加formWatch示例
Browse files Browse the repository at this point in the history
  • Loading branch information
greper committed Dec 6, 2023
1 parent a4c31e2 commit eac2c14
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/router/source/modules/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ export const crudResources = [
name: "FormView",
path: "/crud/form/view",
component: "/crud/form/view/index.vue"
},
{
title: "表单Watch",
name: "FormWatch",
path: "/crud/form/watch",
component: "/crud/form/watch/index.vue"
}
]
},
Expand Down
43 changes: 43 additions & 0 deletions src/views/crud/form/watch/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @ts-ignore
import { requestForMock } from "/src/api/service";
const request = requestForMock;
const apiPrefix = "/mock/FormWatch";
export function GetList(query: any) {
return request({
url: apiPrefix + "/page",
method: "get",
data: query
});
}

export function AddObj(obj: any) {
return request({
url: apiPrefix + "/add",
method: "post",
data: obj
});
}

export function UpdateObj(obj: any) {
return request({
url: apiPrefix + "/update",
method: "post",
data: obj
});
}

export function DelObj(id: any) {
return request({
url: apiPrefix + "/delete",
method: "post",
params: { id }
});
}

export function GetObj(id: any) {
return request({
url: apiPrefix + "/get",
method: "get",
params: { id }
});
}
72 changes: 72 additions & 0 deletions src/views/crud/form/watch/crud.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as api from "./api";
import { AddReq, CreateCrudOptionsRet, DelReq, dict, EditReq, FormScopeContext, UserPageQuery, UserPageRes } from "@fast-crud/fast-crud";

export default function (): CreateCrudOptionsRet {
const pageRequest = async (query: UserPageQuery): Promise<UserPageRes> => {
return await api.GetList(query);
};
const editRequest = async ({ form, row }: EditReq) => {
if (form.id == null) {
form.id = row.id;
}
return await api.UpdateObj(form);
};
const delRequest = async ({ row }: DelReq) => {
return await api.DelObj(row.id);
};

const addRequest = async ({ form }: AddReq) => {
return await api.AddObj(form);
};

return {
crudOptions: {
settings: {
viewFormUseCellComponent: true
},
request: {
pageRequest,
addRequest,
editRequest,
delRequest
},
form: {
initialForm: {
name: "123"
},
watch(context: FormScopeContext) {
const { form } = context;
form.c = form.a + form.b;
}
},
columns: {
name: {
title: "姓名",
type: "text"
},
age: {
title: "年龄",
type: "text"
},
a: {
title: "a",
type: "number"
},
b: {
title: "b",
type: "number"
},
c: {
title: "c",
type: "number",
form: {
component: {
disabled: true
},
helper: "c=a+b,实时计算"
}
}
}
}
};
}
33 changes: 33 additions & 0 deletions src/views/crud/form/watch/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<template>
<fs-page>
<template #header>
<div class="title">
form监听
<span class="sub">监听form数据变化,触发计算操作(表单示例演示,c=a+b)</span>
</div>
</template>
<fs-crud ref="crudRef" v-bind="crudBinding" />
</fs-page>
</template>

<script lang="ts">
import { defineComponent, onMounted } from "vue";
import { useFs } from "@fast-crud/fast-crud";
import createCrudOptions from "./crud";
export default defineComponent({
name: "FormWatch",
setup() {
const { crudBinding, crudRef, crudExpose } = useFs({ createCrudOptions });
// 页面打开后获取列表数据
onMounted(() => {
crudExpose.doRefresh();
});
return {
crudBinding,
crudRef
};
}
});
</script>
29 changes: 29 additions & 0 deletions src/views/crud/form/watch/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ts-ignore
import mockUtil from "/src/mock/base";

const options: any = {
name: "FormWatch",
idGenerator: 0
};
const list = [
{
name: "王小虎",
age: 15,
a: 1,
b: 2,
c: null
},
{
name: "王小虎",
age: 15,
a: 1,
b: 3,
c: null
}
];

options.list = list;
options.copyTimes = 1000;
const mock = mockUtil.buildMock(options);

export default mock;

0 comments on commit eac2c14

Please sign in to comment.