I have a grid with a custom command and a client-side function specified for the grid's DataBound event. In the DataBound function, I hide the custom command on rows where a particular column has a certain value. My code is successfully showing the custom button only on the appropriate rows when the grid is displayed. I've also specified a custom popup editor for the grid.
Skeleton of the grid code is:
@(Html.Kendo().Grid<Models.Task>()
.Name("gridTask")
.Columns(columns =>
{
columns.Bound(task => task.Name);
columns.Bound(task => task.TaskStatus);
columns.Command(cmd =>
{
cmd.Edit();
cmd.Custom("Show Survey").Click("ShowSurvey");
});
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.ServerOperation(false)
.Model(model => model.Id(m => m.Task_ID))
.Read(read => read.Action("ReadTask", "TaskPartial"))
.Update(update => update.Action("UpdateTask", "TaskPartial"))
.Create(update => update.Action("CreateTask", "TaskPartial"))
)
.Events(e =>
{
e.Cancel("EditCancel");
e.DataBound("TaskDataBound");
})
.Pageable()
.Sortable()
.Editable(ed => {
ed.Mode(GridEditMode.PopUp);
ed.TemplateName("TaskEditor");
ed.Window(win => win.Title("Task Editor"));
})
)
And the DataBound function is:
function TaskDataBound(e) {
// Check the Show Survey button in each task row. Remove the button if the row has a certain value.
$("[id^='gridTask'] tbody tr .k-grid-ShowSurvey").each(function () {
var currentDataItem = $("[id^='gridTask']").data("kendoGrid").dataItem($(this).closest("tr"));
//Check the current dataItem to see if the button should be removed
if (currentDataItem.testValue == 0) {
$(this).remove();
}
})
}
Now suppose I click Edit on a row which does not show the custom button, and the edits I make do not effect the column determining the visibility of the custom button. When I click Update in the popup editor, the screen is updated as expected. The popup goes away, the updated data is shown, and the grid row still does not show the button.
However, when I click Cancel in the popup editor, the popup goes away but the custom command incorrectly reappears on the grid row, even though the column determining the visibility of the custom command hasn't changed. Why is the custom command mistakenly re-appearing?
I've tried specifying a client-side function for the Cancel event as follows to remove the custom button upon the cancel. Even though I've traced the code to confirm my cancel function is called and the remove() command is hit and executed, the Custom button still comes back.
function EditCancel(e) {
// Check the Show Survey button in each task row. Remove the button if the row has a certain value.
$("[id^='gridTask'] tbody tr").each(function () {
var currentDataItem = $("[id^='gridtask']").data("kendoGrid").dataItem($(this));
//Check the current dataItem to see if the button should be removed
if (currentDataItem.testValue == 0) {
$(this).find(".k-grid-ShowSurvey").remove();
}
})
}