https://ej2.syncfusion.com/angular/documentation/grid/hierarchy-grid
I am trying to use this example of a hierarchy grid but I struggling to understand how to set datasource for child grid
@Component({
selector: 'app-root',
template: `
`,
providers: [DetailRowService, EditService, ToolbarService]
})
export class AppComponent implements OnInit {
public pData: object[];
public childGrid: GridModel = {
dataSource: data,
queryString: 'EmployeeID',
toolbar: ['Add', 'Edit', 'Delete', 'Update', 'Cancel'],
editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
columns: [
{ field: 'OrderID', headerText: 'Order ID', isPrimaryKey: true, textAlign: 'Right', width: 120 },
{ field: 'EmployeeID', headerText: 'Employee ID', textAlign: 'Right', allowEditing: false, width: 120 },
{ field: 'ShipCity', headerText: 'Ship City', width: 150 },
{ field: 'ShipName', headerText: 'Ship Name', width: 150 }
],
actionBegin(args: AddEventArgs) {
if (args.requestType === 'add') {
// `parentKeyFieldValue` refers to the queryString field value of the parent record.
const EmployeeID = 'EmployeeID';
(args.data as object)[EmployeeID] = this.parentDetails.parentKeyFieldValue;
}
}
};
@ViewChild('grid') public grid: GridComponent;
ngOnInit(): void {
this.pData = employeeData;
}
}
I am accustomed of setting data source for the main grid as follows and that works fine:
I think I would like to store the data of child under the parent data node in firebase like this "childEntries":
How could I plug this into child grid dataSource?
Hi Frank Alberts,
Greetings from syncfusion support,
Thank you for reaching out to us. We have reviewed your query about setting the datasource for the child grid in a hierarchy grid. After analyzing your request, we have confirmed that the hierarchy grid does not support using the same data source for both parent and child grids due to the foreign key column's functionality. However, we would like to suggest using the detailTemplate to store the child grid under the parent grid to fulfill your requirements. We have prepared the sample based on your requirement.
We have included a code snippet below that you can use to achieve your requirement.
[app.component.ts]
detailDataBound(e: DetailDataBoundEventArgs) { var index=parseInt((e as any).detailElement.children[0].parentElement.parentElement.previousSibling.getAttribute('data-rowindex')) let detail = new Grid({ dataSource: summaryRowData[index]['children'], columns: [ {field:'orderID', headerText:'orderID', width:'70' ,textAlign:'Right'}, { field: 'TotalCosts', headerText: 'TotalCosts', width: 140 }, { field: 'UnitWeight', headerText: 'UnitWeight', width: 150 }, ], }); |
Sample: https://stackblitz.com/edit/angular-l8ye2l?file=src%2Fapp.component.ts
If you require further assistance, please do not hesitate to contact us. We are always here to help you.
Regards,
Vikram S
Hi Vikram,
Thank you for your help so far! But I am struggling to understand how to set the datasource as the one from firebase:
So I have managed to bind to the data like this but now I am running into issues where I can not trigger function with event args of the child grid
So
actionCompleteMain
works fine with the main grid but, child grid,
actionBegin( cosole logs work fine but the triggering of firebasedb functions returns this error:
Frank Alberts,
From the error, we understand that the issue occurs due to the “this” reference inside the child “actionBegin” event which denotes the Child Grid reference, not the angular component. So, trying to access the angular component property throws the error. So, we suggest the arrow function to have the component reference scope for the child Grid actionBegin event and get the key value from the argument passed to the event.
Hi Pavithra,
I am trying to figure out your statement, but while I do that would it be possible to give a small code snippet for me to reference the ideas as I am not that much of an advanced developer and I can go wrong understanding the terminology,
Thank you for your help so much!
Hey quick update, so I have figured out how to do the arrow function. But it broke prentKeyFieldValue
I have a workaround by just setting a property myself from detail bound, but is there a more elegant way to do this like previously?
Hi Frank,
In actionBegin event, you can get the parentKeyFieldValue using parentsUntil method. The parentsUntil method is likely a custom function that searches for the nearest ancestor element of the given element with the specified class name ('e-grid') and returns it. If the requestType is 'save', the function retrieves a reference to a Grid control using the parentsUntil method. If the requestType is 'delete', the function retrieves a reference to the Grid control using the parentsUntil method with the first element of the args.tr array as the argument. This is likely the row that is being deleted.
Please refer the below code example for more information.
[app.component.ts]
actionBegin(args: any) { switch (args.requestType) { case 'save': console.log(args); var grid = (parentsUntil(args.form, 'e-grid') as any).ej2_instances[0]; console.log(grid.parentDetails); break; case 'delete': var grid = (parentsUntil(args.tr[0], 'e-grid') as any).ej2_instances[0]; console.log(grid.parentDetails); break; } }, |
Sample: https://stackblitz.com/edit/angular-mddxjg-6bgkie?file=src%2Fapp.component.ts
If you require further assistance, please do not hesitate to contact us. We are always here to help you.