我有一个kendo ui网格,在更改行事件中我获取行id并将其传递给另一个加载其他网格的函数。我试图简化操作以找出这样的错误。
HTML代码
<input type="button" id="load-first" value="Load 108" />
<input type="button" id="load-second" value="Load 92" />
的Javascript
$("#load-first").click(function(){
loadEmailGrid(108);
});
$("#load-second").click(function(){
loadEmailGrid(92);
});
function loadEmailGrid(salesRepsId) {
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "operations/get_emails_sales_reps.php?salesRepsId=" + salesRepsId,
type: "GET"
},
update: {
url: "operations/edit_email.php?salesRepsId=" + salesRepsId,
type: "POST",
complete: function (e) {
$("#email-grid").data("kendoGrid").dataSource.read();
}
},
destroy: {
url: "operations/delete_email.php",
type: "POST",
complete: function (e) {
$("#email-grid").data("kendoGrid").dataSource.read();
}
},
create: {
url: "operations/add_email.php?salesRepsId=" + salesRepsId,
type: "POST",
complete: function (e) {
$("#email-grid").data("kendoGrid").dataSource.read();
}
},
},
schema: {
data: "data",
total: "data.length", //total amount of records
model: {
id: "EmailId",
fields: {
EmailType: {
defaultValue: {
EmailTypeId: 2,
EmailTypeName: "Home"
}
},
EmailText: {
type: "string"
},
IsMainEmail: {
type: "boolean"
},
}
}
},
pageSize: 5,
});
//dataSource.sync();
$("#email-grid").kendoGrid({
dataSource: dataSource,
height: 250,
filterable: true,
sortable: true,
pageable: true,
reorderable: false,
groupable: false,
batch: true,
navigatable: true,
toolbar: ["create", "save", "cancel"],
editable: true,
columns: [{
field: "EmailType",
title: "Type",
editor: EmailTypeDropDownEditor,
template: "#=EmailType.EmailTypeName#",
filterable: {
extra: false,
field: "EmailType.EmailTypeName",
operators: {
string: {
startswith: "Starts with",
eq: "Is equal to",
neq: "Is not equal to"
}
}
}
}, {
field: "EmailText",
title: "Email",
}, {
field: "IsMainEmail",
title: "Main?",
width: 65,
template: function (e) {
if (e.IsMainEmail == true) {
return '<img align="center" src ="images/check-icon.png" />';
} else {
return '';
}
}
// hidden: true
}, {
command: "destroy",
title: " ",
width: 90
},
]
});
}
返回add_email.php
的示例[{ “EMAILID”:200}]
如果我用一个id加载网格,例如我单击Load 108按钮。添加操作非常有效。但是当我点击并在两个按钮之间混合时。即加载具有不同id的网格 add函数多次调用一个具有先前id,另一个使用单击的按钮id。更多按钮之间的混合点击,调用更多添加功能。
这是显示问题的link。
拜托,我该如何解决这个问题?我尝试了许多没有运气的东西
答案 0 :(得分:2)
您使用全局变量,因此混淆了数据源。
更改
dataSource = new kendo.data.DataSource({...
到
var dataSource = new kendo.data.DataSource({...
再试一次。
修改强>
尝试将此作为脚本代码。
http://jsfiddle.net/blackjim/9LHW5
您要做的主要事情是将网格的初始化与传输选项的更改分开。
例如:
function loadDatasourceWithId(salesRepsId){
var dataSourceOptions = dataSource.options; // find somehow the dataSource options
dataSourceOptions.transport.read.url = "operations/get_emails_sales_reps.php?salesRepsId="+salesRepsId;
dataSourceOptions.transport.update.url = "operations/edit_email.php?salesRepsId="+salesRepsId;
dataSourceOptions.transport.create.url = "operations/add_email.php?salesRepsId="+salesRepsId
datasource.read(); // read again to get new values to your dataSource
}