Skip to content

Commit c2a31d4

Browse files
committed
fix(components): [dynamic-table] initial fetchData did not carry the default value
1 parent 03e7806 commit c2a31d4

File tree

10 files changed

+187
-136
lines changed

10 files changed

+187
-136
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
"@types/lodash-es": "~4.17.6",
6161
"@types/node": "~18.7.14",
6262
"@types/webpack-env": "~1.18.0",
63-
"@typescript-eslint/eslint-plugin": "~5.36.0",
64-
"@typescript-eslint/parser": "~5.36.0",
63+
"@typescript-eslint/eslint-plugin": "~5.36.1",
64+
"@typescript-eslint/parser": "~5.36.1",
6565
"@vue/cli-plugin-babel": "~5.0.8",
6666
"@vue/cli-plugin-eslint": "~5.0.8",
6767
"@vue/cli-plugin-router": "~5.0.8",
@@ -103,7 +103,7 @@
103103
"unplugin-vue-define-options": "~0.7.3",
104104
"vue-cli-plugin-windicss": "~1.1.6",
105105
"vue-eslint-parser": "~9.0.3",
106-
"vue-tsc": "^0.40.4"
106+
"vue-tsc": "^0.40.5"
107107
},
108108
"__npminstall_done": false,
109109
"repository": {

src/components/core/dynamic-table/src/dynamic-table.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
</ToolBar>
2929
<SchemaForm
3030
ref="editTableFormRef"
31-
layout="inline"
3231
no-style
3332
:initial-values="editFormModel"
3433
:show-action-button-group="false"
@@ -38,7 +37,7 @@
3837
<Table
3938
ref="tableRef"
4039
v-bind="getBindValues"
41-
:columns="getColumns"
40+
:columns="innerColumns"
4241
:data-source="tableData"
4342
@change="handleTableChange"
4443
>
@@ -98,7 +97,7 @@
9897
};
9998
10099
// 表格列的配置描述
101-
const { getColumns } = useColumns({
100+
const { innerColumns } = useColumns({
102101
props,
103102
slots,
104103
state: tableState,

src/components/core/dynamic-table/src/hooks/useColumns.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, unref, useSlots } from 'vue';
1+
import { ref, watchEffect, unref, useSlots } from 'vue';
22
import { cloneDeep, isFunction, mergeWith } from 'lodash-es';
33
import { EditableCell } from '../components/';
44
import { ColumnKeyFlag, CustomRenderParams } from '../types/column';
@@ -24,11 +24,12 @@ export type UseTableColumnsContext = {
2424

2525
export const useColumns = ({ state, methods, props, tableAction }: UseTableColumnsContext) => {
2626
const slots = useSlots();
27+
const innerColumns = ref(props.columns);
2728
const { getColumnKey } = methods;
2829
const { getProps } = state;
2930
const { isEditable } = tableAction;
3031

31-
const getColumns = computed<TableColumn<any>[]>(() => {
32+
watchEffect(() => {
3233
const innerProps = { ...unref(getProps) };
3334
const ColumnKeyFlags = Object.keys(ColumnKeyFlag);
3435
const columns = cloneDeep(innerProps!.columns!.filter((n) => !n.hideInTable));
@@ -53,7 +54,7 @@ export const useColumns = ({ state, methods, props, tableAction }: UseTableColum
5354
} as TableColumn);
5455
}
5556

56-
return columns.map((item) => {
57+
innerColumns.value = columns.map((item) => {
5758
const customRender = item.customRender;
5859

5960
const rowKey = props.rowKey as string;
@@ -149,6 +150,6 @@ export const useColumns = ({ state, methods, props, tableAction }: UseTableColum
149150
};
150151

151152
return {
152-
getColumns,
153+
innerColumns,
153154
};
154155
};

src/components/core/dynamic-table/src/hooks/useEditable.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { cloneDeep } from 'lodash-es';
33
import { message } from 'ant-design-vue';
44
import type { DynamicTableProps } from '../dynamic-table';
55
import type { TableState } from './useTableState';
6+
import type { TableColumn } from '@/components/core/dynamic-table/src/types/column';
67

78
type UseTableMethodsContext = {
89
state: TableState;
@@ -40,6 +41,34 @@ export const useEditable = ({ state, props }: UseTableMethodsContext) => {
4041
});
4142
};
4243

44+
/** 获取要编辑的值 */
45+
const getEditValue = (
46+
recordKey: Key,
47+
currentRow?: Recordable,
48+
columns?: TableColumn<Recordable<any>>[],
49+
) => {
50+
// 克隆当前行数据作为临时编辑的表单数据,避免直接修改原数据
51+
const editValue = cloneDeep(
52+
currentRow ?? tableData.value.find((n) => n[String(props.rowKey)] === recordKey),
53+
);
54+
// 用户设置的默认值优先
55+
columns?.forEach((item) => {
56+
const { formItemProps, editFormItemProps } = item;
57+
const field = (item.dataIndex || item.key) as string;
58+
if (
59+
!Object.is(editFormItemProps?.extendSearchFormProps, false) &&
60+
formItemProps &&
61+
Reflect.has(formItemProps, 'defaultValue')
62+
) {
63+
editValue[field] = formItemProps.defaultValue;
64+
}
65+
if (editFormItemProps && Reflect.has(editFormItemProps, 'defaultValue')) {
66+
editValue[field] = editFormItemProps.defaultValue;
67+
}
68+
});
69+
return editValue;
70+
};
71+
4372
/**
4473
* @description 进入编辑行状态
4574
*
@@ -54,20 +83,7 @@ export const useEditable = ({ state, props }: UseTableMethodsContext) => {
5483
message.warn(props.onlyOneLineEditorAlertMessage || '只能同时编辑一行');
5584
return false;
5685
}
57-
// 克隆当前行数据作为临时编辑的表单数据,避免直接修改原数据
58-
const editValue = cloneDeep(
59-
currentRow ?? tableData.value.find((n) => n[String(props.rowKey)] === recordKey),
60-
);
61-
// 用户设置的默认值优先
62-
props.columns.forEach((item) => {
63-
const field = (item.dataIndex || item.key) as string;
64-
if (item.formItemProps && Reflect.has(item.formItemProps, 'defaultValue')) {
65-
editValue[field] = item.formItemProps.defaultValue;
66-
}
67-
if (item.editFormItemProps && Reflect.has(item.editFormItemProps, 'defaultValue')) {
68-
editValue[field] = item.editFormItemProps.defaultValue;
69-
}
70-
});
86+
const editValue = getEditValue(recordKey, currentRow, props.columns);
7187
setEditFormModel(recordKey, editValue);
7288
editableRowKeys.value.add(recordKey);
7389
return true;
@@ -76,23 +92,9 @@ export const useEditable = ({ state, props }: UseTableMethodsContext) => {
7692
/** 进入编辑单元格状态 */
7793
const startCellEditable = (recordKey: Key, dataIndex: Key, currentRow?: Recordable) => {
7894
editableRowKeys.value.clear();
79-
// 克隆当前行数据作为临时编辑的表单数据,避免直接修改原数据
80-
const editValue = cloneDeep(
81-
currentRow ?? tableData.value.find((n) => n[String(props.rowKey)] === recordKey),
82-
);
83-
const targetColumn = props.columns.find((n) => n.dataIndex === dataIndex);
84-
if (targetColumn) {
85-
const field = (targetColumn.dataIndex || targetColumn.key) as string;
86-
if (targetColumn.formItemProps && Reflect.has(targetColumn.formItemProps, 'defaultValue')) {
87-
editValue[field] = targetColumn.formItemProps.defaultValue;
88-
}
89-
if (
90-
targetColumn.editFormItemProps &&
91-
Reflect.has(targetColumn.editFormItemProps, 'defaultValue')
92-
) {
93-
editValue[field] = targetColumn.editFormItemProps.defaultValue;
94-
}
95-
}
95+
const targetColumn = props.columns.filter((n) => n.dataIndex === dataIndex);
96+
const editValue = getEditValue(recordKey, currentRow, targetColumn);
97+
9698
editableCellKeys.value.add(`${recordKey}.${dataIndex}`);
9799
setEditFormModel(recordKey, {
98100
...(getEditFormModel(recordKey) || editValue),

src/components/core/dynamic-table/src/hooks/useTableMethods.tsx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { unref } from 'vue';
1+
import { unref, nextTick } from 'vue';
22
import { isObject, isFunction } from 'lodash-es';
33
import { useEditable } from './useEditable';
44
import type { DynamicTableProps, DynamicTableEmitFn } from '../dynamic-table';
55
import type { OnChangeCallbackParams, TableColumn } from '../types/';
6-
import type { TableState } from './useTableState';
7-
import { isAsyncFunction } from '@/utils/is';
6+
import type { Pagination, TableState } from './useTableState';
7+
import { isAsyncFunction, isBoolean } from '@/utils/is';
88

99
export type TableMethods = ReturnType<typeof useTableMethods>;
1010

@@ -40,11 +40,20 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
4040
* @description 获取表格数据
4141
*/
4242
const fetchData = async (params = {}, rest?: OnChangeCallbackParams) => {
43+
const [pagination] = rest || [];
4344
// 如果用户没有提供dataSource并且dataRequest是一个函数,那就进行接口请求
4445
if (
4546
Object.is(props.dataSource, undefined) &&
4647
(isFunction(props.dataRequest) || isAsyncFunction(props.dataRequest))
4748
) {
49+
await nextTick();
50+
if (queryFormRef.value) {
51+
const values = await queryFormRef.value.validate();
52+
params = {
53+
...queryFormRef.value.handleFormValues(values),
54+
...params,
55+
};
56+
}
4857
const _pagination = unref(paginationRef)!;
4958
// 是否启用了分页
5059
const enablePagination = isObject(_pagination);
@@ -74,11 +83,13 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
7483
}
7584
}
7685

77-
Object.assign(unref(paginationRef), {
78-
current: ~~page,
79-
pageSize: ~~size,
80-
total: ~~total,
86+
updatePagination({
87+
current: page,
88+
pageSize: size,
89+
total,
8190
});
91+
} else {
92+
updatePagination(pagination);
8293
}
8394
if (Array.isArray(data?.list)) {
8495
tableData.value = data!.list;
@@ -87,7 +98,10 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
8798
} else {
8899
tableData.value = [];
89100
}
101+
} else {
102+
updatePagination(pagination);
90103
}
104+
91105
retryFetchCount = 2;
92106
return tableData;
93107
};
@@ -109,13 +123,11 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
109123
const handleTableChange = async (...rest: OnChangeCallbackParams) => {
110124
// const [pagination, filters, sorter] = rest;
111125
const [pagination] = rest;
112-
let params = {};
113126
if (queryFormRef.value) {
114-
const values = await queryFormRef.value.validate();
115-
params = queryFormRef.value.handleFormValues(values);
127+
await queryFormRef.value.validate();
116128
}
117-
Object.assign(unref(paginationRef), pagination || {});
118-
fetchData(params, rest);
129+
updatePagination(pagination);
130+
await fetchData({}, rest);
119131
emit('change', ...rest);
120132
};
121133

@@ -137,6 +149,18 @@ export const useTableMethods = ({ state, props, emit }: UseTableMethodsContext)
137149
}
138150
};
139151

152+
/** 更新表格分页信息 */
153+
const updatePagination = (info: Pagination = paginationRef.value) => {
154+
if (isBoolean(info)) {
155+
paginationRef.value = info;
156+
} else if (isObject(paginationRef.value)) {
157+
paginationRef.value = {
158+
...paginationRef.value,
159+
...info,
160+
};
161+
}
162+
};
163+
140164
/**
141165
* @description当外部需要动态改变搜索表单的值或选项时,需要调用此方法获取dynamicFormRef实例
142166
*/

src/views/demos/tables/edit-row-table/columns.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export const columns: TableColumn<ListItemType>[] = [
2727
return index > 0;
2828
},
2929
formItemProps: {
30+
defaultValue: '李白',
31+
rules: [{ required: true, message: '请输入姓名' }],
32+
},
33+
editFormItemProps: {
34+
/** 不继承于 `formItemProps`的属性 */
35+
extendSearchFormProps: false,
3036
rules: [{ required: true, message: '请输入姓名' }],
3137
},
3238
},

src/views/demos/tables/edit-row-table/index.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@
4545
4646
const [DynamicTable] = useTable();
4747
48-
const editableType = ref<EditableType>('single');
48+
const editableType = ref<EditableType>('cell');
4949
5050
const loadData = async (
5151
params,
5252
onChangeParams: OnChangeCallbackParams,
5353
): Promise<API.TableListResult> => {
54+
console.log('params', params);
5455
console.log('onChangeParams', onChangeParams);
5556
await waitTime(500);
5657

src/views/demos/tables/search-table/columns.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,19 @@ export const columns: TableColumn<ListItemType>[] = [
7171
align: 'center',
7272
dataIndex: 'name',
7373
sorter: true,
74+
width: 300,
75+
resizable: true,
7476
formItemProps: {
77+
defaultValue: '李白',
7578
required: true,
7679
},
7780
},
7881
{
7982
title: '性别',
8083
align: 'center',
8184
dataIndex: 'gender',
85+
width: 120,
86+
resizable: true,
8287
formItemProps: {
8388
component: 'Select',
8489
componentProps: ({ formInstance, formModel, tableInstance }) => ({

src/views/demos/tables/search-table/index.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
<template #description> 查询表格-查询表单使用示例 </template>
55
</Alert>
66
<Card title="查询表单基本使用示例" style="margin-top: 20px">
7-
<DynamicTable size="small" bordered :data-request="loadData" :columns="columns" row-key="id">
7+
<DynamicTable
8+
size="small"
9+
bordered
10+
:data-request="loadData"
11+
:columns="columns"
12+
row-key="id"
13+
@resize-column="handleResizeColumn"
14+
>
815
<template #bodyCell="{ column, record }">
916
<template v-if="column.dataIndex === 'name'">
1017
{{ record.name }} <a class="text-red-500">[测试bodyCell]</a>
@@ -26,6 +33,7 @@
2633
params,
2734
onChangeParams: OnChangeCallbackParams,
2835
): Promise<API.TableListResult> => {
36+
console.log('params', params);
2937
console.log('onChangeParams', onChangeParams);
3038
3139
return new Promise((resolve) => {
@@ -55,6 +63,11 @@
5563
}, 500);
5664
});
5765
};
66+
67+
const handleResizeColumn = (w, col) => {
68+
// console.log('w', w, col);
69+
col.width = w;
70+
};
5871
</script>
5972

6073
<style lang="less" scoped></style>

0 commit comments

Comments
 (0)