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 be2188b commit de163f3
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/router/modules/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,22 @@ const formRoutes = {
},
{
meta: {
title: '查看表单使用cell组价',
title: '查看表单使用cell组件',
keepAlive: true,
},
name: 'FormView',
path: 'view',
component: () => import('@/views/crud/form/view/index.vue'),
},
{
meta: {
title: '表单Watch',
keepAlive: true,
},
name: 'FormWatch',
path: 'view',
component: () => import('@/views/crud/form/watch/index.vue'),
},
],
};
const featureRoutes = {
Expand Down
44 changes: 44 additions & 0 deletions src/views/crud/form/watch/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { requestForMock } from '@/utils/http/service';
const request = (req: any) => {
return requestForMock(req);
};
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 },
});
}
80 changes: 80 additions & 0 deletions src/views/crud/form/watch/crud.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as api from './api';
import {
AddReq,
CreateCrudOptionsRet,
DelReq,
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 de163f3

Please sign in to comment.