Skip to content

Commit 9ad0e54

Browse files
authored
Merge pull request #4766 from VisActor/feature/optimize-filter-plugin
Feature/optimize filter plugin
2 parents dfd7581 + 2337bb9 commit 9ad0e54

7 files changed

Lines changed: 444 additions & 307 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
class: 1,
11+
phone: 30,
12+
computer: 30,
13+
tv: 30,
14+
pad: 30
15+
},
16+
{
17+
id: 2,
18+
class: 1,
19+
phone: 30,
20+
computer: 10,
21+
tv: 15,
22+
pad: 0
23+
},
24+
{
25+
id: 3,
26+
class: 4,
27+
phone: 360,
28+
computer: 360,
29+
tv: 240,
30+
pad: 240
31+
},
32+
{
33+
id: 4,
34+
class: 2,
35+
phone: 20,
36+
computer: 0,
37+
tv: 20,
38+
pad: 10
39+
},
40+
{
41+
id: 5,
42+
class: 4,
43+
phone: 20,
44+
computer: 15,
45+
tv: 10,
46+
pad: 0
47+
},
48+
{
49+
id: 6,
50+
class: 5,
51+
phone: 30,
52+
computer: 30,
53+
tv: 30,
54+
pad: 30
55+
},
56+
{
57+
id: 7,
58+
class: 3,
59+
phone: 2,
60+
computer: 1,
61+
tv: 1,
62+
pad: 1
63+
},
64+
{
65+
id: 8,
66+
class: 3,
67+
phone: 20,
68+
computer: 0,
69+
tv: 0,
70+
pad: 0
71+
},
72+
{
73+
id: 9,
74+
class: 3,
75+
phone: 10,
76+
computer: 20,
77+
tv: 10,
78+
pad: 10
79+
},
80+
{
81+
id: 10,
82+
class: 2,
83+
phone: 60,
84+
computer: 0,
85+
tv: 90,
86+
pad: 0
87+
},
88+
{
89+
id: 11,
90+
class: 1,
91+
phone: 30,
92+
computer: 20,
93+
tv: 10,
94+
pad: 0
95+
},
96+
{
97+
id: 12,
98+
class: 1,
99+
phone: 0,
100+
computer: 0,
101+
tv: 0,
102+
pad: 0
103+
}
104+
];
105+
const columns: VTable.ColumnsDefine = [
106+
{
107+
field: 'id',
108+
title: 'ID',
109+
width: 120,
110+
sort: true
111+
},
112+
{
113+
field: 'class',
114+
title: ' 班级',
115+
width: 120,
116+
sort: true,
117+
headerIcon: {
118+
type: 'svg',
119+
svg: '<svg width="24" height="24" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44Z" fill="#ff5a5f" stroke="#333" stroke-width="1"/></svg>',
120+
width: 20,
121+
height: 20,
122+
name: 'name-icon',
123+
positionType: VTable.TYPES.IconPosition.absoluteRight,
124+
marginRight: 30
125+
}
126+
},
127+
{
128+
field: 'phone',
129+
title: '手机',
130+
width: 100
131+
},
132+
{
133+
field: 'computer',
134+
title: '计算机',
135+
width: 120,
136+
sort: true
137+
},
138+
{
139+
field: 'tv',
140+
title: '电视机',
141+
width: 150,
142+
sort: true
143+
},
144+
{
145+
field: 'pad',
146+
title: '平板电脑',
147+
width: 100,
148+
sort: true
149+
}
150+
];
151+
152+
const filterPlugin = new FilterPlugin({});
153+
(window as any).filterPlugin = filterPlugin;
154+
155+
const option: VTable.ListTableConstructorOptions = {
156+
container: document.getElementById(CONTAINER_ID),
157+
records,
158+
columns,
159+
padding: 10,
160+
plugins: [filterPlugin]
161+
};
162+
const tableInstance = new VTable.ListTable(option);
163+
(window as any).tableInstance = tableInstance;
164+
165+
bindDebugTool(tableInstance.scenegraph.stage, {
166+
customGrapicKeys: ['col', 'row']
167+
});
168+
}

packages/vtable-plugins/demo/menu.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export const menus = [
1919
path: 'filter',
2020
name: 'bug'
2121
},
22+
{
23+
path: 'filter',
24+
name: 'value-filter'
25+
},
2226
{
2327
path: 'header-highlight',
2428
name: '(deprecated)header-highlight'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ export class FilterStateManager {
117117
FilterActionType.CLEAR_ALL_FILTERS,
118118
FilterActionType.APPLY_FILTERS
119119
];
120-
return shouldApplyActions.includes(action.type) || action.payload.enable;
120+
return shouldApplyActions.includes(action.type);
121121
}
122122
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export class FilterToolbar {
3535
this.conditionFilter = new ConditionFilter(this.table, this.filterStateManager);
3636

3737
this.filterMenuWidth = 300; // 待优化,可能需要自适应内容的宽度
38+
39+
// 监听筛选状态变化,更新清除筛选按钮状态
40+
this.filterStateManager.subscribe(state => {
41+
if (this.isVisible && this.selectedField !== null) {
42+
this.updateClearFilterButtonState(this.selectedField);
43+
}
44+
});
3845
}
3946

4047
private onTabSwitch(tab: 'byValue' | 'byCondition'): void {
@@ -82,6 +89,19 @@ export class FilterToolbar {
8289
this.hide();
8390
}
8491

92+
/**
93+
* 更新清除筛选按钮的状态
94+
*/
95+
private updateClearFilterButtonState(field: string | number): void {
96+
const currentFilter = this.filterStateManager.getFilterState(field);
97+
const hasActiveFilter = currentFilter && currentFilter.enable;
98+
99+
this.clearFilterOptionLink.style.display = 'inline';
100+
this.clearFilterOptionLink.style.opacity = hasActiveFilter ? '1' : '0.5';
101+
this.clearFilterOptionLink.style.pointerEvents = hasActiveFilter ? 'auto' : 'none';
102+
this.clearFilterOptionLink.style.cursor = hasActiveFilter ? 'pointer' : 'not-allowed';
103+
}
104+
85105
render(container: HTMLElement): void {
86106
// === 主容器 ===
87107
this.filterMenu = document.createElement('div');
@@ -240,6 +260,9 @@ export class FilterToolbar {
240260
this.onTabSwitch('byValue');
241261
}
242262

263+
// 更新清除筛选按钮状态
264+
this.updateClearFilterButtonState(field);
265+
243266
// 确保在事件冒泡完成后才设置 isVisible 为 true
244267
setTimeout(() => {
245268
this.isVisible = true;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ export class FilterPlugin implements pluginsDefinition.IVTablePlugin {
316316
this.table = null;
317317
this.filterEngine = null;
318318
this.filterStateManager = null;
319+
this.filterToolbar.valueFilter.destroy();
319320
this.filterToolbar = null;
320321
}
321322
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface FilterAction {
4848

4949
export interface ValueFilterOptionDom {
5050
id: string;
51-
originalValue: any;
51+
originalValue: any[];
5252
itemContainer: HTMLDivElement;
5353
checkbox: HTMLInputElement;
5454
countSpan: HTMLSpanElement;

0 commit comments

Comments
 (0)