Kendo ui 服务器响应如何满足dataSource.transport.create?
我使用的是kendoGrid,远程数据来自php 这是网格的一个示例:Kendo ui 服务器响应如何满足dataSource.transport.create?,kendo-ui,kendo-grid,Kendo Ui,Kendo Grid,我使用的是kendoGrid,远程数据来自php 这是网格的一个示例: $('#grid').kendoGrid({ columns: [ {field: 'Agent_Code', title: 'Agent code'}, {field: 'Agent_Name', title: 'Agent name'}, {field: 'Short_Name', title: 'Short name'}, {field: 'Mor
$('#grid').kendoGrid({
columns: [
{field: 'Agent_Code', title: 'Agent code'},
{field: 'Agent_Name', title: 'Agent name'},
{field: 'Short_Name', title: 'Short name'},
{field: 'More_Info', title: 'More info'},
{command: {name: 'edit'}}
],
toolbar: [
{name: 'create'}
],
dataSource: {
transport: {
create: {
url: 'ajax/create.php',
type: 'POST',
dataType: 'json'
},
read: {
url: 'ajax/read.php',
type: 'POST',
dataType: 'json'
},
update: {
url: 'ajax/update.php',
type: 'POST',
dataType: 'json'
},
destroy: {
},
},
schema: {
type: 'json',
data: 'data.results',
model: {
id: 'Agent_Code',
fields: {
Agent_Code: {editable: true, nullable: false, type: 'string'},
Agent_Name: {editable: true, nullable: false, type: 'string'},
Short_Name: {editable: true, nullable: false, type: 'string'},
More_Info: {editable: true, nullable: false, type: 'string'}
}
}
}
}
});
网格填充正确,因为我将“dataSource.schema.data”设置为“data.results”,所以网格知道如何在服务器响应中定位数据。
“transport.read”的服务器“json”响应结构为:
我的问题是,我不知道如何对“dataSource.transport.create”和“dataSource.transport.update”执行此操作。服务器正确接收数据,将其存储到数据库,并使用以下命令响应:
{
code: 'ok',
text: 'operation succeeded'
}
但是网格不明白一切都是好的,请更新网格
这里少了什么
关于您需要修改您的
模式
定义,添加错误
schema : {
type : 'json',
data : 'data.results',
model: {
id : 'Agent_Code',
fields: {
Agent_Code: {editable: true, nullable: false, type: 'string'},
Agent_Name: {editable: true, nullable: false, type: 'string'},
Short_Name: {editable: true, nullable: false, type: 'string'},
More_Info : {editable: true, nullable: false, type: 'string'}
}
},
errors : function(a) {
return a.code !== "ok";
}
}
此errors
函数根据code
返回true
或false
此外,由于您在更新或插入记录时似乎不返回该记录,因此应修改数据
,使其在更新
或创建
时不尝试查找该记录,而是在读取
时查找该记录
您可以将其定义为:
data : function (a) {
if (a.data && a.data.results){
return a.data.results;
} else {
return null;
}
},
现在,模式定义是:
schema : {
type : 'json',
data : function (a) {
if (a.data && a.data.results){
return a.data.results;
} else {
return null;
}
},
model: {
id : 'Agent_Code',
fields: {
Agent_Code: {editable: true, nullable: false, type: 'string'},
Agent_Name: {editable: true, nullable: false, type: 'string'},
Short_Name: {editable: true, nullable: false, type: 'string'},
More_Info : {editable: true, nullable: false, type: 'string'}
}
},
errors : function(a) {
return a.code !== "ok";
}
}
注意:如果总是返回ok
,则可以避免定义错误
,唯一重要的部分是重新定义数据
编辑:您可以在KendoUI网站上阅读更多内容:
错误
和错误
我不明白为什么if语句包含a.data&&。另外,你能给我指一下关于这个问题的文档吗?我在kendoui网站上没有找到任何文档。我检查
a.data
,因为当服务器将结果发送到update
时,JSON不包括它,但无论如何都会执行它,以防止崩溃。然后我检查a.data.results
,因为这里是信息的实际来源。是的,我可能只检查了第一个条件,希望消息格式正确,但我更喜欢这样做,因为它更安全。我将更新我的答案,包括对剑道UI文档的引用。
schema : {
type : 'json',
data : function (a) {
if (a.data && a.data.results){
return a.data.results;
} else {
return null;
}
},
model: {
id : 'Agent_Code',
fields: {
Agent_Code: {editable: true, nullable: false, type: 'string'},
Agent_Name: {editable: true, nullable: false, type: 'string'},
Short_Name: {editable: true, nullable: false, type: 'string'},
More_Info : {editable: true, nullable: false, type: 'string'}
}
},
errors : function(a) {
return a.code !== "ok";
}
}