Handle Duplicate Rows In Kendo Grid

Kasusnya ketika sesudah menambahkan baris baru di grid. Ditambahkan sebuah baris baru ke grid, tapi baris baru batal ditambahkan. Hal yang terjadi adalah, muncul duplikasi baris-baris yang sudah ada di grid.

Untuk mengatasi masalah tersebut gunakan barisan kode dibawah ini:
[code language=”javascript”]
//Pada model tambahkan field IdGrid untuk id di grid client side
public class DetailModel
{
public int Id { get; set; }

public int IdGrid { get; set; }

public string Name { get; set; }
}
[/code]

[code language=”javascript”]
//Ketika memanggil fungsi edit di controller tambahkan kode ini:
public class MasterModel
{
public List<DetailModel> DetailList { get; set; }

public MasterModel { classMaster dbItem }
{
DetailList = new DetailModel().MapList(dbItem.details.ToList());

//set unique id for grid detail auto increment
int idGrid = 1;
foreach(DetailModel model in DetailList)
{
model.IdGrid = idGrid;
++idGrid;
}
//
}
}
[/code]

[code language=”javascript”]
//set dataSource and variable to count item in dataSource
var ds = [];
//fill ds if data for grid is exist
fillDataSourceGrid(ds);
//
var counterGrid = ds.length;

function fillDataSourceGrid(data){
//insert code here
}

//set this function on kendo dataSource
data: ds,
schema: {
model: {
id: ‘IdGrid’,
fields: {
Id: { type: "number" },
Name: { type: "string" },
}
}
},

transport: {
read: function (o) {
o.success(initialDataPic);
},
create: function (o) {
//set IdGrid auto increment
var item = o.data;
counterGrid++;
item.IdGrid = counterGrid;
o.success(item);
},
update: function (o) {
o.success();
},
destroy: function (o) {
o.success();
}
},[/code]

Yosef Sukianto has written 36 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>