I have parent and child grid. Parent Grid has Start and End date. Based on which it populates Months in Child grid.
I want user to give option to choose Year in child grid's toolbar. So user can select year and all the columns in child grid will be visible for selected year, hiding the non selected year columns from child g. So user can focus on each year and update data.
How do i achieve this ? I am aware that there is an option toolbarTemplate. but unfortunately I did not find anything which will help me achieve this.
Please note that Start and End date in parent will be different for each record.
|
export class AppComponent {
public parentData: Object[];
public childGrid: any;
public secondChildGrid: any;
detailDataBound(args: any) { //detaildataBound event of parent Grid//
//while expanding the parent row here we can access the parent row data
var startdate = args.data.StartDate.getFullYear(); //getting startdate year
var enddate = args.data.EndDate.getFullYear();//getting enddate year
var arr = [];
for(var i = startdate; i <= enddate; i++) {
arr.push(i); here we generate the dropdown datasource between the year of start and end date
}
let dropDownListObject: DropDownList = new DropDownList({
// set placeholder to DropDownList input element
placeholder: "Select a Year",
dataSource: arr,
value: arr[0],
change: this.onChange.bind(args)
});
dropDownListObject.appendTo(args.childGrid.element.querySelector('#toolbar-template'));
}
onChange(args) { //change event of dropdown
//here we have set visible the false for 0th and 2nd index column. You can hide your column based on your choosing year as you want
this.childGrid.columns[0].visible = false;
this.childGrid.columns[2].visible = false;
this.childGrid.refreshColumns(); //call this method for UI changes
}
ngOnInit(): void {
this.parentData = DataUtil.parse.parseJson(JSON.stringify(employeeData.slice(0,1)))
this.childGrid = {
dataSource: orderDatas,
queryString: 'EmployeeID',
toolbarTemplate: '#toolbar-template',
allowPaging: true,
pageSettings: {pageSize: 6, pageCount: 5},
columns: [
{ field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 120 },
{ field: 'ShipCity', headerText: 'Ship City', width: 120 },
{ field: 'Freight', headerText: 'Freight', width: 120 },
{ field: 'ShipName', headerText: 'Ship Name', width: 150 }
]
};
}
}
|
Thanks for the pointers but it did not work for me, may be because i already have a toolbar ? However, I tried using your method and added dropdown but it gave me weird result. Can you please help ?
As you can see instead of using template i have used class to append my dropdown.
|
this.childGrid = {
dataSource: orderDatas,
toolbar: ['Print', { text: 'Year', id: 'year' }],
}; |
|
// Grid’s detailDataBound event handler
detailDataBound(args) {
// The custom toolbar item element is retrieved using its id
var customItem = args.childGrid.toolbarModule.element.querySelector('#year');
// An input element is created for appending the dropdownlist component
var divEle = document.createElement('div');
// The input element is appended as a child to the custom toolbar item button’s parent
customItem.parentElement.append(divEle);
// The custom toolbar button element is removed
customItem.remove();
var startdate = args.data.StartDate.getFullYear();
var enddate = args.data.EndDate.getFullYear();
var arr = [];
for (var i = startdate; i <= enddate; i++) {
arr.push(i);
}
// The EJ2 Dropdownlist component is initialized and appended on to the input element
let dropDownListObject: DropDownList = new DropDownList({
// set placeholder to DropDownList input element
placeholder: "Select a Year",
dataSource: arr,
value: arr[0],
change: this.onChange.bind(args)
});
dropDownListObject.appendTo(divEle);
} |
|
// Dropdownlist’s change event handler
onChange(args) {
// Current child Grid instance is retrieved
var childGrid = args.element.closest('.e-grid').ej2_instances[0];
// Here the required actions can be performed on the child Grid and refreshed
childGrid.columns[0].visible = this.isState;
childGrid.columns[2].visible = this.isState;
childGrid.refreshColumns();
this.isState = !this.isState;
} |
We are nearly there. If you notice in your example we have to click dropdown 2 times in order to select value. first time we select it does not select value, second time when we select. it works as expected.
Can you please help.
|
export class AppComponent {
ngOnInit(): void {
this.parentData = DataUtil.parse.parseJson(JSON.stringify(employeeData))
this.childGrid = {
toolbar: ['Print', { template: "<input id='lname'>" }],
pageSettings: {pageSize: 6, pageCount: 5},
columns: [
{ field: 'OrderID', headerText: 'Order ID', width: 120 },
{ field: 'ShipCity', headerText: 'Ship City', width: 120 },
{ field: 'Freight', headerText: 'Freight', width: 120 },
{ field: 'ShipName', headerText: 'Ship Name', width: 150 }
]
};
}
detailDataBound(args) {
var startdate = args.data.StartDate.getFullYear();
var enddate = args.data.EndDate.getFullYear();
var arr = [];
for(var i = startdate; i <= enddate; i++) {
arr.push(i);
}
//The EJ2 Dropdownlist component is initialized and appended on to the input element
let dropDownListObject: DropDownList = new DropDownList({
// set placeholder to DropDownList input element
placeholder: "Select a Year",
dataSource: arr,
value: arr[0],
change: this.onChange.bind(args)
});
dropDownListObject.appendTo("#lname");
}
}
|