-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AG-8439 - SSRM groupIncludeTotalFooter
- Loading branch information
1 parent
2928bbb
commit 6fde407
Showing
10 changed files
with
353 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...rid-docs/src/content/docs/server-side-model-grouping/_examples/total-footer/fakeServer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// This fake server uses http://alasql.org/ to mimic how a real server | ||
// might generate sql queries from the Server-Side Row Model request. | ||
// To keep things simple it does the bare minimum to support the example. | ||
function FakeServer(allData) { | ||
alasql.options.cache = false; | ||
|
||
return { | ||
getTotalData: function(request) { | ||
const query = `${selectSql(request)} FROM ? ${whereSql(request)}`; | ||
return alasql(query, [allData])[0]; | ||
}, | ||
getData: function(request) { | ||
var results = executeQuery(request); | ||
|
||
return { | ||
success: true, | ||
rows: results, | ||
lastRow: getLastRowIndex(request) | ||
}; | ||
} | ||
}; | ||
|
||
function executeQuery(request) { | ||
var sql = buildSql(request); | ||
|
||
console.log('[FakeServer] - about to execute query:', sql); | ||
|
||
return alasql(sql, [allData]); | ||
} | ||
|
||
function buildSql(request) { | ||
return selectSql(request) + ' FROM ?' + whereSql(request) + groupBySql(request) + orderBySql(request) + limitSql(request); | ||
} | ||
|
||
function selectSql(request) { | ||
var rowGroupCols = request.rowGroupCols; | ||
var valueCols = request.valueCols; | ||
var groupKeys = request.groupKeys; | ||
|
||
const isTotalFooter = (rowGroupCols && !groupKeys); | ||
if (isTotalFooter || isDoingGrouping(rowGroupCols, groupKeys)) { | ||
var colsToSelect = []; | ||
if (groupKeys) { | ||
var rowGroupCol = rowGroupCols[groupKeys.length]; | ||
colsToSelect.push(rowGroupCol.id); | ||
} | ||
|
||
valueCols.forEach(function(valueCol) { | ||
colsToSelect.push(valueCol.aggFunc + '(' + valueCol.id + ') AS ' + valueCol.id); | ||
}); | ||
|
||
return 'SELECT ' + colsToSelect.join(', '); | ||
} | ||
|
||
return 'SELECT *'; | ||
} | ||
|
||
function whereSql(request) { | ||
var rowGroups = request.rowGroupCols; | ||
var groupKeys = request.groupKeys; | ||
var whereParts = []; | ||
|
||
if (groupKeys) { | ||
groupKeys.forEach(function(key, i) { | ||
var value = typeof key === 'string' ? "'" + key + "'" : key; | ||
|
||
whereParts.push(rowGroups[i].id + ' = ' + value); | ||
}); | ||
} | ||
|
||
if (whereParts.length > 0) { | ||
return ' WHERE ' + whereParts.join(' AND '); | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
function groupBySql(request) { | ||
var rowGroupCols = request.rowGroupCols; | ||
var groupKeys = request.groupKeys; | ||
|
||
if (isDoingGrouping(rowGroupCols, groupKeys)) { | ||
var rowGroupCol = rowGroupCols[groupKeys.length]; | ||
|
||
return ' GROUP BY ' + rowGroupCol.id + ' HAVING count(*) > 0'; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
function orderBySql(request) { | ||
var sortModel = request.sortModel; | ||
|
||
if (sortModel.length === 0) return ''; | ||
|
||
var sorts = sortModel.map(function(s) { | ||
return s.colId + ' ' + s.sort.toUpperCase(); | ||
}); | ||
|
||
return ' ORDER BY ' + sorts.join(', '); | ||
} | ||
|
||
function limitSql(request) { | ||
if (request.endRow == undefined || request.startRow == undefined) { return ''; } | ||
var blockSize = request.endRow - request.startRow; | ||
|
||
return ' LIMIT ' + blockSize + ' OFFSET ' + request.startRow; | ||
} | ||
|
||
function isDoingGrouping(rowGroupCols, groupKeys) { | ||
// we are not doing grouping if at the lowest level | ||
return rowGroupCols.length > groupKeys.length; | ||
} | ||
|
||
function getLastRowIndex(request) { | ||
return executeQuery({ ...request, startRow: undefined, endRow: undefined }).length; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...g-grid-docs/src/content/docs/server-side-model-grouping/_examples/total-footer/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div class="example-wrapper"> | ||
<div id="myGrid" class="ag-theme-quartz"></div> | ||
</div> | ||
|
||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/alasql.min.js"></script> |
104 changes: 104 additions & 0 deletions
104
...n/ag-grid-docs/src/content/docs/server-side-model-grouping/_examples/total-footer/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { | ||
GridApi, | ||
createGrid, | ||
GetRowIdParams, | ||
GridOptions, | ||
IServerSideDatasource, | ||
IServerSideGetRowsParams, | ||
IsServerSideGroupOpenByDefaultParams, | ||
IServerSideGetTotalFooterDataParams, | ||
} from '@ag-grid-community/core'; | ||
declare var FakeServer: any; | ||
import { RowGroupingModule } from '@ag-grid-enterprise/row-grouping'; | ||
import { ServerSideRowModelModule } from '@ag-grid-enterprise/server-side-row-model'; | ||
import { ModuleRegistry } from "@ag-grid-community/core"; | ||
|
||
ModuleRegistry.registerModules([RowGroupingModule, ServerSideRowModelModule]); | ||
|
||
let gridApi: GridApi<IOlympicData>; | ||
const gridOptions: GridOptions<IOlympicData> = { | ||
columnDefs: [ | ||
{ field: 'country', enableRowGroup: true, rowGroup: true, hide: true }, | ||
{ field: 'sport', enableRowGroup: true, rowGroup: true, hide: true }, | ||
{ field: 'year', minWidth: 100 }, | ||
{ field: 'gold', aggFunc: 'sum', enableValue: true }, | ||
{ field: 'silver', aggFunc: 'sum', enableValue: true }, | ||
{ field: 'bronze', aggFunc: 'sum', enableValue: true }, | ||
], | ||
defaultColDef: { | ||
flex: 1, | ||
minWidth: 120, | ||
}, | ||
autoGroupColumnDef: { | ||
flex: 1, | ||
minWidth: 280, | ||
}, | ||
rowModelType: 'serverSide', | ||
isServerSideGroupOpenByDefault: isServerSideGroupOpenByDefault, | ||
groupIncludeTotalFooter: true, | ||
getRowId: getRowId, | ||
}; | ||
|
||
function getRowId(params: GetRowIdParams) { | ||
return Math.random().toString(); | ||
} | ||
|
||
function isServerSideGroupOpenByDefault( | ||
params: IsServerSideGroupOpenByDefaultParams | ||
) { | ||
var route = params.rowNode.getRoute(); | ||
if (!route) { | ||
return false; | ||
} | ||
|
||
var routeAsString = route.join(','); | ||
|
||
var routesToOpenByDefault = ['Zimbabwe', 'Zimbabwe,Swimming']; | ||
|
||
return routesToOpenByDefault.indexOf(routeAsString) >= 0; | ||
} | ||
|
||
function getServerSideDatasource(server: any): IServerSideDatasource { | ||
return { | ||
getTotalFooterData: (params: IServerSideGetTotalFooterDataParams) => { | ||
const response = server.getTotalData(params.request); | ||
params.success(response); | ||
}, | ||
getRows: (params: IServerSideGetRowsParams) => { | ||
console.log('[Datasource] - rows requested by grid: ', params.request); | ||
|
||
var response = server.getData(params.request); | ||
|
||
// adding delay to simulate real server call | ||
setTimeout(() => { | ||
if (response.success) { | ||
// call the success callback | ||
params.success({ | ||
rowData: response.rows, | ||
rowCount: response.lastRow, | ||
}); | ||
} else { | ||
// inform the grid request failed | ||
params.fail(); | ||
} | ||
}, 400); | ||
}, | ||
}; | ||
} | ||
|
||
// setup the grid after the page has finished loading | ||
var gridDiv = document.querySelector<HTMLElement>('#myGrid')!; | ||
gridApi = createGrid(gridDiv, gridOptions); | ||
|
||
fetch('https://www.ag-grid.com/example-assets/olympic-winners.json') | ||
.then((response) => response.json()) | ||
.then(function (data) { | ||
// setup the fake server with entire dataset | ||
var fakeServer = new FakeServer(data); | ||
|
||
// create datasource with a reference to the fake server | ||
var datasource = getServerSideDatasource(fakeServer); | ||
|
||
// register the datasource with the grid | ||
gridApi!.setGridOption('serverSideDatasource', datasource); | ||
}); |
10 changes: 10 additions & 0 deletions
10
...g-grid-docs/src/content/docs/server-side-model-grouping/_examples/total-footer/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.example-wrapper { | ||
display: flex; | ||
flex-direction: column; | ||
height: 100%; | ||
} | ||
|
||
#myGrid { | ||
flex: 1 1 auto; | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.