如何在 kendo.data.Datasource 上获得单一结果
Posted
技术标签:
【中文标题】如何在 kendo.data.Datasource 上获得单一结果【英文标题】:How to get Single result on kendo.data.Datasource 【发布时间】:2013-11-03 10:32:28 【问题描述】:我有这样的 Odata 结果
"odata.metadata":"https://localhost/DocTalkMobileWebApiOData/odata/$metadata#MasterPatient/@Element","PatUniqueId":"39e713db-6a0e-4e59-bf7b-033f4fc47ad5", "PatID":null,
"pat_lname":"White","pat_fname":"Peter","pat_mi":" ","pat_ssn":"270787655","pat_dob":"08/07/1973","pat_sex":"M","pat_status":null,"priInsID":2,"secInsID":1,"PCPID":1,"InternalDrID":1,"EXPID":1,"EXPDate":"","pat_phone":null,"isNew":true,"imported":true,"byWhom":"dt","lastUpdate":"2011-03-30T09:41:57.36","changeStamp":"AAAAAAAAIUE=","address":"","city":"","state":"","zip":"","currentMcp":"","currentVisitCount":-2,"otherId":"543674","pcpName":null,"hasChanges":true,"ProgramSource":null,"mrnID":"","createdBy":null,"createdDate":"2007-10-26T10:16:15","expLocation":null,"ethnicId":1,"prefLanguageId":1,"raceId":1
我试图通过 kendo.ui.datasource 得到这个结果:
newPatient = new kendo.data.DataSource(
type: 'odata', // <-- Include OData style params on query string.
transport:
read:
url: url + '/MasterPatient(guid\'00000000-0000-0000-0000-000000000000\')', // <-- Get data from here
dataType: "json" // <-- The default was "jsonp"
,
parameterMap: function (options, type)
var paramMap = kendo.data.transports.odata.parameterMap(options);
delete paramMap.$inlinecount; // <-- remove inlinecount parameter.
delete paramMap.$format; // <-- remove format parameter.
return paramMap;
,
schema:
data: function (data)
return data;
,
total: function (data)
return data['odata.count']
,
);
newPatient.fetch(function ()
kendo.bind($('#newPatientTab'), newPatient);
);
但不知道为什么它总是抛出错误:
Uncaught TypeError: Object [object global] has no method 'slice'
请帮助我。谢谢
【问题讨论】:
Single Result
对你意味着什么?无论 odata 查询如何,此 odata url 是否总是返回一个结果?
@Jayantha 是的,我的意思是这只是一条记录 :)
【参考方案1】:
在 Kendo UI 中,DataSource 仅适用于数组。如果您可以更改服务器响应以发送类似这样的内容
["odata.metadata":"https://localhost/DocTalkMobileWebApiOData/odata/$metadata#MasterPatient/@Element","PatUniqueId":"39e713db-6a0e-4e59-bf7b-033f4fc47ad5","PatID":null,"pat_lname":"White","pat_fname":"Peter","pat_mi":" ","pat_ssn":"270787655","pat_dob":"08/07/1973","pat_sex":"M","pat_status":null,"priInsID":2,"secInsID":1,"PCPID":1,"InternalDrID":1,"EXPID":1,"EXPDate":"","pat_phone":null,"isNew":true,"imported":true,"byWhom":"dt","lastUpdate":"2011-03-30T09:41:57.36","changeStamp":"AAAAAAAAIUE=","address":"","city":"","state":"","zip":"","currentMcp":"","currentVisitCount":-2,"otherId":"543674","pcpName":null,"hasChanges":true,"ProgramSource":null,"mrnID":"","createdBy":null,"createdDate":"2007-10-26T10:16:15","expLocation":null,"ethnicId":1,"prefLanguageId":1,"raceId":1]
然后它会正常工作。 注:它是数组格式。
或
您可以将单个对象包装到客户端的数组中,在模式的数据函数内部。
schema:
data: function(server-response)
return [server-response];
Kendo 团队应该把更多的时间放在好的文档上。
【讨论】:
是的,它现在工作正常。我找到了另一种实现方式: $.getJSON(url + '/MasterPatient(guid\'00000000-0000-0000-0000-000000000000\')', function (data) newPatient = new kendo.observable(data); kendo.bind($('#newPatientForm'), newPatient); ); 使用 guid 调用数据不是一个好主意。这不是通用的。 如果它解决了您的问题,您可以接受答案。不要让问题悬而未决。【参考方案2】:这意味着您没有使用支持的 odata 源。在这种情况下,如果您的后端不正确支持 odata,您需要在这里考虑是否真的需要来自客户端的 kendo odata 源。
查看来自 odata url http://services.odata.org/Northwind/Northwind.svc/?$format=json 的回复
它应该在value
字段中返回一个对象数组。
如果您无法更改支持的内容,您可以做的是在 Schema.data 函数中格式化数据
schema:
data: function (data)
return [data];
,
【讨论】:
以上是关于如何在 kendo.data.Datasource 上获得单一结果的主要内容,如果未能解决你的问题,请参考以下文章
怎样用js给Html.Telerik.Grid重新绑定数据源