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

[Solved] Show command button conditionally

6 Answers 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Datamex
Top achievements
Rank 2
Datamex asked on 17 Apr 2011, 09:29 PM
Hello,

I've added a custom command button to the grid as follows:

                 columns.Command(commands =>
                 {
                     columns.Bound(c => c.UserName)
                      .ClientTemplate("<a href=\"#\" onclick=\"javascript: LockAndRefresh('<#= UserName #>'); return false;\" class=\"t-button\">Unlock</a>")
                      .Title(String.Empty)
                      .Width(103);
                   commands.Delete().ButtonType(GridButtonType.ImageAndText);
                 }).Width(103);

Now I want to show this column only when the property 'IsLockedOut' on the item is true. So I need to do something like

.Hidden(IsLockedOut == false)

But I don't know how to get to the IsLockedOut property in that scope?

6 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 18 Apr 2011, 01:03 PM
Hello,

I suspect you want to hide the button in the cell, as you cannot hide the entire column base on a current record's value due to the fact that usually  this value is different for each record therefore it can be either visible or not. If this is the case you may hide the button with similar to the following code:

.ClientTemplate("<a href=\"#\" onclick=\"javascript: LockAndRefresh('<#= UserName #>'); return false;\" class=\"t-button <#= IsLockedOut? 'hidden' : '' #>\">Unlock</a>")

where hidden css class has following declaration:

.hidden
{
    display:none;
}


Regards,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Datamex
Top achievements
Rank 2
answered on 18 Apr 2011, 01:29 PM
Sounds like a solution, but isn't there anything I can do to prevent it from rendering alltogether? Rendering it and then hiding seems a bit dirty.
0
Rosen
Telerik team
answered on 18 Apr 2011, 03:34 PM
Hi,

You may expose a single property of the ViewModel which to determine if the columns should be visible or not.

All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Datamex
Top achievements
Rank 2
answered on 18 Apr 2011, 03:42 PM
That would hide the entire column, that's not what I want. I want to only prevent some of the buttons from rendering alltogether.
0
Rosen
Telerik team
answered on 19 Apr 2011, 08:36 AM
Hi Datamex,

I'm afraid that this is not possible if you are using ajax binding.

All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Morgan
Top achievements
Rank 1
answered on 06 Apr 2012, 01:12 PM
One tip when trying the solution indicated here.  The styles for the 't-button' class will cascade and override your hidden class in some cases.  However, if you mark your style with the '!important' attribute as shown here, it will then work as intended.

.hidden
{
    display: none !important;
}

As for the solution being a little dirty... I'll paraphrase.. if it's dirty, and it works, it's not dirty ;)

Morgan
Tags
Grid
Asked by
Datamex
Top achievements
Rank 2
Answers by
Rosen
Telerik team
Datamex
Top achievements
Rank 2
Morgan
Top achievements
Rank 1
Share this question
or