Commit 491ba9a3cc19ceb97dd9a6448831b64c86e1e475

Authored by vben
1 parent d09406e3

feat: add the parameter sortFn to the table

CHANGELOG.zh_CN.md
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 4
5 - 面包屑支持显示图标 5 - 面包屑支持显示图标
6 - 新增 tinymce 富文本组件 6 - 新增 tinymce 富文本组件
  7 +- 表单新增 submitOnReset 控制是否在重置时重新发起请求
7 8
8 ### 🎫 Chores 9 ### 🎫 Chores
9 10
@@ -21,6 +22,7 @@ @@ -21,6 +22,7 @@
21 - 修复菜单没有子节点时显示折叠的问题 22 - 修复菜单没有子节点时显示折叠的问题
22 - 修复面包屑显示样式问题 23 - 修复面包屑显示样式问题
23 - 修复 modal 在 destroyOnClose=true 时多次打开拖拽失效 24 - 修复 modal 在 destroyOnClose=true 时多次打开拖拽失效
  25 +- 修复表格出现多个 action 列
24 26
25 # 2.0.0-rc.4 (2020-10-21) 27 # 2.0.0-rc.4 (2020-10-21)
26 28
src/components/Table/src/BasicTable.vue
@@ -64,7 +64,7 @@ @@ -64,7 +64,7 @@
64 import { ROW_KEY } from './const'; 64 import { ROW_KEY } from './const';
65 import { PaginationProps } from './types/pagination'; 65 import { PaginationProps } from './types/pagination';
66 import { deepMerge } from '/@/utils'; 66 import { deepMerge } from '/@/utils';
67 - import { TableCustomRecord } from 'ant-design-vue/types/table/table'; 67 + import { SorterResult, TableCustomRecord } from 'ant-design-vue/types/table/table';
68 import { useEvent } from '/@/hooks/event/useEvent'; 68 import { useEvent } from '/@/hooks/event/useEvent';
69 69
70 import './style/index.less'; 70 import './style/index.less';
@@ -216,12 +216,22 @@ @@ -216,12 +216,22 @@
216 fetch({ searchInfo: info, page: 1 }); 216 fetch({ searchInfo: info, page: 1 });
217 } 217 }
218 218
219 - function handleTableChange(pagination: PaginationProps) {  
220 - const { clearSelectOnPageChange } = unref(getMergeProps); 219 + function handleTableChange(
  220 + pagination: PaginationProps,
  221 + filters: Partial<Record<string, string[]>>,
  222 + sorter: SorterResult<any>
  223 + ) {
  224 + const { clearSelectOnPageChange, sortFn } = unref(getMergeProps);
221 if (clearSelectOnPageChange) { 225 if (clearSelectOnPageChange) {
222 clearSelectedRowKeys(); 226 clearSelectedRowKeys();
223 } 227 }
224 setPagination(pagination); 228 setPagination(pagination);
  229 +
  230 + if (sorter && isFunction(sortFn)) {
  231 + const sortInfo = sortFn(sorter);
  232 + fetch({ sortInfo });
  233 + return;
  234 + }
225 fetch(); 235 fetch();
226 } 236 }
227 237
src/components/Table/src/const.ts
  1 +import { SorterResult } from 'ant-design-vue/types/table/table';
  2 +
1 export const ROW_KEY = 'key'; 3 export const ROW_KEY = 'key';
2 4
3 export const PAGE_SIZE_OPTIONS = ['10', '50', '80', '100']; 5 export const PAGE_SIZE_OPTIONS = ['10', '50', '80', '100'];
@@ -10,3 +12,11 @@ export const FETCH_SETTING = { @@ -10,3 +12,11 @@ export const FETCH_SETTING = {
10 listField: 'items', 12 listField: 'items',
11 totalField: 'total', 13 totalField: 'total',
12 }; 14 };
  15 +
  16 +export function DEFAULT_SORT_FN(sortInfo: SorterResult<any>) {
  17 + const { field, order } = sortInfo;
  18 + return {
  19 + field,
  20 + order,
  21 + };
  22 +}
src/components/Table/src/hooks/useDataSource.ts
@@ -98,6 +98,8 @@ export function useDataSource( @@ -98,6 +98,8 @@ export function useDataSource(
98 ...(useSearchForm ? getFieldsValue() : {}), 98 ...(useSearchForm ? getFieldsValue() : {}),
99 ...searchInfo, 99 ...searchInfo,
100 ...(opt ? opt.searchInfo : {}), 100 ...(opt ? opt.searchInfo : {}),
  101 + ...(opt ? opt.sortInfo : {}),
  102 + ...(opt ? opt.filterInfo : {}),
101 }; 103 };
102 if (beforeFetch && isFunction(beforeFetch)) { 104 if (beforeFetch && isFunction(beforeFetch)) {
103 params = beforeFetch(params) || params; 105 params = beforeFetch(params) || params;
src/components/Table/src/props.ts
1 import type { PropType } from 'vue'; 1 import type { PropType } from 'vue';
2 import type { PaginationProps } from './types/pagination'; 2 import type { PaginationProps } from './types/pagination';
3 import type { BasicColumn, FetchSetting, TableSetting } from './types/table'; 3 import type { BasicColumn, FetchSetting, TableSetting } from './types/table';
4 -import type { TableCustomRecord, TableRowSelection } from 'ant-design-vue/types/table/table'; 4 +import type {
  5 + SorterResult,
  6 + TableCustomRecord,
  7 + TableRowSelection,
  8 +} from 'ant-design-vue/types/table/table';
5 import type { FormProps } from '/@/components/Form/index'; 9 import type { FormProps } from '/@/components/Form/index';
6 -import { FETCH_SETTING } from './const'; 10 +import { DEFAULT_SORT_FN, FETCH_SETTING } from './const';
7 11
8 // 注释看 types/table 12 // 注释看 types/table
9 export const basicProps = { 13 export const basicProps = {
10 tableSetting: { 14 tableSetting: {
11 type: Object as PropType<TableSetting>, 15 type: Object as PropType<TableSetting>,
12 }, 16 },
  17 +
  18 + sortFn: {
  19 + type: Function as PropType<(sortInfo: SorterResult<any>) => any>,
  20 + default: DEFAULT_SORT_FN,
  21 + },
  22 +
13 showTableSetting: { 23 showTableSetting: {
14 type: Boolean as PropType<boolean>, 24 type: Boolean as PropType<boolean>,
15 default: false, 25 default: false,
src/components/Table/src/types/table.ts
@@ -28,6 +28,8 @@ export interface RenderEditableCellParams { @@ -28,6 +28,8 @@ export interface RenderEditableCellParams {
28 export interface FetchParams { 28 export interface FetchParams {
29 searchInfo?: any; 29 searchInfo?: any;
30 page?: number; 30 page?: number;
  31 + sortInfo?: any;
  32 + filterInfo?: any;
31 } 33 }
32 34
33 export interface GetColumnsParams { 35 export interface GetColumnsParams {
@@ -75,6 +77,8 @@ export interface TableSetting { @@ -75,6 +77,8 @@ export interface TableSetting {
75 } 77 }
76 78
77 export interface BasicTableProps<T = any> { 79 export interface BasicTableProps<T = any> {
  80 + // 自定义排序方法
  81 + sortFn?: (sortInfo: SorterResult<any>) => any;
78 // 显示表格设置 82 // 显示表格设置
79 showTableSetting?: boolean; 83 showTableSetting?: boolean;
80 tableSetting?: TableSetting; 84 tableSetting?: TableSetting;
@@ -106,7 +110,6 @@ export interface BasicTableProps&lt;T = any&gt; { @@ -106,7 +110,6 @@ export interface BasicTableProps&lt;T = any&gt; {
106 emptyDataIsShowTable?: boolean; 110 emptyDataIsShowTable?: boolean;
107 // 额外的请求参数 111 // 额外的请求参数
108 searchInfo?: any; 112 searchInfo?: any;
109 -  
110 // 使用搜索表单 113 // 使用搜索表单
111 useSearchForm?: boolean; 114 useSearchForm?: boolean;
112 // 表单配置 115 // 表单配置