I am trying to disable my edit and delete commands conditionally. I have found a couple of resources on these forums but none of them work in me scenario. I attempted to use the .Visible() function of the command cell; however, this approach is throwing an error in the browser console. So, I went a different direction and used the .Template() for my command. This is also not working because I am not getting the data item for the row in my template handler. Here is where I am at.
@(Html.Kendo().Grid<DocumentVM>()
.Name("Documents")
.Columns(columns =>
{
columns.Bound(f => f.Title)
.ClientTemplate("<a href='" +
Url.Action("DisplayFile", "Documents") + "/#=Id#' target='_blank'><i class='k-icon #:FileIcon#'></i> #=Title#</a>"
);
columns.ForeignKey(f => f.DocumentTypesId, (System.Collections.IEnumerable)ViewData["DocTypes"], "Id", "Description");
columns.Bound(f => f.CreatedDate).Title("Date Uploaded");
columns.Bound(f => f.CreatedBy).Title("Uploaded By");
columns.Command(command => {
command.Edit().Template("#= EditableTemplate(data) #");
command.Destroy().Template("#= DeleteTemplate(data) #");
});
...
)
function EditableTemplate(data) {
if (data.Editable) {
return "<a role='button' class='k-button k-button-icontext k-grid-edit' href='#'><span class='k-icon k-i-edit'></span>Edit</a>"
}
else {
return "<a role='button' class='k-button k-button-icontext k-grid-edit k-state-disabled' href='#'><span style='color: green;' class='k-icon k-i-check'></span>Edit</a>"
}
}
function DeleteTemplate(data) {
var currentUser = $("#CurrentUser").val();
var userIsAdmin = $("#UserIsAdmin").val();
if (data.Editable === true && currentUser === data.CreatedBy) {
return "<a role='button' class='k-button k-button-icontext k-grid-delete' href='#'><span class='k-icon k-i-close'></span>Delete</a>"
}
else {
return "<a role='button' class='k-button k-button-icontext k-grid-delete k-state-disabled' href='#'><span class='k-icon k-i-close'></span>Delete</a>"
}
}
For full disclosure, I have also attempted to retrieve the data item via Javascript in my handler methods but I can't seem to find a solution to identify which row is triggering the method.