This is a migrated thread and some comments may be shown as answers.

Hide column based on bool value passed within the databound model

5 Answers 3728 Views
Grid
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 22 Nov 2018, 10:10 AM

Hey,

 

So i have a grid and within the databound model i return an [Active] variable, based on if this variable is true or false i would like to hide a column within the grid based on this [Active] value.

I have read that if columns need to be hidden / shown dynamically that i should use  .Hidden() instead of .Visible() because the .Hidden() will still render the column but just set its display to [None] whereas the .Visible() does not render the column at all.

Based on a value selected from a dropdown a new request is made to the server and the grid is rebound with data,the grid should hide / show the appropriate columns

 

Html.Kendo().Grid<JobsViewModel>()
.Name("Grid")
.Columns(columns =>
{
 columns.Bound(o => o.Status).Title("Status");
 columns.Bound(o => o.Reason).Title("Reason").Hidden(o=>o.Active); <-- How to hide based on [Active]

 

i need the [Reason] column hidden based on the [Active] variable

.Hidden(o=>o.Active) is not valid, this would be the perfect scenario for me.

 

5 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 26 Nov 2018, 07:07 AM
Hello Rhett,

Based on the provided information I assume that the Active variable is actually a field of the data item. Please correct me if I am wrong.

Have in mind that the column can either completely hidden or displayed - it is not possible to hide a column only for certain rows. Therefore it is not possible to hide/show the column depending no a field of the data item.

The Hidden setting has an overload which accepts a boolean parameter that indicates whether the column should be hidden or not. In other words, you could use a flag and based on its value hide/show the column.

e.g.
 
columns.Bound(o => o.Reason).Title("Reason").Hidden(Model.Active); // where Model.Active is a boolean property of the view model


Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
n/a
Top achievements
Rank 1
answered on 28 Nov 2018, 08:24 AM

The [Active] variable is a field on the data item.

I understand that you cannot hide columns for only certain rows, that is not what i am trying to achieve.

In my particular case once the grid has been loaded, all of the rows [Active] variable will be either True or False because of my filter, you may only view all [Active] = True OR [Active] = False. the grid will never show data for both True and False at the same time.

This is what i want, in this grid there are 10 columns but only only 5 columns should be shown at any given time based on [Active] = True or [Active] = False.

So if [Active] = True,Only show 5 columns

If [Active] = False, Only show the other 5 columns and hide the 5 Active columns, if that makes sense.

 

On the other hand, is it possible to set a ViewBag Object every time the Grid loads?

Example

Html.Kendo().Grid<JobsViewModel>()
.Name("Grid")
.Columns(columns =>
{
 columns.Bound(o => o.Status).Title("Status");
 columns.Bound(o => o.Reason).Title("Reason").Hidden(ViewBag.Active);

 

I would need the ViewBag object value changed on every Grid.Read() request.

I hope this make sense.

0
n/a
Top achievements
Rank 1
answered on 28 Nov 2018, 08:27 AM
columns.Bound(o => o.Reason).Title("Reason").Hidden(Model.Active); // where Model.Active is a boolean property of the view model

 

I would need the above to work not for the ViewModel of the page, but the View Model of the actual grid.

Html.Kendo().Grid<JobsViewModel>()
.Columns(columns =>
{
 columns.Bound(o => o.Reason).Title("Reason").Hidden(JobsViewModel.Active);
}

 

0
Georgi
Telerik team
answered on 30 Nov 2018, 07:11 AM
Hello Rhett,

You could add a static property to the JobsViewModel and use it as in the provided configuration.

e.g.

// property
public static int Active { get; set; }
 
// column
 
columns.Bound(o => o.Reason).Title("Reason").Hidden(JobsViewModel.Active);


Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Tigga
Top achievements
Rank 1
Iron
answered on 15 Jul 2021, 06:25 PM

Better late than never I hope.

My application needs to reveal or hide a column based on the type of user. I have 3 types, Admin, Customer and User.  I want to show the Purchase Order number to the Admin and the Customer in my KendoGrid, but not the User type. So in this case the grid will either always or never show the column based on the credentials used. The beauty of the solution is that the column heading won't show up, nor will this take up room in the grid.  Most other ways I tried left gaping holes. The downside is that while the display won't have the data, the html certainly will and a sophisticated user could use the developer tools in the browser to glean the data - if they really want to. Of course, without a column heading they may not realize "other" data is present. 

Here's the code I used....

 @(Html.Kendo().Grid(Model)
    .Name("ShopScheduleGrid")
    .Groupable()
    .Columns(columns =>
    {
        columns.Bound(p => p.PONum)
              .Title("PO User").Filterable(true).Visible("Admin".Equals(@Session["myUserType"])).Width(140)
              .HtmlAttributes(new { style = "width:140px" });  
        columns.Bound(p => p.PONum)
              .Title("PO Admin").Filterable(true).Visible("User".Equals(@Session["myUserType"]))
              .Width(140).HtmlAttributes(new { style = "width:140px" }); 

...

In the HomeController at login time I set the session variable and retrieve it here.  I am not adding the visibility to bound class. 

Tags
Grid
Asked by
n/a
Top achievements
Rank 1
Answers by
Georgi
Telerik team
n/a
Top achievements
Rank 1
Tigga
Top achievements
Rank 1
Iron
Share this question
or