Completed
Last Updated: 17 Mar 2021 07:21 by Vinodh
Release 2.9.0
Bernhard
Created on: 01 Aug 2019 05:10
Category: Grid
Type: Feature Request
52
Conditional command buttons (shown on condition based on model values and/or invoking CUD operations programmatically)

I need the ability to show or hide command buttons based on a row's property.

I am aware that I can just cancel commands, but in my opinion, a command button should not even be shown when the command can't or shouldn't be executed on a row.

Unfortunately, I was unable to find a way to access the "context" (the instance) in a TelerikGridCommandColumn the same way you're able to in a normal TelerikGridColumn.

---

ADMIN EDIT

You can Vote for and Follow this request for a follow up on providing the model as context to the command column: https://feedback.telerik.com/blazor/1461283-pass-the-model-context-to-command-button. At the moment, conditional command buttons are possible in a "normal" column through the grid state, the page above shows an example.

---

18 comments
Vinodh
Posted on: 17 Mar 2021 07:21

Hi there, I had the same problem. I did a workaround by doing an enable or disable on the button. I understand that hiding/showing is better but at least its better than nothing. I used OnRowRender to achieve this. Not the most elegant solution. 

Solution: 

<TelerikGrid Data="@DataModel"  Sortable="true" OnRowRender="@OnRowRenderHandler">
   <GridColumns>
       <GridColumn Field="@(nameof(Id))" Title="#" width="180px" />
       <GridColumn Field="@(nameof(Name))" Title="Name" />   
       <GridCommandColumn>
             <GridCommandButton Enabled="@enable" Icon="@IconName.Edit" ShowInEdit="false" OnClick="@EditOrderDetail">Edit</GridCommandButton>
       </GridCommandColumn>
   </GridColumns>
</TelerikGrid>

 

@code{

   public bool enable = true;

   private void OnRowRenderHandler(GridRowRenderEventArgs args)
   {
            var item = args.Item as Data;

            enable = Data.Status != "Some Condition";
        }

}

 

 

 

 

ADMIN
Marin Bratanov
Posted on: 15 Mar 2021 07:48

Hi Max,

You can Vote for and Follow this request for a follow up on providing the model as context to the command column: https://feedback.telerik.com/blazor/1461283-pass-the-model-context-to-command-button. At the moment, conditional command buttons are possible in a "normal" column through the grid state, the page above shows an example. I've also added your Vote to it on your behalf to raise its priority.

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Max
Posted on: 14 Mar 2021 19:38
I am looking for this feature too.
Coen
Posted on: 08 Sep 2020 09:40
I'm looking for this as well.
ADMIN
Marin Bratanov
Posted on: 09 Apr 2020 16:55

Hello Christian,

I made a public request for this so you can Follow it. It offers a solution and some explanations on why this is not possible at the moment. Here's the link: https://feedback.telerik.com/blazor/1461283-pass-the-model-context-to-command-button

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Christian
Posted on: 09 Apr 2020 14:22

Hi Marin,

 

just to clearify this. It's not yet possible to show a button based on a condition yet, is it?

I have seen the updates with the grid state which allows sorting and filtering and so on.

However, something like this:

<GridCommandColumn>
@{
context as MyModel;
if(context.ShowButton)
{
<GridCommandButton>Todo</GridCommandButton>
}
}
</GridCommandColumn>

 

is not yet possible. Is this correct?

 

Best regards,

Christian

ADMIN
Marin Bratanov
Posted on: 18 Mar 2020 15:20

Hello everyone,

Most of these are now possible through the grid state from any button: https://docs.telerik.com/blazor-ui/components/grid/state

In subsequent releases we will be reviewing passing a context to the command column so you can add conditions there.

I would encourage you to take a peek at the state to see if it will suit your needs so we can discuss if anything else needs to be done.

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor
Sam
Posted on: 30 Jan 2020 21:19
This would be incredibly helpful. Just to reinforce what some of the other developers in here have said, I would also be perfectly content to trigger the commands programmatically. It would just be nice to stop having to use clunky workarounds.
Christian
Posted on: 19 Dec 2019 07:00
This is something we would need as well :)
ADMIN
Marin Bratanov
Posted on: 09 Dec 2019 09:30

Hi René,

Allowing something like that is the point of this feature request, so it's not possible at the moment. Whether it will be a method on the grid that will change the state of the rows, or allowing the context of the row to pass down to the command column is yet to be determined. The idea is to allow you to handle the command buttons like random buttons - showing and hiding them conditionally, perhaps even letting you put them in other templates.

For the time being, you could achieve something like that through a custom editing form without the built-in command buttons - either through a row template where you'll toggle the contents based on a flag (something similar is done here (in the Master-Detail section near the end) ; or through any custom edit form outside of the grid (see here and here).

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor
René
Posted on: 09 Dec 2019 09:08
Is there a way to trigger edit/delete for a row programatically?  Then I could conditionally show my own buttons in a normal column and in the handler of those buttons trigger the telerik edit/delete actions.
ADMIN
Marin Bratanov
Posted on: 01 Oct 2019 12:42

Hello Anthony,

The priorities of various issues and enhancements depend largely on the demand and at this point new components like a scheduler and mode inputs (like combo/autocomplete) are higher on the list. At this point I can't say when this will get implemented, as it will require a non-trivial research and perhaps even a breaking change. What I can suggest is that you click the Follow button to get email notifications for status updates. We will also update the page to list the release when this will be available in once we know.

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor
Anthony
Posted on: 01 Oct 2019 11:50

Hi Marin,

This is certainly something we would find useful. Any update on when this is likely to be implemented?

 

Kind regards,

Anthony.

 

Bernhard
Posted on: 01 Aug 2019 07:10

Alright, I see.

Thanks for taking a look!

ADMIN
Marin Bratanov
Posted on: 01 Aug 2019 07:05
Hello Bernhard,

I renamed the item to make it more generic, because I don't know yet how would that be done best. 

Passing a "context" would require that the buttons be in a template, which is a breaking change. 

The other problem this would pose is that those buttons need to somehow be registered with the grid, and since this is now a generic RenderFragment, there is no automatic way for the grid to do that that I can see off the top of my head (this does not mean it's impossible, of course).

At the moment, I am thinking that perhaps we should extend the grid with methods that you can use to invoke the CUD operations, so you can call them from anywhere, and perhaps that would let you just use a "regular" column instead of a command column for such conditional logic.

I am going to defer to a more in-depth investigation when it is performed to decide on how this can be done best.


Regards,
Marin Bratanov
Progress Telerik UI for Blazor
Bernhard
Posted on: 01 Aug 2019 06:06

Hi Marin,

Thank you for the quick reply.

 

The suggestion in your code snippet is how I instinctively tried to do this in the first place, so it would absolutely suit my needs.

That being said, if you think there's a solution that's more elegant or consistent with other implementations, I wouldn't mind if it was implemented in another way either.

ADMIN
Marin Bratanov
Posted on: 01 Aug 2019 05:59
Hi Bernhard,

I agree that conditional commands would be nice to have.

Would defining the command buttons in a fashion like the snippet below suit your needs? I am asking so I get a better idea of what you would like to have and use so we can see how we could accommodate it.

 

<TelerikGridCommandColumn>
    @{
        if ((context as SampleData).ID > 2)
        {
            <TelerikGridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</TelerikGridCommandButton>
            <TelerikGridCommandButton Command="Edit" Icon="edit">Edit</TelerikGridCommandButton>
            <TelerikGridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</TelerikGridCommandButton>
        }
        if ((context as SampleData).ID > 5)
        {
            <TelerikGridCommandButton Command="Delete" Icon="delete">Delete</TelerikGridCommandButton>
        }
    }
</TelerikGridCommandColumn>

If not, would you want to be able to define command buttons in the template of a column/row? If yes, how would you want to be able to define those? Would having methods on the grid like .EditItem(T myCurrentModel), .DeleteItem(T myCurrentModel), .CancelEdit(T myCurrentModel), .SaveItem(T myCurrentModel) suit your needs, so you could call them from an arbitrary place in your code?

Regards,

Marin Bratanov
Progress Telerik UI for Blazor
Bernhard
Posted on: 01 Aug 2019 05:20

I just realized that in a normal TelerikGridColumn can only be accessed inside the pre-defined Template tags.

Any possibility to show or hide a command button based on a row's property would of course be sufficient.