Due to the requirements, I had to implement the grid cells so that some of them are always in edit mode (input) and that the grid will fire an ajax call to the API server whenever user finishes updating cell value.
Here's what I have:
Grid DataSource and event handlers
.Events(e => e
.DataBound("onDataBound")
.DataBinding("onDataBinding")
)
.DataSource(dataSource => dataSource
.Ajax()
.Events(e => e.Change("onDataChange"))
.Read(read => read.Action("action", "controller").Data("GetParams"))
.Model(mod => mod.Id(m => m.id))
.PageSize(100)
)
Click handler (get new data based on currently selected options)
$("#btn").click(function () {
$("#Grid").data("kendoGrid").dataSource.read(GetParams());
}); // end click
onDataChange, onDataBinding and onDataBound
function onDataChange(e) {
if (e.items.length == 1) {
var updatedCell = e.items[0];
var queryString = "?id="+updatedCell.id+"&field="+e.field+"&val="+updatedCell[e.field];
$.ajax({
headers: { 'RequestVerificationToken': '@Html.TokenHeaderValue()' },
success: function (response) {
var resObj = response.toString();
if (resObj == "success") popupNotification.show("Auto Save Success!", "error");
else popupNotification.show("Auto Save Error", "error");
},
fail: function (response) {
popupNotification.show("Auto Save Error", "error");
}
});// end ajax
}
}
function onDataBinding(arg) {
var rows = this.tbody.children();
var dataItems = this.dataSource.view();
for (var i = 0; i < rows.length; i++) {
kendo.unbind(rows[i]);
}
}
function onDataBound(arg) {
var rows = this.tbody.children();
var dataItems = this.dataSource.view();
for (var i = 0; i < dataItems.length; i++) {
var temp = dataItems[i];
if (temp["RecordTypeID"] == 1 || temp["RecordTypeID"] == 3)
{
kendo.bind(rows[i], dataItems[i]);
}
}
generateChart(PopulateSummaryTable());
}
What happens right now is whenever the dataSource.read(GetParams()) gets called for the first time, the following things happen in this particular order:
onDataBinding
onDataBound
onDataChange
which is fine. But if the button is not clicked (which calls dataSource.read(GetParams())), and the user simply edits currently displayed cells, I'm hoping only onDataChange gets called, but it turns out that onDataBinding and onDataBound get called again, and then onDataChange.
Is it possible to prevent onDataBinding and onDataBound from being called more than once?
Please see attached for the UI