Nirman
Nirman

Reputation: 6793

How to implement attribute driven visibility of Kendo grid columns?

Following is the Kendo Grid in one of my MVC view -

@(Html.Kendo().Grid<MyProject.ViewModels.EmployeeViewModel>()
.Name("EmployeeGrid")
.DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("AdminEmployeeSearchData", "Employee").Type(HttpVerbs.Post))
        .ServerOperation(true)
        )
.Columns(columns =>
{
    columns.Bound(p => p.EmployeeID).Title("ID")
    columns.Bound(p => p.EmployeeName).Title("Employee Name")
    columns.Bound(p => p.EmployeeSalary).Title("Employee Salary")
})
.AutoBind(false)
.Sortable()
.EnableCustomBinding(true)

)

Here, EmployeeSalary column is supposed to be visible to specific roles. In order to achieve that, one simple approach is to use IF...ELSE block in the view where I will validate the current user role, and based on it will display/ hide the column. But this is bound to become unmanageable once we have a number of columns in the grid.

I am thinking to drive this through Attribute at the field level of the view model. That is, EmployeeSalary field will have some attribute say "IsAccessible" and the logic would be at the attribute level. And based on the result the Kendo grid should display/ hide that column.

Could anyone please suggest how we can achieve this?

Also, let me know if anything is unclear here.

Any help on this will be much appreciated.

Thanks

Upvotes: 1

Views: 1363

Answers (1)

Steve Greene
Steve Greene

Reputation: 12324

If you have a model property that determines visibility you, can use the MVC wrapper's Visible property. So for example:

columns.Bound(p => p.EmployeeSalary).Title("Employee Salary").Visible(Model.IsManager)

Upvotes: 2

Related Questions