Skip to content

Commit e4e4f91

Browse files
newer2333kagol
authored andcommitted
feat(table): 支持toggleRowSelection方法,补充说明文档
1 parent 55a5e6b commit e4e4f91

File tree

4 files changed

+99
-55
lines changed

4 files changed

+99
-55
lines changed

packages/devui-vue/devui/table/src/store/index.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { isBoolean } from '../../../shared/utils';
12
import { watch, Ref, ref, computed, unref, ComponentInternalInstance } from 'vue';
23
import { Column, SortMethod, SortDirection } from '../components/column/column-types';
34
import { DefaultRow, ITable } from '../table-types';
45
import { TableStore } from './store-types';
56
import { useExpand } from './use-expand';
67

7-
88
function replaceColumn(array: any[], column: any) {
99
return array.map((item) => {
1010
if (item.id === column.id) {
@@ -72,12 +72,11 @@ function createColumnGenerator() {
7272
insertColumn,
7373
removeColumn,
7474
sortColumn,
75-
updateColumns
75+
updateColumns,
7676
};
7777
}
7878

79-
80-
function createSelection<T>(dataSource: Ref<T[]>, trackBy: (item: T, index: number) => string) {
79+
function createSelection<T>(dataSource: Ref<T[]>, trackBy: (item: T, index?: number | undefined) => string) {
8180
const _checkSet: Ref<Set<string>> = ref(new Set());
8281

8382
const checkRow = (toggle: boolean, row: T, index: number) => {
@@ -88,6 +87,32 @@ function createSelection<T>(dataSource: Ref<T[]>, trackBy: (item: T, index: numb
8887
}
8988
};
9089

90+
const toggleRowSelection = (row: T, checked?: boolean, index?: number) => {
91+
const isIncluded = _checkSet.value.has(trackBy(row, index));
92+
93+
const addRow = () => {
94+
_checkSet.value.add(trackBy(row, index));
95+
};
96+
97+
const deleteRow = () => {
98+
_checkSet.value.delete(trackBy(row, index));
99+
};
100+
101+
if (isBoolean(checked)) {
102+
if (checked && !isIncluded) {
103+
addRow();
104+
} else if (!checked && isIncluded) {
105+
deleteRow();
106+
}
107+
} else {
108+
if (isIncluded) {
109+
deleteRow();
110+
} else {
111+
addRow();
112+
}
113+
}
114+
};
115+
91116
const isRowChecked = (row: T, index: number) => {
92117
return _checkSet.value.has(trackBy(row, index));
93118
};
@@ -140,6 +165,7 @@ function createSelection<T>(dataSource: Ref<T[]>, trackBy: (item: T, index: numb
140165
getCheckedRows,
141166
checkRow,
142167
isRowChecked,
168+
toggleRowSelection,
143169
};
144170
}
145171

@@ -176,7 +202,7 @@ export function createStore<T>(dataSource: Ref<T[]>, table: ITable<DefaultRow>):
176202
const _data: Ref<T[]> = ref([]);
177203
const { _columns, flatColumns, insertColumn, removeColumn, sortColumn, updateColumns } = createColumnGenerator();
178204

179-
const { _checkAll, _checkSet, _halfChecked, getCheckedRows, isRowChecked, checkRow } = createSelection(
205+
const { _checkAll, _checkSet, _halfChecked, getCheckedRows, isRowChecked, checkRow, toggleRowSelection } = createSelection(
180206
_data,
181207
table.props.trackBy as (v: T) => string
182208
);
@@ -218,5 +244,6 @@ export function createStore<T>(dataSource: Ref<T[]>, table: ITable<DefaultRow>):
218244
isRowExpanded,
219245
setExpandRows,
220246
toggleRowExpansion,
247+
toggleRowSelection,
221248
};
222249
}

packages/devui-vue/devui/table/src/store/store-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export interface TableStore<T = Record<string, any>> {
4747
toggleRowExpansion(row: T): void;
4848
isRowExpanded(row: T): boolean;
4949
setExpandRows: (rowKeys: string[]) => void;
50+
// 设置行选中状态
51+
toggleRowSelection: (row: T) => void;
5052
}
5153

5254
export interface UseExpand {

packages/devui-vue/devui/table/src/table-types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ export const tableProps = {
9696
type: Array as PropType<string[]>,
9797
},
9898
trackBy: {
99-
type: Function as PropType<(v: Record<string, any>, index: number) => string>,
100-
default(v: Record<string, any>, index: number): string {
99+
type: Function as PropType<(v: Record<string, any>, index?: number | undefined) => string>,
100+
default(v: Record<string, any>, index?: number | undefined): string {
101101
return `${index}`;
102-
}
103-
}
102+
},
103+
},
104104
};
105105

106106
export type TableProps = ExtractPropTypes<typeof tableProps>;

packages/devui-vue/docs/components/table/index.md

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,15 @@ export default defineComponent({
216216

217217
### 表格交互
218218

219-
:::demo 通过添加一个`d-column`并且设置`type`属性为`checkable`即可实现表格的多选功能。`getCheckedRows`方法可以获取已选择的列表。通过`cell-click`事件监听单元格点击,事件回调参数包含行索引、列索引、行数据、列数据。在列上配置`resizeable`属性,可实现该列拖动改变宽度,`min-width``max-width`设置可拖动范围,事件`resize-start``resizing``resize-end`分别在拖动开始时、进行中、结束后触发。
219+
:::demo 通过添加一个`d-column`并且设置`type`属性为`checkable`即可实现表格的多选功能。`getCheckedRows`方法可以获取已选择的列表。`toggleRowSelection`方法可以切换某一行的选中状态。通过`cell-click`事件监听单元格点击,事件回调参数包含行索引、列索引、行数据、列数据。在列上配置`resizeable`属性,可实现该列拖动改变宽度,`min-width``max-width`设置可拖动范围,事件`resize-start``resizing``resize-end`分别在拖动开始时、进行中、结束后触发。
220220

221221
```vue
222222
<template>
223223
<div>
224224
<d-button @click="handleClick" class="mr-m mb-m">Get CheckedRows</d-button>
225225
<d-button @click="insertRow" class="mr-m mb-m">Insert Row</d-button>
226226
<d-button @click="deleteRow" class="mr-m mb-m">Delete Row</d-button>
227+
<d-button @click="toggleRow" class="mr-m mb-m">Toggle Row</d-button>
227228
<d-table
228229
ref="tableRef"
229230
:data="data"
@@ -307,6 +308,10 @@ export default defineComponent({
307308
console.log('checked:', checked, selection);
308309
};
309310
311+
const toggleRow = () => {
312+
tableRef.value.store.toggleRowSelection(data.value[0]);
313+
};
314+
310315
const checkable = (row, rowIndex) => {
311316
return row.lastName === 'Li' || false;
312317
};
@@ -351,6 +356,7 @@ export default defineComponent({
351356
onResizeStart,
352357
onResizing,
353358
onResizeEnd,
359+
toggleRow,
354360
};
355361
},
356362
});
@@ -1052,27 +1058,30 @@ export default defineComponent({
10521058

10531059
```vue
10541060
<template>
1055-
<d-table
1056-
ref="tableRef"
1057-
:data="dataSource"
1058-
:trackBy="(item) => item?.firstName"
1059-
row-key="id"
1060-
:expand-row-keys="['1', '3']"
1061-
@expand-change="expandChange"
1062-
>
1063-
<d-column type="expand">
1064-
<template #default="rowData">
1065-
<div>First Name: {{ rowData.row.firstName }}</div>
1066-
<div>Last Name: {{ rowData.row.lastName }}</div>
1067-
<div>Gender: {{ rowData.row.gender }}</div>
1068-
<div>Date of birth: {{ rowData.row.date }}</div>
1069-
</template>
1070-
</d-column>
1071-
<d-column field="firstName" header="First Name"></d-column>
1072-
<d-column field="lastName" header="Last Name"></d-column>
1073-
<d-column field="gender" header="Gender"></d-column>
1074-
<d-column field="date" header="Date of birth"></d-column>
1075-
</d-table>
1061+
<div>
1062+
<d-button @click="toggleRowExpansion" class="mr-m mb-m">toggleRowExpansion</d-button>
1063+
<d-table
1064+
ref="tableRef"
1065+
:data="dataSource"
1066+
:trackBy="(item) => item?.firstName"
1067+
row-key="id"
1068+
:expand-row-keys="['1', '3']"
1069+
@expand-change="expandChange"
1070+
>
1071+
<d-column type="expand">
1072+
<template #default="rowData">
1073+
<div>First Name: {{ rowData.row.firstName }}</div>
1074+
<div>Last Name: {{ rowData.row.lastName }}</div>
1075+
<div>Gender: {{ rowData.row.gender }}</div>
1076+
<div>Date of birth: {{ rowData.row.date }}</div>
1077+
</template>
1078+
</d-column>
1079+
<d-column field="firstName" header="First Name"></d-column>
1080+
<d-column field="lastName" header="Last Name"></d-column>
1081+
<d-column field="gender" header="Gender"></d-column>
1082+
<d-column field="date" header="Date of birth"></d-column>
1083+
</d-table>
1084+
</div>
10761085
</template>
10771086
10781087
<script>
@@ -1122,7 +1131,11 @@ export default defineComponent({
11221131
console.log('expandedRows', expandedRows);
11231132
};
11241133
1125-
return { dataSource, expandChange, tableRef };
1134+
const toggleRowExpansion = () => {
1135+
tableRef.value.store.toggleRowExpansion(dataSource.value[0]);
1136+
};
1137+
1138+
return { dataSource, expandChange, tableRef, toggleRowExpansion };
11261139
},
11271140
});
11281141
</script>
@@ -1132,26 +1145,26 @@ export default defineComponent({
11321145

11331146
### Table 参数
11341147

1135-
| 参数名 | 类型 | 默认值 | 说明 | 跳转 Demo |
1136-
| :-------------------- | :------------------------ | :-------- | :------------------------------------------------------------------------- | :------------------------ |
1137-
| data | `array` | [] | 可选,显示的数据 | [基本用法](#基本用法) |
1138-
| trackBy | `Function(item, index: number): string` | (item, index) => \`${index}\` | 可选,用于获取该行数据的特定标记 | [表格交互](#表格交互) |
1139-
| striped | `boolean` | false | 可选,是否显示斑马纹间隔 | [表格样式](#表格样式) |
1140-
| size | [TableSize](#tablesize) | 'sm' | 可选,表格大小,分别对应行高 40px,48px,56px | [表格样式](#表格样式) |
1141-
| max-width | `string` | -- | 可选,表格最大宽度 |
1142-
| max-height | `boolean` | -- | 可选,表格最大高度 |
1143-
| table-width | `string` | -- | 可选,表格宽度 |
1144-
| table-height | `string` | -- | 可选,表格高度 |
1145-
| row-hovered-highlight | `boolean` | true | 可选,鼠标在该行上时,高亮该行 | [表格样式](#表格样式) |
1146-
| fix-header | `boolean` | false | 可选,固定头部 | [固定表头](#固定表头) |
1147-
| show-loading | `boolean` | false | 可选,显示加载动画 | [空数据模板](#空数据模板) |
1148-
| header-bg | `boolean` | false | 可选,头部背景 | [表格样式](#表格样式) |
1149-
| table-layout | `'fixed' \| 'auto'` | 'fixed' | 可选,表格布局,可选值为'auto' | [表格样式](#表格样式) |
1150-
| span-method | [SpanMethod](#spanmethod) | -- | 可选,合并单元格的计算方法 | [合并单元格](#合并单元格) |
1151-
| border-type | [BorderType](#bordertype) | '' | 可选,表格边框类型,默认有行边框;`bordered`: 全边框;`borderless`: 无边框 | [表格样式](#表格样式) |
1152-
| empty | `string` | 'No Data' | 可选,配置未传递表格数据时需要显示的空数据文本 | [空数据模板](#空数据模板) |
1153-
| show-header | `boolean` | true | 可选,配置是否显示表头 | [表格样式](#表格样式) |
1154-
| row-key | `string` | -- | 可选,行数据的 Key,用来优化 Table 渲染 | |
1148+
| 参数名 | 类型 | 默认值 | 说明 | 跳转 Demo |
1149+
| :-------------------- | :-------------------------------------- | :---------------------------- | :------------------------------------------------------------------------- | :------------------------ |
1150+
| data | `array` | [] | 可选,显示的数据 | [基本用法](#基本用法) |
1151+
| trackBy | `Function(item, index: number): string` | (item, index) => \`${index}\` | 可选,用于获取该行数据的特定标记 | [表格交互](#表格交互) |
1152+
| striped | `boolean` | false | 可选,是否显示斑马纹间隔 | [表格样式](#表格样式) |
1153+
| size | [TableSize](#tablesize) | 'sm' | 可选,表格大小,分别对应行高 40px,48px,56px | [表格样式](#表格样式) |
1154+
| max-width | `string` | -- | 可选,表格最大宽度 |
1155+
| max-height | `boolean` | -- | 可选,表格最大高度 |
1156+
| table-width | `string` | -- | 可选,表格宽度 |
1157+
| table-height | `string` | -- | 可选,表格高度 |
1158+
| row-hovered-highlight | `boolean` | true | 可选,鼠标在该行上时,高亮该行 | [表格样式](#表格样式) |
1159+
| fix-header | `boolean` | false | 可选,固定头部 | [固定表头](#固定表头) |
1160+
| show-loading | `boolean` | false | 可选,显示加载动画 | [空数据模板](#空数据模板) |
1161+
| header-bg | `boolean` | false | 可选,头部背景 | [表格样式](#表格样式) |
1162+
| table-layout | `'fixed' \| 'auto'` | 'fixed' | 可选,表格布局,可选值为'auto' | [表格样式](#表格样式) |
1163+
| span-method | [SpanMethod](#spanmethod) | -- | 可选,合并单元格的计算方法 | [合并单元格](#合并单元格) |
1164+
| border-type | [BorderType](#bordertype) | '' | 可选,表格边框类型,默认有行边框;`bordered`: 全边框;`borderless`: 无边框 | [表格样式](#表格样式) |
1165+
| empty | `string` | 'No Data' | 可选,配置未传递表格数据时需要显示的空数据文本 | [空数据模板](#空数据模板) |
1166+
| show-header | `boolean` | true | 可选,配置是否显示表头 | [表格样式](#表格样式) |
1167+
| row-key | `string` | -- | 可选,行数据的 Key,用来优化 Table 渲染 | |
11551168

11561169
### Table 事件
11571170

@@ -1165,9 +1178,11 @@ export default defineComponent({
11651178

11661179
### Table 方法
11671180

1168-
| 方法名 | 类型 | 说明 |
1169-
| :------------- | :--------- | :------------------- |
1170-
| getCheckedRows | `() => []` | 获取当前选中的行数据 |
1181+
| 方法名 | 类型 | 说明 |
1182+
| :----------------- | :------------------------ | :--------------------------------------------------------------------------------------------------------------- |
1183+
| getCheckedRows | `() => []` | 获取当前选中的行数据 |
1184+
| toggleRowExpansion | `(row, expended) => void` | 用于可展开的表格,切换某一行的展开状态。 如果使用了第二个参数,则是设置这一行是否展示(expended 为 true 则展开) |
1185+
| toggleRowSelection | `(row, checked) => void` | 用于可选择的表格,切换某一行的选中状态。 如果使用了第二个参数,则是设置这一行是否选中(checked 为 true 则选中) |
11711186

11721187
### Table 插槽
11731188

0 commit comments

Comments
 (0)