只是想知道为什么javascript的push方法会插入“index”
var agendaBatch=[];
for(var i=0; i<agendas.length; i++) {
var agenda = {
MeetingId: meetingId,
Title: agendas[i].title,
Description: agendas[i].description,
Remarks: "",
};
agendaBatch.push(agenda);
}
console.log(kendo.stringify(agendaBatch));
dataSourceAgenda.add(agendaBatch);
dataSourceAgenda.sync();
输出:
{"0":{"Title":"Agenda title","Description":"Agenda details","Remarks":""},
"1":{"Title":"Agenda title","Description":"Agenda details","Remarks":""}}
我期望此输出符合我的 Web API 参数要求
[{"Title":"Agenda title","Description":"Agenda details","Remarks":""},
{"Title":"Agenda title","Description":"Agenda details","Remarks":""}]
有什么建议我该怎么做?....
更新:刚刚发现,我正在使用 kendo ui 数据源,当我删除架构上的 Id 时我解决了这个问题
var dataSourceAgenda = new kendo.data.DataSource({
transport: {
type: "odata",
create: {
type: "POST",
url: API_URL + "/agendas",
contentType: "application/json; charset=utf-8",
dataType: 'json'
},
parameterMap: function (options, operation) {
if (operation !== "read" && options) {
return kendo.stringify(options);
}
}
},
schema: {
model: {
id: "Id", //I get my desired output if this is removed
fields: {
MeetingId: { type: "number" },
Title: { type: "string" },
Description: { type: "string" },
Remarks: { type: "string" },
}
},
}
});
但是我需要在其他函数中使用 Id 参数,无论如何我可以在不删除 kendo 数据源中的 Id 的情况下执行此操作。
更改了问题标题!
最佳答案
根据 Kendo UI DataSource ( here ) 的文档,add
方法接受一个 Object
而不是 Object 的
。array
此外,您将一个名为 Id
的字段用作 id
,该字段不在您的 model
字段中。
尝试执行以下操作:
var dataSourceAgenda = new kendo.data.DataSource({
transport: {
create : function (op) {
...
},
parameterMap: function (options, operation) {
if (operation !== "read" && options) {
return kendo.stringify(options.models);
}
}
},
batch : true,
schema : {
model: {
id : "Id", //I get my desired output if this is removed
fields: {
Id : { type: "number" },
MeetingId : { type: "number" },
Title : { type: "string" },
Description: { type: "string" },
Remarks : { type: "string" }
}
}
}
});
即:
- 将
batch
设置为true
,以便在您调用sync
时能够一次发送多个请求。 - 在
schema.model.fields
定义中定义Id
。 - 执行
options.models
的stringify
。
关于javascript - Kendo DataSource 在数据源同步上附加 Schema Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18352277/