Commit 34781d42e8da39e7656d02fb02d6220be6d0a3fa
1 parent
456a6614
feat(Table): 支持设置默认的排序值
Showing
5 changed files
with
23 additions
and
7 deletions
src/components/Table/src/hooks/useDataSource.ts
@@ -241,7 +241,7 @@ export function useDataSource( | @@ -241,7 +241,7 @@ export function useDataSource( | ||
241 | } | 241 | } |
242 | 242 | ||
243 | async function fetch(opt?: FetchParams) { | 243 | async function fetch(opt?: FetchParams) { |
244 | - const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } = | 244 | + const { api, searchInfo, defSort, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } = |
245 | unref(propsRef); | 245 | unref(propsRef); |
246 | if (!api || !isFunction(api)) return; | 246 | if (!api || !isFunction(api)) return; |
247 | try { | 247 | try { |
@@ -269,6 +269,7 @@ export function useDataSource( | @@ -269,6 +269,7 @@ export function useDataSource( | ||
269 | ...(useSearchForm ? getFieldsValue() : {}), | 269 | ...(useSearchForm ? getFieldsValue() : {}), |
270 | ...searchInfo, | 270 | ...searchInfo, |
271 | ...(opt?.searchInfo ?? {}), | 271 | ...(opt?.searchInfo ?? {}), |
272 | + ...defSort, | ||
272 | ...sortInfo, | 273 | ...sortInfo, |
273 | ...filterInfo, | 274 | ...filterInfo, |
274 | ...(opt?.sortInfo ?? {}), | 275 | ...(opt?.sortInfo ?? {}), |
src/components/Table/src/props.ts
@@ -69,6 +69,11 @@ export const basicProps = { | @@ -69,6 +69,11 @@ export const basicProps = { | ||
69 | type: Object as PropType<Recordable>, | 69 | type: Object as PropType<Recordable>, |
70 | default: null, | 70 | default: null, |
71 | }, | 71 | }, |
72 | + // 默认的排序参数 | ||
73 | + defSort: { | ||
74 | + type: Object as PropType<Recordable>, | ||
75 | + default: null, | ||
76 | + }, | ||
72 | // 使用搜索表单 | 77 | // 使用搜索表单 |
73 | useSearchForm: propTypes.bool, | 78 | useSearchForm: propTypes.bool, |
74 | // 表单配置 | 79 | // 表单配置 |
src/components/Table/src/types/table.ts
@@ -176,6 +176,8 @@ export interface BasicTableProps<T = any> { | @@ -176,6 +176,8 @@ export interface BasicTableProps<T = any> { | ||
176 | emptyDataIsShowTable?: boolean; | 176 | emptyDataIsShowTable?: boolean; |
177 | // 额外的请求参数 | 177 | // 额外的请求参数 |
178 | searchInfo?: Recordable; | 178 | searchInfo?: Recordable; |
179 | + // 默认的排序参数 | ||
180 | + defSort?: Recordable; | ||
179 | // 使用搜索表单 | 181 | // 使用搜索表单 |
180 | useSearchForm?: boolean; | 182 | useSearchForm?: boolean; |
181 | // 表单配置 | 183 | // 表单配置 |
src/settings/componentSetting.ts
@@ -24,12 +24,16 @@ export default { | @@ -24,12 +24,16 @@ export default { | ||
24 | // Custom general sort function | 24 | // Custom general sort function |
25 | defaultSortFn: (sortInfo: SorterResult) => { | 25 | defaultSortFn: (sortInfo: SorterResult) => { |
26 | const { field, order } = sortInfo; | 26 | const { field, order } = sortInfo; |
27 | - return { | ||
28 | - // The sort field passed to the backend you | ||
29 | - field, | ||
30 | - // Sorting method passed to the background asc/desc | ||
31 | - order, | ||
32 | - }; | 27 | + if (field && order) { |
28 | + return { | ||
29 | + // The sort field passed to the backend you | ||
30 | + field, | ||
31 | + // Sorting method passed to the background asc/desc | ||
32 | + order, | ||
33 | + }; | ||
34 | + } else { | ||
35 | + return {}; | ||
36 | + } | ||
33 | }, | 37 | }, |
34 | // Custom general filter function | 38 | // Custom general filter function |
35 | defaultFilterFn: (data: Partial<Recordable<string[]>>) => { | 39 | defaultFilterFn: (data: Partial<Recordable<string[]>>) => { |
src/views/demo/table/UseTable.vue
@@ -54,6 +54,10 @@ | @@ -54,6 +54,10 @@ | ||
54 | titleHelpMessage: '使用useTable调用表格内方法', | 54 | titleHelpMessage: '使用useTable调用表格内方法', |
55 | api: demoListApi, | 55 | api: demoListApi, |
56 | columns: getBasicColumns(), | 56 | columns: getBasicColumns(), |
57 | + defSort: { | ||
58 | + field: 'name', | ||
59 | + order: 'ascend', | ||
60 | + }, | ||
57 | rowKey: 'id', | 61 | rowKey: 'id', |
58 | showTableSetting: true, | 62 | showTableSetting: true, |
59 | onChange, | 63 | onChange, |