-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiltering-usage-examples.js
More file actions
266 lines (222 loc) · 6.67 KB
/
Copy pathfiltering-usage-examples.js
File metadata and controls
266 lines (222 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// TablixJS Advanced Filtering - Usage Examples
// Basic table initialization with filtering enabled
const table = new Table('#myTable', {
data: myData,
columns: myColumns,
filtering: {
enabled: true,
mode: 'client', // or 'server'
showBadges: true,
showTooltips: true,
debounceDelay: 300
}
});
// ===========================================
// PROGRAMMATIC FILTERING EXAMPLES
// ===========================================
// 1. Value-based filtering (checkbox selection)
await table.applyFilter('status', {
type: 'value',
values: ['Active', 'Pending']
});
// 2. Condition-based filtering
await table.applyFilter('name', {
type: 'condition',
conditions: [
{ operator: 'beginsWith', value: 'A' },
{ operator: 'contains', value: 'son' }
]
});
// 3. Complex condition examples
await table.applyFilter('salary', {
type: 'condition',
conditions: [
{ operator: 'equals', value: '75000' }
]
});
await table.applyFilter('email', {
type: 'condition',
conditions: [
{ operator: 'endsWith', value: '@company.com' },
{ operator: 'notContains', value: 'temp' }
]
});
// 4. Empty/Not empty filters
await table.applyFilter('description', {
type: 'condition',
conditions: [
{ operator: 'isNotEmpty' }
]
});
// 5. Clear specific column filter
await table.clearFilter('status');
// 6. Clear all filters
await table.clearAllFilters();
// 7. Get current filter state
const activeFilters = table.getActiveFilters();
console.log('Current filters:', activeFilters);
// 8. Get filter for specific column
const statusFilter = table.getColumnFilter('status');
console.log('Status filter:', statusFilter);
// ===========================================
// EVENT HANDLING
// ===========================================
// Listen for filter events
table.eventManager.on('beforeFilter', (data) => {
console.log('About to apply filter:', data);
// data.columnName, data.filterConfig, data.currentFilters
});
table.eventManager.on('afterFilter', (data) => {
console.log('Filter applied:', data);
// data.columnName, data.filterConfig, data.filteredData, data.activeFilters
// Update UI based on filtered results
updateStatusBar(data.filteredData.length);
});
// ===========================================
// SERVER-SIDE FILTERING
// ===========================================
const serverTable = new Table('#serverTable', {
filtering: {
enabled: true,
mode: 'server',
serverFilterLoader: async (params) => {
// params contains: filters, sort, page, pageSize
const response = await fetch('/api/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params)
});
const result = await response.json();
// Must return { data: [...], totalRows: number }
return {
data: result.items,
totalRows: result.totalCount
};
}
}
});
// ===========================================
// CUSTOM OPERATORS
// ===========================================
// Register custom filter operator
table.filterManager.registerOperator('isEven', {
label: 'Is even number',
apply: (value) => {
const num = parseInt(value);
return !isNaN(num) && num % 2 === 0;
}
});
// Use custom operator
await table.applyFilter('id', {
type: 'condition',
conditions: [
{ operator: 'isEven' }
]
});
// ===========================================
// FILTER COMBINATIONS
// ===========================================
// Apply multiple filters across columns
await table.applyFilter('department', {
type: 'value',
values: ['Engineering', 'Marketing']
});
await table.applyFilter('status', {
type: 'value',
values: ['Active']
});
await table.applyFilter('salary', {
type: 'condition',
conditions: [
{ operator: 'contains', value: '7' } // Salary contains '7'
]
});
// ===========================================
// INTEGRATION WITH OTHER FEATURES
// ===========================================
// Filtering works seamlessly with sorting and pagination
await table.sort('name', 'asc'); // Sort after filtering
await table.goToPage(2); // Navigate through filtered results
// ===========================================
// UI CUSTOMIZATION
// ===========================================
// Access FilterUI for advanced customization
const filterUI = table.filterUI;
// Close all dropdowns programmatically
filterUI.closeAllDropdowns();
// Get available operators
const operators = table.filterManager.getOperators();
console.log('Available operators:', Object.keys(operators));
// ===========================================
// ADVANCED SCENARIOS
// ===========================================
// 1. Filter with dynamic data
table.eventManager.on('afterLoad', async () => {
// Reapply filters after new data is loaded
const currentFilters = table.getActiveFilters();
if (Object.keys(currentFilters).length > 0) {
// Filters are automatically preserved and reapplied
console.log('Filters maintained after data reload');
}
});
// 2. Conditional filter UI
const shouldShowFilters = (columnName) => {
return ['status', 'department', 'name'].includes(columnName);
};
// Custom column configuration
const advancedColumns = [
{
name: 'id',
title: 'ID',
filterable: false // Could be used for custom logic
},
{
name: 'name',
title: 'Full Name',
filterable: true
}
// ... more columns
];
// 3. Batch filter operations
const batchApplyFilters = async (filterConfigs) => {
for (const [columnName, config] of Object.entries(filterConfigs)) {
await table.applyFilter(columnName, config);
}
};
await batchApplyFilters({
status: { type: 'value', values: ['Active'] },
department: { type: 'value', values: ['Engineering'] }
});
// ===========================================
// ERROR HANDLING
// ===========================================
try {
await table.applyFilter('invalidColumn', {
type: 'condition',
conditions: [{ operator: 'unknownOp', value: 'test' }]
});
} catch (error) {
console.error('Filter error:', error);
}
// ===========================================
// PERFORMANCE CONSIDERATIONS
// ===========================================
// For large datasets, consider:
// 1. Server-side filtering for 10,000+ rows
// 2. Debounce user input in condition filters
// 3. Limit the number of unique values in value filters
// Debounced filter application example
let filterTimeout;
const debouncedFilter = (columnName, filterConfig) => {
clearTimeout(filterTimeout);
filterTimeout = setTimeout(async () => {
await table.applyFilter(columnName, filterConfig);
}, 300);
};
export {
// Export examples for documentation
table,
serverTable,
batchApplyFilters,
debouncedFilter
};