BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
@(Html.EJ().Grid<object>("Grid")
.Datasource(ds => ds.Json(Model).UpdateURL("/OrderTables/Update")
.InsertURL("/OrderTables/Insert").RemoveURL("/OrderTables/Remove").Adaptor(AdaptorType.RemoteSaveAdaptor))
.AllowPaging(true)
.ClientSideEvents(eve => eve.ActionBegin("begin"))
...
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).IsIdentity(true).TextAlign(TextAlign.Right).Width(75).Add();
...
}))
<script type="text/javascript">
function begin(args) {
function begin(args) {
if (args.requestType == "save") {
var index = this.model.dataSource.dataSource.json.length - 1;
args.data.OrderID = this.model.dataSource.dataSource.json[index].OrderID + 1
}
}</script> |
Hello, thank you for your reply
"The above one will works when you using removeSaveAdaptor in grid. "
I don't really know how that "removeSaveAdaptor" works. I don't use it, and I haven't even seen it before. I think that "local data in grid" is what we are using here.
You said that if we are working with the local data in the grid, I needed to access datasource using this.model.datasource. Well, how would I achieve this line of code: args.data.OrderID = this.model.dataSource.dataSource.json[index].OrderID + 1 ??
I need to simply do something like args.data.OrderID = this.model.datasource.OrderId + 1; ?? this.model.datasource.OrderId selects the correct row and cell to increment or I need and index too?
I don't really know how the js structure should be written when we are working with local data in the grid.
Ok I think I figured it out. The code should be:
function ActionBegin(args) {
if (args.requestType == "save") {
var index = this.model.dataSource.length - 1;
if (index == -1) {
args.data.InfraccionId = 1;
}
else {
args.data.InfraccionId = this.model.dataSource[index].InfraccionId + 1
}
}
Now I have another problem. This code will execute every time you save a row. That means that it will assign a new primary key also when you edit the record, and this destroy the logic and the grid starts behave crazy when you edit. Which is the correct workaround for this?
I think I solved everything with some logic. Here's my js code;
var RecordJustAdded = false;
var InfraccionData = [
{ id: "Primera", text: "Primera" },
{ id: "Segunda", text: "Segunda" },
{ id: "Tercera", text: "Tercera" },
{ id: "Cuarta", text: "Cuarta" },
{ id: "Quinta", text: "Quinta" },
{ id: "Sexta", text: "Sexta" },
{ id: "Septima", text: "Séptima" },
{ id: "Octava", text: "Octava" },
{ id: "Novena", text: "Novena" },
{ id: "Decima", text: "Décima" }
];
function ActionBegin(args) {
if (args.requestType == "save") {
if (RecordJustAdded) {
var index = this.model.dataSource.length - 1;
if (index == -1) {
args.data.InfraccionId = 1;
}
else {
args.data.InfraccionId = this.model.dataSource[index].InfraccionId + 1
}
}
}
else if (args.requestType == "cancel") {
RecordJustAdded = false
}
}
function ActionCompleted(args) {
if (args.requestType == "add") {
$("#" + this._id + "Infraccion").ejDropDownList({
dataSource: InfraccionData,
field: { text: "text", value: "text" },
width: "100%"
});
RecordJustAdded = true;
}
if (args.requestType == "save" && RecordJustAdded) {
this.model.dataSource.shift();// Remove the newly added record from first position
this.model.dataSource.push(args.data)// Push the newly added record in data source
this.refreshContent();
RecordJustAdded = false;
}
}
I have the RecordJustAdded declaration to control where to increment the primary key. I do it only when you click the add button and save the row. I do not increment the primary key if you just save it.
Anyway, I don't think this is the prettiest way to do it. So when you have time, suggest me a better way. Thank you!
@(Html.EJ().Grid<object>("Grid")
...
.ClientSideEvents(eve => eve.ActionBegin("begin").ToolbarClick("click"))
...
.Columns(col =>
{
...
}))
<script type="text/javascript">
var flag;
function begin(args) {
if (args.requestType == "save" && flag) {
var index = this.model.dataSource.length - 1;
args.data.OrderID = this.model.dataSource[index].OrderID + 1
flag = false;
}
}
function click(args) {
if (args.itemName == "Add") { //when clicking the add icon in toolbar assigning the global variable as true.
flag = true;
}
}
</script> |