Issue: http://issues.qcadoo.org/browse/QCADOO-56
Requirements
As a developers I would like to have the ability to insert a custom dataset to the grid component in a way that would preserve its filter and pagination functionality if possible.
...
Code Block |
---|
language | xml |
---|
title | Order groups grid component |
---|
|
<component type="grid" name="orderGroups" reference="grid">
<option type="column" name="number" fields="number" link="true" />
<option type="column" name="name" fields="name" link="true" />
<!-- suggested option for an column from a custom dataset, the main entity must always be in the 0 column index -->
<option type="column" name="ordersCount" datasetColumIndex="1" />
<option type="column" name="dateFrom" fields="dateFrom" />
<option type="column" name="dateTo" fields="dateTo" />
<option type="order" column="name" direction="asc" />
<option type="correspondingView" value="orderGroups/orderGroupDetails" />
<option type="correspondingComponent" value="form" />
<option type="searchable" value="name,number,dateFrom,dateTo" />
<option type="orderable" value="name,number,dateFrom,dateTo" />
<option type="fullscreen" value="true" />
<option type="multiselect" value="true" />
</component>
|
Code Block |
---|
title | Hook for loading data |
---|
|
loadOrderGroups(viewState) {
grid = viewState.getComponentByReference("orderGroupsGrid");
// expanded Hibernate or JPA criteria builder
criteriaBuilder = someService.getCriteriaBuilder();
criteriaQuery = criteriaBuilder.createQuery();
from = criteriaQuery.from("orderGroup");
min = criteriaBuilder.min(from.get("orders"));
// the 'modelName' entity must be always first
criteriaQuery = criteriaQuery.multiselect(from, min);
// this would apply criteria from the grids filter but not pagination yet
criteriaQuery = someService.applyGridFilterCriteria(criteriaQuery, grid);
query = someService.createQuery(criteriaQuery);
// list() dosn't fetch data from the db, it just prepares the list
// object which in fact is a proxy to the database
datasetSize = query.list().size();
query.setFirstResult(grid.getFirstResult());
query.setMaxResults(grid.getMaxResults());
// the first get from the list will actualy call select on the db
// remember that in this case this is actually a List<Object[]>
grid.setDatasetPage(query.list());
grid.setDatasetSize(datasetSize);
}
|
...
Code Block |
---|
|
loadInventory(viewState) {
grid = viewState.getComponentByReference("inventoryGrid");
criteriaBuilder = someService.getCriteriaBuilder();
// my method that prepares a query
criteriaQuery = preapreBaseInventoryCriteriaQuery(criteriaBuilder);
searchFilters = grid.getSearchFilters()
// my method to add the right criteria
criteriaQuery = applyInventorySearchFilters(criteriaQuery, searchFilters);
ordering = grid.getOrdering();
// my method to add the right criteria
criteriaQuery = applyInventoryOrderFilters(criteriaQuery);
query = someService.createQuery(criteriaQuery);
datasetSize = query.list().size();
query.setFirstResult(grid.getFirstResult());
query.setMaxResults(grid.getMaxResults());
grid.setDatasetPage(query.list());
grid.setDatasetSize(datasetSize);
} |
...