Skip to content

Commit

Permalink
fix(dynamic-table): add onChangeParams param for dataRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Apr 13, 2022
1 parent 8b87e06 commit 9c9ec46
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 134 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"qs": "^6.10.3",
"socket.io-client": "4.4.1",
"sortablejs": "^1.15.0",
"vue": "^3.2.31",
"vue": "^3.2.32",
"vue-i18n": "9.2.0-beta.30",
"vue-router": "^4.0.14",
"vue-types": "^4.1.1",
Expand All @@ -56,8 +56,8 @@
"@types/lodash-es": "^4.17.6",
"@types/node": "^17.0.23",
"@types/webpack-env": "^1.16.3",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"@typescript-eslint/eslint-plugin": "^5.19.0",
"@typescript-eslint/parser": "^5.19.0",
"@vue/cli-plugin-babel": "^5.0.4",
"@vue/cli-plugin-eslint": "^5.0.4",
"@vue/cli-plugin-router": "^5.0.4",
Expand All @@ -67,7 +67,7 @@
"babel-plugin-import": "^1.13.3",
"commitizen": "^4.2.4",
"conventional-changelog-cli": "^2.2.2",
"eslint": "^8.12.0",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-prettier": "^4.0.0",
Expand All @@ -77,7 +77,7 @@
"less-loader": "10.2.0",
"lint-staged": "^12.3.7",
"path-browserify": "^1.0.1",
"postcss-html": "^1.3.0",
"postcss-html": "^1.3.1",
"postcss-less": "^6.0.0",
"prettier": "^2.6.2",
"regenerator-runtime": "^0.13.9",
Expand All @@ -92,7 +92,7 @@
"svg-sprite-loader": "^6.0.11",
"typescript": "^4.6.3",
"unplugin-vue-define-options": "^0.6.0",
"vue-cli-plugin-windicss": "^1.1.3",
"vue-cli-plugin-windicss": "^1.1.4",
"vue-eslint-parser": "^8.3.0"
},
"__npminstall_done": false,
Expand Down
4 changes: 2 additions & 2 deletions src/components/core/dynamic-table/src/dynamic-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ export const dynamicTableProps = {
export type DynamicTableProps = ExtractPropTypes<typeof dynamicTableProps>;

export const dynamicTableEmits = {
change: (...rest: Parameters<NonNullable<OnChangeCallbackParams>>) => rest.length === 4,
change: (...rest: OnChangeCallbackParams) => rest.length === 4,
'toggle-advanced': (isAdvanced: boolean) => isBoolean(isAdvanced),
};

export type DynamicTableEmits = typeof dynamicTableEmits;

export type DynamicTableEmitFn = EmitFn<DynamicTableEmits>;
// @ts-ignore:next-line
// @ts-ignore
export type DynamicTableInstance = InstanceType<typeof DynamicTable>;

// 默认支持的插槽
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
* @param {boolean} flush 是否将页数重置到第一页
* @description 获取表格数据
*/
const fetchData = async (params = {}) => {
const fetchData = async (params = {}, rest?: OnChangeCallbackParams) => {
// 如果用户没有提供dataSource并且dataRequest是一个函数,那就进行接口请求
if (
Object.is(props.dataSource, undefined) &&
Expand All @@ -58,7 +58,7 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
}
loadingRef.value = true;
const data = await props
?.dataRequest?.(queryParams)
?.dataRequest?.(queryParams, rest)
.finally(() => (loadingRef.value = false));

if (data?.pagination) {
Expand Down Expand Up @@ -103,13 +103,13 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
/**
* @description 分页改变
*/
const handleTableChange: OnChangeCallbackParams = (...rest) => {
const handleTableChange = (...rest: OnChangeCallbackParams) => {
// const [pagination, filters, sorter] = rest;
const [pagination] = rest;
if (Object.keys(pagination).length) {
Object.assign(unref(paginationRef), pagination);
}
fetchData();
fetchData(pagination, rest);
emit('change', ...rest);
};

Expand Down
8 changes: 6 additions & 2 deletions src/components/core/dynamic-table/src/types/table.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import type { TableProps } from 'ant-design-vue';
import type { TablePaginationConfig } from 'ant-design-vue/es/table';
import type { DynamicTableProps } from '../dynamic-table';

/**
* 加载表格数据的参数
*/
export type LoadDataParams = {
export type LoadDataParams = TablePaginationConfig & {
/** 根据自己业务需求定义页码 */
page?: number;
/** 根据自己业务需求定义页数据条数 */
limit?: number;
};

/** 表格onChange事件回调参数 */
export type OnChangeCallbackParams = TableProps['onChange'];
export type OnChangeCallbackParams = Parameters<NonNullable<TableProps['onChange']>>;

/** 表格onChange事件回调函数 */
export type OnChangeCallback = TableProps['onChange'];

export type TableActionType = {
/** 刷新并清空,页码也会重置,不包括搜索表单 */
Expand Down
1 change: 1 addition & 0 deletions src/views/demos/tables/search-table/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const columns: TableColumn<ListItemType>[] = [
title: '姓名',
align: 'center',
dataIndex: 'name',
sorter: true,
formItemProps: {
required: true,
},
Expand Down
9 changes: 7 additions & 2 deletions src/views/demos/tables/search-table/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
<script lang="ts" setup>
import { Alert, Card } from 'ant-design-vue';
import { columns, tableData } from './columns';
import { useTable } from '@/components/core/dynamic-table';
import { useTable, type OnChangeCallbackParams } from '@/components/core/dynamic-table';
const [DynamicTable, dynamicTableInstance] = useTable();
const loadData = async (params): Promise<API.TableListResult> => {
const loadData = async (
params,
onChangeParams: OnChangeCallbackParams,
): Promise<API.TableListResult> => {
console.log('onChangeParams', onChangeParams);
return new Promise((resolve) => {
setTimeout(() => {
resolve({
Expand Down
Loading

1 comment on commit 9c9ec46

@vercel
Copy link

@vercel vercel bot commented on 9c9ec46 Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vue3-antd-admin – ./

vue3-antd-admin.vercel.app
vue3-antd-admin-git-main-buqiyuan.vercel.app
vue3-antd-admin-buqiyuan.vercel.app

Please sign in to comment.