From 8739ea79d27220cde645ae3c8d6b86cbe998969b Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 15 Dec 2020 14:32:56 +0800 Subject: [PATCH] fix: add view filter to action params --- packages/app/src/api/index.ts | 21 +++++++++++++++++++ .../app/src/components/views/SimpleTable.tsx | 3 ++- packages/app/src/components/views/Table.tsx | 21 ++----------------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index 9573d27cb5..0f5d350879 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -58,6 +58,27 @@ api.resourcer.use(async (ctx: actions.Context, next) => { } await next(); }); + +api.resourcer.use(async (ctx: actions.Context, next) => { + const { resourceName, viewName, filter } = ctx.action.params; + // TODO: 需要补充默认视图的情况 + let view: any; + if (viewName) { + view = await ctx.db.getModel('views').findOne({ + where: { + collection_name: resourceName, + name: viewName, + } + }); + const viewFilter = view.get('filter'); + if (viewFilter) { + const args = [viewFilter, filter].filter(Boolean); + ctx.action.setParam('filter', {and: args}); + console.log(ctx.action.params.filter); + } + } + await next(); +}); api.resourcer.use(associated); api.resourcer.registerActionHandlers({...actions.common, ...actions.associate}); diff --git a/packages/app/src/components/views/SimpleTable.tsx b/packages/app/src/components/views/SimpleTable.tsx index 1f2ca83a08..a4d67187a2 100644 --- a/packages/app/src/components/views/SimpleTable.tsx +++ b/packages/app/src/components/views/SimpleTable.tsx @@ -25,7 +25,7 @@ export function SimpleTable(props: SimpleTableProps) { associatedName, associatedKey, } = props; - const { rowKey = 'id', fields = [], rowViewName, actions = [], paginated = true, defaultPerPage = 10 } = schema; + const { rowKey = 'id', name: viewName, fields = [], rowViewName, actions = [], paginated = true, defaultPerPage = 10 } = schema; const { sourceKey = 'id' } = activeTab.field||{}; const drawerRef = useRef(); const name = associatedName ? `${associatedName}.${resourceName}` : resourceName; @@ -37,6 +37,7 @@ export function SimpleTable(props: SimpleTableProps) { perPage: paginated ? pageSize : -1, sorter, filter, + viewName, }) .then(({data = [], meta = {}}) => { return { diff --git a/packages/app/src/components/views/Table.tsx b/packages/app/src/components/views/Table.tsx index 28093f4877..e4b2cea014 100644 --- a/packages/app/src/components/views/Table.tsx +++ b/packages/app/src/components/views/Table.tsx @@ -6,24 +6,6 @@ import { request, useRequest } from 'umi'; import api from '@/api-client'; import { components, fields2columns } from './SortableTable'; -const columns = [ - { - title: '姓名', - dataIndex: 'name', - key: 'name', - }, - { - title: '年龄', - dataIndex: 'age', - key: 'age', - }, - { - title: '住址', - dataIndex: 'address', - key: 'address', - }, -]; - export interface TableProps { schema?: any; activeTab?: any; @@ -41,7 +23,7 @@ export function Table(props: TableProps) { associatedName, associatedKey, } = props; - const { fields, defaultTabName, rowKey = 'id', actions = [], paginated = true, defaultPerPage = 10 } = schema; + const { name: viewName, fields, defaultTabName, rowKey = 'id', actions = [], paginated = true, defaultPerPage = 10 } = schema; // const { data, mutate } = useRequest(() => api.resource(name).list({ // associatedKey, // })); @@ -55,6 +37,7 @@ export function Table(props: TableProps) { perPage: paginated ? pageSize : -1, sorter, filter, + viewName, // ...args2, }) .then(({data = [], meta = {}}) => {