-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpansion-panel.ts
More file actions
406 lines (360 loc) · 13.8 KB
/
expansion-panel.ts
File metadata and controls
406 lines (360 loc) · 13.8 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { Component, OnInit, ViewChild, QueryList, ViewChildren } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatButtonModule } from '@angular/material/button';
import { AgGridAngular } from 'ag-grid-angular';
import { GridOptions, IServerSideDatasource, IServerSideGetRowsRequest } from 'ag-grid-community';
import 'ag-grid-enterprise';
interface GridConfig {
id: string;
title: string;
columnDefs: any[];
datasourceUrl: string;
gridOptions?: GridOptions;
isExpanded?: boolean;
isConfigured?: boolean;
}
@Component({
selector: 'app-multi-grid',
standalone: true,
imports: [CommonModule, MatExpansionModule, MatButtonModule, AgGridAngular],
template: `
<div class="container">
<h2>Multiple AG-Grid with SSRM</h2>
<mat-accordion multi="true">
<mat-expansion-panel
*ngFor="let config of gridConfigs; trackBy: trackByGridId"
[(expanded)]="config.isExpanded"
(opened)="onPanelOpened(config)"
(closed)="onPanelClosed(config)">
<mat-expansion-panel-header>
<mat-panel-title>{{ config.title }}</mat-panel-title>
<mat-panel-description>
{{ config.isConfigured ? 'Configured' : 'Not configured' }}
</mat-panel-description>
</mat-expansion-panel-header>
<div class="grid-container" *ngIf="config.isExpanded">
<ag-grid-angular
#agGrid
class="ag-theme-alpine"
style="height: 400px; width: 100%;"
[gridOptions]="config.gridOptions"
[columnDefs]="config.columnDefs"
[rowModelType]="'serverSide'"
[suppressRowClickSelection]="true"
[animateRows]="true"
[pagination]="true"
[paginationPageSize]="100"
[cacheBlockSize]="100"
[maxBlocksInCache]="10"
(gridReady)="onGridReady($event, config)">
</ag-grid-angular>
</div>
</mat-expansion-panel>
</mat-accordion>
</div>
`,
styles: [`
.container {
padding: 20px;
max-width: 1200px;
margin: 0 auto;
}
.grid-container {
margin-top: 16px;
}
mat-expansion-panel {
margin-bottom: 8px;
}
.ag-theme-alpine {
border: 1px solid #ddd;
border-radius: 4px;
}
`]
})
export class MultiGridComponent implements OnInit {
@ViewChildren('agGrid') agGrids!: QueryList<AgGridAngular>;
gridConfigs: GridConfig[] = [
{
id: 'users-grid',
title: 'Users Data',
datasourceUrl: '/api/users',
columnDefs: [
{ field: 'id', headerName: 'ID', width: 80, sortable: true, filter: true },
{ field: 'name', headerName: 'Name', width: 150, sortable: true, filter: 'agTextColumnFilter' },
{ field: 'email', headerName: 'Email', width: 200, sortable: true, filter: 'agTextColumnFilter' },
{ field: 'department', headerName: 'Department', width: 130, sortable: true, filter: 'agSetColumnFilter' },
{ field: 'role', headerName: 'Role', width: 120, sortable: true, filter: 'agSetColumnFilter' },
{ field: 'salary', headerName: 'Salary', width: 100, sortable: true, filter: 'agNumberColumnFilter', valueFormatter: this.currencyFormatter }
],
isExpanded: false,
isConfigured: false
},
{
id: 'orders-grid',
title: 'Orders Data',
datasourceUrl: '/api/orders',
columnDefs: [
{ field: 'orderId', headerName: 'Order ID', width: 100, sortable: true, filter: true },
{ field: 'customerName', headerName: 'Customer', width: 150, sortable: true, filter: 'agTextColumnFilter' },
{ field: 'product', headerName: 'Product', width: 200, sortable: true, filter: 'agTextColumnFilter' },
{ field: 'quantity', headerName: 'Quantity', width: 100, sortable: true, filter: 'agNumberColumnFilter' },
{ field: 'price', headerName: 'Price', width: 100, sortable: true, filter: 'agNumberColumnFilter', valueFormatter: this.currencyFormatter },
{ field: 'status', headerName: 'Status', width: 120, sortable: true, filter: 'agSetColumnFilter' },
{ field: 'orderDate', headerName: 'Order Date', width: 130, sortable: true, filter: 'agDateColumnFilter' }
],
isExpanded: false,
isConfigured: false
},
{
id: 'products-grid',
title: 'Products Data',
datasourceUrl: '/api/products',
columnDefs: [
{ field: 'productId', headerName: 'Product ID', width: 100, sortable: true, filter: true },
{ field: 'name', headerName: 'Product Name', width: 200, sortable: true, filter: 'agTextColumnFilter' },
{ field: 'category', headerName: 'Category', width: 130, sortable: true, filter: 'agSetColumnFilter' },
{ field: 'price', headerName: 'Price', width: 100, sortable: true, filter: 'agNumberColumnFilter', valueFormatter: this.currencyFormatter },
{ field: 'stock', headerName: 'Stock', width: 100, sortable: true, filter: 'agNumberColumnFilter' },
{ field: 'supplier', headerName: 'Supplier', width: 150, sortable: true, filter: 'agTextColumnFilter' },
{ field: 'description', headerName: 'Description', width: 250, sortable: true, filter: 'agTextColumnFilter' }
],
isExpanded: false,
isConfigured: false
}
];
ngOnInit(): void {
this.initializeGridOptions();
}
trackByGridId(index: number, config: GridConfig): string {
return config.id;
}
private initializeGridOptions(): void {
this.gridConfigs.forEach(config => {
config.gridOptions = {
defaultColDef: {
flex: 1,
minWidth: 100,
resizable: true,
sortable: true,
filter: true,
floatingFilter: true
},
rowSelection: 'multiple',
enableRangeSelection: true,
enableCharts: true,
sideBar: {
toolPanels: [
{
id: 'columns',
labelDefault: 'Columns',
labelKey: 'columns',
iconKey: 'columns',
toolPanel: 'agColumnsToolPanel'
},
{
id: 'filters',
labelDefault: 'Filters',
labelKey: 'filters',
iconKey: 'filter',
toolPanel: 'agFiltersToolPanel'
}
]
},
statusBar: {
statusPanels: [
{ statusPanel: 'agTotalAndFilteredRowCountComponent', align: 'left' },
{ statusPanel: 'agSelectedRowCountComponent', align: 'center' },
{ statusPanel: 'agAggregationComponent', align: 'right' }
]
}
};
});
}
onPanelOpened(config: GridConfig): void {
console.log(`Panel opened: ${config.title}`);
config.isExpanded = true;
// Trigger grid configuration when panel opens
if (!config.isConfigured) {
setTimeout(() => {
this.configureGrid(config);
}, 100); // Small delay to ensure DOM is ready
}
}
onPanelClosed(config: GridConfig): void {
console.log(`Panel closed: ${config.title}`);
config.isExpanded = false;
}
onGridReady(event: any, config: GridConfig): void {
console.log(`Grid ready: ${config.title}`);
if (!config.isConfigured) {
this.configureGrid(config);
}
}
private configureGrid(config: GridConfig): void {
if (config.isConfigured) return;
console.log(`Configuring grid: ${config.title}`);
// Find the specific AG-Grid instance for this config
const agGridComponent = this.findAgGridComponent(config);
if (agGridComponent && agGridComponent.api) {
// Create server-side datasource
const datasource = this.createServerSideDatasource(config);
// Set the datasource
agGridComponent.api.setServerSideDatasource(datasource);
config.isConfigured = true;
console.log(`Grid configured successfully: ${config.title}`);
}
}
private findAgGridComponent(config: GridConfig): AgGridAngular | undefined {
// This is a simplified approach. In a more complex scenario,
// you might want to use ViewChild with template reference variables
// or implement a more sophisticated grid tracking mechanism
return this.agGrids.find((grid, index) => {
const configIndex = this.gridConfigs.findIndex(c => c.id === config.id);
return index === configIndex;
});
}
private createServerSideDatasource(config: GridConfig): IServerSideDatasource {
return {
getRows: (params: IServerSideGetRowsRequest) => {
console.log(`Loading data for ${config.title}:`, params);
// Simulate server-side data loading
this.loadServerSideData(config, params)
.then(result => {
console.log(`Data loaded for ${config.title}:`, result);
params.success({
rowData: result.data,
rowCount: result.totalCount
});
})
.catch(error => {
console.error(`Error loading data for ${config.title}:`, error);
params.fail();
});
}
};
}
private async loadServerSideData(config: GridConfig, params: IServerSideGetRowsRequest): Promise<{ data: any[], totalCount: number }> {
// Extract request parameters
const startRow = params.request.startRow || 0;
const endRow = params.request.endRow || 100;
const pageSize = endRow - startRow;
const sortModel = params.request.sortModel;
const filterModel = params.request.filterModel;
// Build query parameters
const queryParams = new URLSearchParams({
startRow: startRow.toString(),
endRow: endRow.toString(),
pageSize: pageSize.toString()
});
if (sortModel && sortModel.length > 0) {
queryParams.append('sortBy', sortModel[0].colId);
queryParams.append('sortDirection', sortModel[0].sort);
}
if (filterModel && Object.keys(filterModel).length > 0) {
queryParams.append('filters', JSON.stringify(filterModel));
}
// In a real application, make HTTP request to your server
// For demo purposes, return mock data
return this.getMockData(config, startRow, pageSize, sortModel, filterModel);
}
private async getMockData(
config: GridConfig,
startRow: number,
pageSize: number,
sortModel: any[],
filterModel: any
): Promise<{ data: any[], totalCount: number }> {
// Simulate network delay
await new Promise(resolve => setTimeout(resolve, 500));
let mockData: any[] = [];
const totalCount = 1000; // Total records available
// Generate mock data based on grid type
switch (config.id) {
case 'users-grid':
mockData = this.generateUsersData(startRow, pageSize);
break;
case 'orders-grid':
mockData = this.generateOrdersData(startRow, pageSize);
break;
case 'products-grid':
mockData = this.generateProductsData(startRow, pageSize);
break;
}
// Apply basic sorting (in real app, this would be done server-side)
if (sortModel && sortModel.length > 0) {
const sortField = sortModel[0].colId;
const sortDirection = sortModel[0].sort;
mockData.sort((a, b) => {
const aValue = a[sortField];
const bValue = b[sortField];
if (sortDirection === 'asc') {
return aValue < bValue ? -1 : aValue > bValue ? 1 : 0;
} else {
return aValue > bValue ? -1 : aValue < bValue ? 1 : 0;
}
});
}
return {
data: mockData,
totalCount: totalCount
};
}
private generateUsersData(startRow: number, pageSize: number): any[] {
const data = [];
const departments = ['Engineering', 'Sales', 'Marketing', 'HR', 'Finance'];
const roles = ['Manager', 'Senior', 'Junior', 'Lead', 'Director'];
for (let i = 0; i < pageSize; i++) {
const id = startRow + i + 1;
data.push({
id: id,
name: `User ${id}`,
email: `user${id}@company.com`,
department: departments[id % departments.length],
role: roles[id % roles.length],
salary: Math.floor(Math.random() * 100000) + 40000
});
}
return data;
}
private generateOrdersData(startRow: number, pageSize: number): any[] {
const data = [];
const products = ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard', 'Mouse'];
const statuses = ['Pending', 'Processing', 'Shipped', 'Delivered', 'Cancelled'];
for (let i = 0; i < pageSize; i++) {
const id = startRow + i + 1;
data.push({
orderId: `ORD-${id.toString().padStart(5, '0')}`,
customerName: `Customer ${id}`,
product: products[id % products.length],
quantity: Math.floor(Math.random() * 10) + 1,
price: Math.floor(Math.random() * 1000) + 100,
status: statuses[id % statuses.length],
orderDate: new Date(2024, Math.floor(Math.random() * 12), Math.floor(Math.random() * 28) + 1).toISOString().split('T')[0]
});
}
return data;
}
private generateProductsData(startRow: number, pageSize: number): any[] {
const data = [];
const categories = ['Electronics', 'Clothing', 'Books', 'Home', 'Sports'];
const suppliers = ['Supplier A', 'Supplier B', 'Supplier C', 'Supplier D'];
for (let i = 0; i < pageSize; i++) {
const id = startRow + i + 1;
data.push({
productId: `PROD-${id.toString().padStart(5, '0')}`,
name: `Product ${id}`,
category: categories[id % categories.length],
price: Math.floor(Math.random() * 500) + 10,
stock: Math.floor(Math.random() * 100),
supplier: suppliers[id % suppliers.length],
description: `Description for product ${id}`
});
}
return data;
}
private currencyFormatter(params: any): string {
if (params.value == null) return '';
return '$' + params.value.toLocaleString();
}
}