// Global variable= to store the primary key value of the last edited row
// Based on this value the only row(last edited row) to be edited can be determined
var editPrimaryKeyId;
var initialRender = true;
// Grid’s dataBound event handler
function onDataBound(args) {
if (initialRender) {
// The Grid data source’s primary key value is stored in global variable
editPrimaryKeyId = this.dataSource[0]["OrderID"];
// ‘Add’ toolbar item is enabled/disabled based on data source length
if (this.dataSource.length === 0) {
this.toolbarModule.toolbar.enableItems(0, true);
} else {
this.toolbarModule.toolbar.enableItems(0, false);
}
initialRender = false;
}
} |
// Grid’s actionComplete event handler
function actionComplete(args) {
if (args.requestType === "save" || args.requestType === "delete") {
var proxy = this;
// ‘Add’ toolbar item is enabled/disabled based on data source length
(this.dataSource.length === 0) ? setTimeout(function () { proxy.toolbarModule.toolbar.enableItems(0, true) }, 2) : setTimeout(function () { proxy.toolbarModule.toolbar.enableItems(0, false) }, 2);
}
} |
// Grid’s actionBegin event handler
function actionBegin(args) {
// Executed only if data source is not empty
if (this.dataSource.length !== 0) {
if (args.requestType === "beginEdit" && args.rowData["OrderID"] !== editPrimaryKeyId) {
// Edit operation is cancelled if it is not the latest added row
args.cancel = true;
}
if (args.action !== "add" && args.requestType === "save") {
// Deep copy of the edited data
var newData = Object.assign({}, args.data);
// Save operation is cancelled and edit is closed
args.cancel = true;
this.closeEdit();
// Randomly generated number for the new data’s primary key
newData["OrderID"] = newData["OrderID"] + parseInt(Math.random() * 100);
// The latest row’s primary key is updated in the global variable
editPrimaryKeyId = newData["OrderID"];
// Edited data is added as new record
this.addRecord(newData);
}
} else if (this.dataSource.length === 0 && args.action === "add" && args.requestType === "save") {
// The newly added row’s primary key is updated in the global variable
var newData = Object.assign({}, args.data);
editPrimaryKeyId = newData["OrderID"];
}
} |
function onDataBound(args) {
if (initialRender) {
editPrimaryKeyId = this.currentViewData[0]["OrderID"];
if (this.currentViewData.length === 0) {
this.toolbarModule.toolbar.enableItems(0, true);
} else {
this.toolbarModule.toolbar.enableItems(0, false);
}
initialRender = false;
}
}
function actionComplete(args) {
if (args.requestType === "save" || args.requestType === "delete" || args.requestType === "cancel") {
var proxy = this;
(this.currentViewData.length === 0) ? setTimeout(function () { proxy.toolbarModule.toolbar.enableItems(0, true) }, 2) : setTimeout(function () { proxy.toolbarModule.toolbar.enableItems(0, false) }, 2);
}
}
function actionBegin(args) {
if (this.currentViewData.length !== 0) {
if (args.requestType === "beginEdit" && args.rowData["OrderID"] !== editPrimaryKeyId) {
args.cancel = true;
}
if (args.action !== "add" && args.requestType === "save") {
var newData = Object.assign({}, args.data);
args.cancel = true;
this.closeEdit();
newData["OrderID"] = newData["OrderID"] + parseInt(Math.random() * 100);
editPrimaryKeyId = newData["OrderID"];
this.addRecord(newData);
}
} else if (this.currentViewData.length === 0 && args.action === "add" && args.requestType === "save") {
var newData = Object.assign({}, args.data);
editPrimaryKeyId = newData["OrderID"];
}
} |