Skip to content

Commit 42ae217

Browse files
authored
Merge pull request #4845 from VisActor/fix/header-group-filterPlugin
Fix/header group filter plugin
2 parents b9c3b1c + f3d2ea8 commit 42ae217

4 files changed

Lines changed: 125 additions & 4 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import * as VTable from '@visactor/vtable';
2+
import { bindDebugTool } from '@visactor/vtable/es/scenegraph/debug-tool';
3+
import { FilterPlugin } from '../../src/filter';
4+
const CONTAINER_ID = 'vTable';
5+
6+
export function createTable() {
7+
const records = [
8+
{
9+
id: 1,
10+
name: 'name.1',
11+
name_1: 'name_1.1',
12+
name_2: 'name_2.1',
13+
name_2_1: 'name_2_1.1',
14+
name_2_2: 'name_2_2.1'
15+
},
16+
{
17+
id: 2,
18+
name: 'name.2',
19+
name_1: 'name_1.2',
20+
name_2: 'name_2.2',
21+
name_2_1: 'name_2_1.2',
22+
name_2_2: 'name_2_2.2'
23+
},
24+
{
25+
id: 3,
26+
name: 'name.3',
27+
name_1: 'name_1.3',
28+
name_2: 'name_2.3',
29+
name_2_1: 'name_2_1.3',
30+
name_2_2: 'name_2_2.3'
31+
},
32+
{
33+
id: 4,
34+
name: 'name.4',
35+
name_1: 'name_1.4',
36+
name_2: 'name_2.4',
37+
name_2_1: 'name_2_1.4',
38+
name_2_2: 'name_2_2.4'
39+
},
40+
{
41+
id: 5,
42+
name: 'name.5',
43+
name_1: 'name_1.5',
44+
name_2: 'name_2.5',
45+
name_2_1: 'name_2_1.5',
46+
name_2_2: 'name_2_2.5'
47+
}
48+
];
49+
50+
const columns = [
51+
{
52+
field: 'id',
53+
title: 'ID',
54+
width: 100
55+
},
56+
{
57+
field: 'name',
58+
title: 'Name',
59+
columns: [
60+
{
61+
field: 'name_1',
62+
title: 'Name_1',
63+
width: 120
64+
},
65+
{
66+
field: 'name_2',
67+
title: 'Name_2',
68+
width: 150,
69+
columns: [
70+
{
71+
field: 'name_2_1',
72+
title: 'Name_2_1',
73+
width: 150
74+
},
75+
{
76+
field: 'name_2_2',
77+
title: 'Name_2_2',
78+
width: 150
79+
}
80+
]
81+
}
82+
]
83+
}
84+
];
85+
86+
const filterPlugin = new FilterPlugin({});
87+
(window as any).filterPlugin = filterPlugin;
88+
89+
const option = {
90+
records,
91+
columns,
92+
headerHierarchyType: 'grid-tree', // 启用树形折叠
93+
headerExpandLevel: 2, // 默认展开至第二级
94+
widthMode: 'standard',
95+
defaultRowHeight: 40,
96+
plugins: [filterPlugin]
97+
};
98+
99+
const tableInstance = new VTable.ListTable(document.getElementById(CONTAINER_ID), option);
100+
101+
(window as any).tableInstance = tableInstance;
102+
103+
bindDebugTool(tableInstance.scenegraph.stage, {
104+
customGrapicKeys: ['col', 'row']
105+
});
106+
}

packages/vtable-plugins/demo/menu.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export const menus = [
2323
path: 'filter',
2424
name: 'value-filter'
2525
},
26+
{
27+
path: 'filter',
28+
name: 'group'
29+
},
2630
{
2731
path: 'header-highlight',
2832
name: '(deprecated)header-highlight'

packages/vtable-plugins/src/filter/filter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,18 @@ export class FilterPlugin implements pluginsDefinition.IVTablePlugin {
243243

244244
const compactIcons = (list: any[]) => (list.length === 0 ? undefined : list.length === 1 ? list[0] : list);
245245

246-
columns.forEach(column => {
246+
const stack = [...columns];
247+
const subColumns = [];
248+
while (stack.length > 0) {
249+
const currentCol = stack.pop();
250+
if (currentCol.columns) {
251+
currentCol.columns.forEach(col => stack.push(col));
252+
} else {
253+
subColumns.push(currentCol);
254+
}
255+
}
256+
257+
subColumns.forEach(column => {
247258
const shouldShow = this.shouldEnableFilterForColumn(column.field as string | number, column);
248259
const isFiltering = !!this.filterStateManager.getFilterState(column.field as string | number)?.enable;
249260
let icons = toIconList(column.headerIcon);

packages/vtable-plugins/src/filter/value-filter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class ValueFilter {
105105
// 空行不做处理
106106
if (isValid(record)) {
107107
const originalValue = record[fieldId];
108-
const formattedValue = formatFn(record);
108+
const formattedValue = formatFn(record) ?? '(空白)';
109109
if (formattedValue !== undefined && formattedValue !== null) {
110110
countMap.set(formattedValue, (countMap.get(formattedValue) || 0) + 1);
111111

@@ -139,7 +139,7 @@ export class ValueFilter {
139139
records = this.table.internalProps.records;
140140
} else {
141141
const recordsList = this.getRecords(this.table, true); // 已筛选:使用原始表格数据
142-
const records = recordsList.filter(record =>
142+
records = recordsList.filter(record =>
143143
filteredFields.every(field => {
144144
const filterType = this.filterStateManager.getFilterState(field)?.type;
145145
if (filterType !== 'byValue' && filterType !== null && filterType !== undefined) {
@@ -155,7 +155,7 @@ export class ValueFilter {
155155
// 空行不做处理
156156
if (isValid(record)) {
157157
const originalValue = record[candidateField];
158-
const formattedValue = formatFn(record);
158+
const formattedValue = formatFn(record) ?? '(空白)';
159159
countMap.set(formattedValue, (countMap.get(formattedValue) || 0) + 1);
160160
if (formattedValue !== undefined && formattedValue !== null) {
161161
const unformattedSet = toUnformatted.get(formattedValue);

0 commit comments

Comments
 (0)