In the below hierarchical grids, which include the parent and child grids. Show or hide some of the columns in the child grid is based on the values presented in the parent grid. Right now, it's done by using events, as indicated by the following codes.
@(Html.Kendo().Grid<Payroll.Models.PayrollAuthorizationByDepartmentModel>().Name("parentGrid")
.Columns(columns =>
{
columns.Bound(dataSource => dataSource.DepartmentName).Title("Department Name");
columns.Bound(dataSource => dataSource.TotalHours).Title("Hours");
columns.Bound(dataSource => dataSource.TotalVacationHours).Title("Vacation Hours");
columns.Bound(dataSource => dataSource.TotalBereavementHours).Title("Bereavement yHours");
columns.Bound(dataSource => dataSource.TotalJuryDutyHours).Title("Jury Duty Hours");
columns.Bound(dataSource => dataSource.TotalHolidayHours).Title("Holiday Hours");
columns.Bound(dataSource => dataSource.TotalPhamacistHours).Title("Phamacist Hours");
})
.DataSource(dataSource => dataSource
.Ajax()
.Aggregates(aggregates =>
.Read(read => read.Action("Location_Authorization_Read", "Authorize", new { locationId ="#=LocationId#"))
.PageSize(10)
.ServerOperation(false)
.Sort(sort => sort.Add("LocationId").Ascending())
)
.ClientDetailTemplateId("template")
.Events(events => events.DetailExpand("expandParentGrid"))
.Events(events => events.DataBound("onDataBound"))
)
<script id="template" type="text/kendo-tmpl">
@(Html.Kendo().Grid<Payroll.Models.PayrollAuthorizationByEmployeeModel>()
.Name("childGridAuthorizationByEmployee_#=DepartmentId#")
.Columns(columns =>
{
columns.Bound(dataSource => dataSource.EmployeeId).Title("Emp. No");
columns.Bound(dataSource => dataSource.EmployeeName).Title("Emp. Name");
columns.Bound(e => e.Hours).Title("Hours"));
columns.Bound(e => e.VacationHours).Title("Vac."));
columns.Bound(e => e.JuryDutyHours).Title("Jury"));
columns.Bound(e => e.HolidayPayHours).Title("Holiday"));
columns.Bound(e => e.BereavementPayHours).Title("Bereav."));
columns.Bound(e => e.PhamacistHours).Title("Pharm. "));
})
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("AuthorizationByEmployee_Read", "Authorize", new { locationId = "#=LocationId#", departmentId ="#=DepartmentId#"}))
.Events(events => events.DataBound("onDataBoundOfChildGrid"))
.ToClientTemplate())
</script>
<script type="text/javascript">
function onDataBound(e) {
this.expandRow(this.tbody.find("tr.k-master-row"));
}
function expandParentGrid(e){
var dataItem = e.sender.dataItem(e.masterRow);
if (!(dataItem.TotalVacationHours) > 0) {
$("#childGridAuthorizationByEmployee_" + dataItem.DepartmentId).data("kendoGrid").hideColumn("VacationHours");
}
if (!(dataItem.TotalJuryDutyHours) > 0) {
$("#childGridAuthorizationByEmployee_" + dataItem.DepartmentId).data("kendoGrid").hideColumn("JuryDutyHours");
}
if (!(dataItem.TotalHolidayPayHours) > 0) {
$("#childGridAuthorizationByEmployee_" + dataItem.DepartmentId).data("kendoGrid").hideColumn("HolidayPayHours");
}
if (!(dataItem.TotalBereavementPayHours) > 0) {
$("#childGridAuthorizationByEmployee_" + dataItem.DepartmentId).data("kendoGrid").hideColumn("BereavementPayHours");
}
if (!(dataItem.TotalPharmacistHours) > 0) {
$("#childGridAuthorizationByEmployee_" + dataItem.DepartmentId).data("kendoGrid").hideColumn("PhamacistHours");
}
}
</script>
I was wondering if there is a way to directly use properties of the parent grid in the child grid. Something like this:
<script id="template" type="text/kendo-tmpl">
@(Html.Kendo().Grid<Payroll.Models.PayrollAuthorizationByEmployeeModel>()
.Name("childGridAuthorizationByEmployee_#=DepartmentId#")
.Columns(columns =>
{
columns.Bound(dataSource => dataSource.EmployeeId).Title("Emp. No");
columns.Bound(dataSource => dataSource.EmployeeName).Title("Emp. Name");
columns.Bound(e => e.Hours).Title("Hours"));
if(#=******.TotalVacationHours# >0) {
columns.Bound(e => e.VacationHours).Title("Vac."));
}
if(#=*****.TotalJuryDutyHours# >0) {
columns.Bound(e => e.JuryDutyHours).Title("Jury"));
}
columns.Bound(e => e.HolidayPayHours).Title("Holiday"));
columns.Bound(e => e.BereavementPayHours).Title("Bereav."));
columns.Bound(e => e.PhamacistHours).Title("Pharm. "));
})
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("AuthorizationByEmployee_Read", "Authorize", new { locationId = "#=LocationId#", departmentId ="#=DepartmentId#"}))
.Events(events => events.DataBound("onDataBoundOfChildGrid"))
.ToClientTemplate())
</script>
Thank you very much!