Carga de rejilla de árbol bajo demanda
The Ignite UI for Angular IgxTreeGrid can be rendered in such way that it requires the minimal amount of data to get from the server so the user could see it as quickly as possible. Then, only after the user expands a row, the children for that particular parent row will be loaded. This mechanism, also known as Load on Demand, can be easily configured to work with any remote data.
Angular Tree Grid Load On Demand Example
Usage
The Load on Demand feature is compatible with both types of Tree Grid data sources - primary and foreign keys or child collection. You only need to load the root level data in the Tree Grid and specify the necessary keys for one of the data source types. In order to load the child rows when the user expands a row, the Tree Grid provides the callback input property loadChildrenOnDemand.
<igx-tree-grid #treeGrid [data]="data" primaryKey="ID" foreignKey="ParentID"
[loadChildrenOnDemand]="loadChildren">
...
</igx-tree-grid>
The loadChildrenOnDemand callback provides two parameters:
- parentID: el ID de la fila principal que se está expandiendo.
- done: devolución de llamada que se debe llamar con los niños cuando se recuperan del servidor.
public loadChildren = (parentID: any, done: (children: any[]) => void) => {
this.dataService.getData(parentID, (children) => done(children));
}
After the user clicks the expand icon, it is replaced by a loading indicator. When the done callback is called, the loading indicator disappears and the children are loaded. The Tree Grid adds the children to the underlying data source and populates the necessary keys automatically.
Si tienes una forma de proporcionar información sobre si una fila tiene hijos antes de expandirse, podrías usar la propiedad dehasChildrenKey entrada. De este modo, podrías proporcionar una propiedad booleana desde los objetos de datos que indique si debe mostrarse un indicador de expansión.
<igx-tree-grid #treeGrid [data]="data" primaryKey="ID" foreignKey="ParentID"
[loadChildrenOnDemand]="loadChildren" hasChildrenKey="hasEmployees">
...
</igx-tree-grid>
Note that setting the hasChildrenKey property is not required. In case you don't provide it, expansion indicators will be displayed for each row. After expanding a row that has no children, you still need to call the done callback with undefined or empty array. In this case after the loading indicator disappears, the expansion indicator never shows up.
If you want to provide your own custom loading indicator, you may create an ng-template and mark it with the igxRowLoadingIndicator directive. The following code snippet demonstrates how to define such custom template:
<igx-tree-grid ...>
...
<ng-template igxRowLoadingIndicator>
<igx-icon fontSet="material">loop</igx-icon>
</ng-template>
</igx-tree-grid>