gpt4 book ai didi

kendo-ui - 服务器响应如何满足dataSource.transport.create?

转载 作者:行者123 更新时间:2023-12-02 00:54:07 25 4
gpt4 key购买 nike

我正在使用 kendoGrid,我的远程数据来自 php。

这是网格的示例:

$('#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”响应结构是:

{
code: 'ok',
data: {
id: [' ', ' '],
results: [
{Agent_Code: ' ', Agent_Name: ' ', Short_Name: ' ', More_Info: ' '},
{Agent_Code: ' ', Agent_Name: ' ', Short_Name: ' ', More_Info: ' '}
]
}
}

我的问题是我不明白如何为“dataSource.transport.create”和“dataSource.transport.update”执行此操作,服务器正确接收数据,将其存储到数据库并响应:

{
code: 'ok',
text: 'operation succeeded'
}

但网格不明白一切正常并更新网格。

这里缺少什么?

问候

最佳答案

您需要修改您的schema定义,向其中添加errors

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函数返回truefalse,具体取决于code

此外,由于您在更新或插入记录时似乎没有返回记录,因此您应该修改 data 以在 update创建,但在读取时找到它。

您可以将其定义为:

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,那么您可能会避免定义errors,唯一重要的部分是重新定义data.

编辑:您可以在 KendoUI 网站中阅读更多内容:

  1. 关于使用 here特别是关于使用 errorserror here .
  2. 您还可以查看DataSource.schema.errors文档和 DataSource.error]( http://docs.kendoui.com/api/framework/datasource#events-error ) 事件处理程序文档。

关于kendo-ui - 服务器响应如何满足dataSource.transport.create?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20583702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com