Jammer
Jammer

Reputation: 10208

Kendo UI Grid in MVC with Conditional Custom Command Button

I have a KendoUI Grid I'm using an MVC web application, all working fine however I want to add a custom command button that is shown conditionally in the UI and simply executes a command on my controller passing it the required parameter.

columns.Command(command => command.Custom("UnlockAccount").SendDataKeys(true).Click())

The command is specified as above but I only want the button to show when the DataItems IsLocked property is true.

I also cannot figure out how to just call and method on the controller rather. I cannot find a demo of this on the Kendo site and not sure how to move this forward.

Upvotes: 4

Views: 12528

Answers (4)

FoxDeploy
FoxDeploy

Reputation: 13537

As of the December 2018 release of Kendo, you can now conditionally display custom buttons more easily, but it still relies on JavaScript to do its work, this function should be defined before your dataGrid or you'll run into issues.

function showCommand(dataItem) {
        console.log("determining to hide or show" + dataItem);
        // show the Edit button for the item with Status='New'
        if (dataItem.Status == 'New') {
            return true;
        }
        else {
            return false;
        }       
}

Then the code for the Grid.

.Columns (columns => {
columns.Command (
        command => command.Custom ("Approve")
        .Visible ("showCommand")
        .Click ("approveFunc")
    )
    .Width (100)
    .HeaderTemplate ("Actions")
})

Upvotes: 3

Apeman
Apeman

Reputation: 1

You can control custom command button visibility by Visible property.

columns.Command(command => command.Custom("UnlockAccount").SendDataKeys(true).Click().Visible("unlockAccountVisible"))

Visible property accepts JS function name and passes current dataItem as an argument. JS function that evaluates button visibility:

<script>
  function unlockAccountVisible(dataItem) {
  // show the UnlockAccount button only when data item property IsLocked == true
  return dataItem.IsLocked;
  }
</script>

Read more in Show Command Buttons Conditionally kendo-ui documentation article.

Upvotes: 0

Roland Schaer
Roland Schaer

Reputation: 1668

Here is a specific example for using client templates for conditional command buttons.

const string ShowUpdateButton = "#if (IsNetReversal == false) {#<a class='k-button k-button-icontext k-grid-edit' href='\\#'><span class='k-icon k-edit'></span>Update</a>#}#";
const string ShowReverseButton = "#if (IsNetReversal == false) {#<a class='k-button k-button-icontext k-grid-reverse' href='/JournalDetail/Reverse/#: ID #'  ><span class='k-icon k-reverse'></span>Reverse</a>#}#";
const string ShowDeleteButton = "#if (IsAdjustment == true) {#<a class='k-button k-button-icontext k-grid-delete' href='\\#'><span class='k-icon k-delete'></span>Delete</a>#}#";

You can do the template inline but I find it easier (particularly for multiple buttons) if you declare constants and then use string.format to concatenate them.

col.Template(o => o).ClientTemplate(string.Format("{0}{1}{2}", ShowUpdateButton, ShowDeleteButton, ShowReverseButton));

The upside is it will work with popup editor whereas jquery hacks will ignore the conditional status when a user cancels out of edit. A cancel from the popup editor will restore the grid row from the viewmodel or wherever Kendo stores it which results in button states from before any jquery/javascript hack. The method above will also auto-wire the standard commands since I copied their HTML output for the client template.

The downside is that if Kendo changes their pattern for command buttons the client template may fail. I tired several other methods besides this one and the downside to this method seems better than the other methods.

Note on Kendo Forums: As of the date of this post, they do not appear to allow people who do not pay for support to post to the forums so I would suggest posting questions here instead. They monitor Stack Overflow and in my experience they seem to answer questions more quickly here.

Upvotes: 4

Petur Subev
Petur Subev

Reputation: 20203

Use template column instead - via the ClientTemplate method.

Conditional templates are covered here and multiple times on the forums - the Command columns is not that flexible.

Upvotes: 3

Related Questions