Commit 456a661488ad46cff9d184828bc4ec4b50ee73ba

Authored by 无木
1 parent 59028867

fix(table): `deleteTableDataRecord` not work

CHANGELOG.zh_CN.md
... ... @@ -12,12 +12,14 @@
12 12 - 修复`useTable`与`BasicTable`实例的`reload`方法`await`表现不一致的问题
13 13 - 修复`clickToRowSelect`会无视行选择框 disabled 状态的问题
14 14 - 修复`BasicTable`在某些情况下,分页会被重置的问题
  15 + - 修改 `deleteTableDataRecord` 方法
15 16 - **BasicModal**
16 17 - 修复点击遮罩、按下`Esc`键都不能关闭`Modal`的问题
17 18 - 修复点击关闭按钮、最大化按钮旁边的空白区域也会导致`Modal`关闭的问题
18 19 - **BasicTree** 修复节点插槽不起作用的问题
19 20 - **CodeEditor** 修复可能会造成的`Build`失败的问题
20 21 - **BasicForm** 修复自定义 FormItem 组件的内容宽度可能超出范围的问题
  22 +- **ApiTreeSelect** 修复`params`变化未能触发重新请求 api 数据的问题
21 23 - **其它**
22 24 - 修复多标签在某些情况下关闭页签不会跳转路由的问题
23 25 - 修复部分组件可能会造成热更新异常的问题
... ...
src/components/Table/src/hooks/useDataSource.ts
... ... @@ -160,21 +160,39 @@ export function useDataSource(
160 160 }
161 161 }
162 162  
163   - function deleteTableDataRecord(record: Recordable | Recordable[]): Recordable | undefined {
  163 + function deleteTableDataRecord(rowKey: string | number | string[] | number[]) {
164 164 if (!dataSourceRef.value || dataSourceRef.value.length == 0) return;
165   - const records = !Array.isArray(record) ? [record] : record;
166   - const recordIndex = records
167   - .map((item) => dataSourceRef.value.findIndex((s) => s.key === item.key)) // 取序号
168   - .filter((item) => item !== undefined)
169   - .sort((a, b) => b - a); // 从大到小排序
170   - for (const index of recordIndex) {
171   - unref(dataSourceRef).splice(index, 1);
172   - unref(propsRef).dataSource?.splice(index, 1);
  165 + const rowKeyName = unref(getRowKey);
  166 + if (!rowKeyName) return;
  167 + const rowKeys = !Array.isArray(rowKey) ? [rowKey] : rowKey;
  168 + for (const key of rowKeys) {
  169 + let index: number | undefined = dataSourceRef.value.findIndex((row) => {
  170 + let targetKeyName: string;
  171 + if (typeof rowKeyName === 'function') {
  172 + targetKeyName = rowKeyName(row);
  173 + } else {
  174 + targetKeyName = rowKeyName as string;
  175 + }
  176 + return row[targetKeyName] === key;
  177 + });
  178 + if (index >= 0) {
  179 + dataSourceRef.value.splice(index, 1);
  180 + }
  181 + index = unref(propsRef).dataSource?.findIndex((row) => {
  182 + let targetKeyName: string;
  183 + if (typeof rowKeyName === 'function') {
  184 + targetKeyName = rowKeyName(row);
  185 + } else {
  186 + targetKeyName = rowKeyName as string;
  187 + }
  188 + return row[targetKeyName] === key;
  189 + });
  190 + if (typeof index !== 'undefined' && index !== -1)
  191 + unref(propsRef).dataSource?.splice(index, 1);
173 192 }
174 193 setPagination({
175 194 total: unref(propsRef).dataSource?.length,
176 195 });
177   - return unref(propsRef).dataSource;
178 196 }
179 197  
180 198 function insertTableDataRecord(record: Recordable, index: number): Recordable | undefined {
... ...
src/components/Table/src/hooks/useTable.ts
... ... @@ -122,8 +122,8 @@ export function useTable(tableProps?: Props): [
122 122 updateTableData: (index: number, key: string, value: any) => {
123 123 return getTableInstance().updateTableData(index, key, value);
124 124 },
125   - deleteTableDataRecord: (record: Recordable | Recordable[]) => {
126   - return getTableInstance().deleteTableDataRecord(record);
  125 + deleteTableDataRecord: (rowKey: string | number | string[] | number[]) => {
  126 + return getTableInstance().deleteTableDataRecord(rowKey);
127 127 },
128 128 insertTableDataRecord: (record: Recordable | Recordable[], index?: number) => {
129 129 return getTableInstance().insertTableDataRecord(record, index);
... ...
src/components/Table/src/types/table.ts
... ... @@ -95,7 +95,7 @@ export interface TableActionType {
95 95 setPagination: (info: Partial<PaginationProps>) => void;
96 96 setTableData: <T = Recordable>(values: T[]) => void;
97 97 updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void;
98   - deleteTableDataRecord: (record: Recordable | Recordable[]) => Recordable | void;
  98 + deleteTableDataRecord: (rowKey: string | number | string[] | number[]) => void;
99 99 insertTableDataRecord: (record: Recordable, index?: number) => Recordable | void;
100 100 findTableDataRecord: (rowKey: string | number) => Recordable | void;
101 101 getColumns: (opt?: GetColumnsParams) => BasicColumn[];
... ...