KendoUI Scheduler层级分组:资源模板中如何访问parentValue
在Kendo Scheduler资源模板中能否访问parentValue的值?
我为Scheduler的资源创建了模板,但目前仅能访问name、text等字段,parentValue显示为undefined。
模板代码
<script id="groupHeaderTemplate" type="text/x-kendo-template"> <span>name: #=name#</span><br/> <span>text: #=text#</span><br/> <span>title: #=title#</span><br/> <span>field: #=field#</span><br/> <span>value: #=value#</span><br/> <span>parentValue: #=parentValue#</span><br/> // 此处为undefined </script>
数据源配置
childResourceDataSource = new kendo.data.DataSource({ type: "webapi", transport: { read: "/turnazione/gestioneturni/OnGetDipendentiByPuntoVenditaResources", }, serverFiltering: true, schema: { model: { id: "value", fields: { value: { type: "number" }, text: { type: "text" }, parentValue: { type: "text" }, } } }, sort: { field: "text", dir: "asc" }, });
接口实际返回数据
[ { "value": 3, "text": "Alberto Giacomelli", "parentData": 55 }, { "value": 5, "text": "Roberto Cacciatori", "parentData": 55 }, { "value": 6, "text": "Giacomo Giordano", "parentData": 99 }, { "value": 8, "text": "alessio pinato", "parentData": 99 } ]
问题原因与解决方法
问题核心是字段映射不匹配:你在数据源schema中定义的字段是parentValue,但接口实际返回的字段名是parentData,导致Kendo无法正确解析该字段,模板中自然取不到值。
有两种修正方式:
- 修改数据源映射:在schema的字段定义中添加
from属性,将接口返回的parentData映射到parentValue:
schema: { model: { id: "value", fields: { value: { type: "number" }, text: { type: "text" }, parentValue: { type: "text", from: "parentData" // 映射接口字段到模型字段 }, } } },
- 统一字段名:要么把schema中的字段名改为
parentData,同时模板中也改为#=parentData#;要么让接口返回的字段名改为parentValue。
另外,如果是层级资源,需确保在Scheduler的资源配置中指定parentIdField,这样Kendo才能正确识别层级关系,同时保证模板能正常访问父级字段:
resources: [ { field: "resourceId", name: "Dipendenti", dataSource: childResourceDataSource, title: "Dipendenti", parentIdField: "parentValue" // 对应模型中的父ID字段名 } ]
完成以上修改后,模板中就能正常访问到父级字段的值了。
内容的提问来源于stack exchange,提问作者pinale
相关产品推荐
相关产品推荐

