剑道 UI 下拉列表数据绑定值

xli*_*inc 4 javascript kendo-ui kendo-mobile icenium kendo-mvvm

我在列表视图中使用 Kendo UI 下拉列表

<ul data-role="listview" id="participants-listview" data-style="inset" data-template="participants-listview-template" data-bind="source: participants, events { click: onSelectParticipant }" />

<script type="text/x-kendo-template" id="listview-template">
    <div>
            <span>#:RoleDesc#</span> 
            <span>
                <select data-role="dropdownlist" id="status-id"
                        data-text-field="StatusDesc" 
                        data-value-field="StatusId"
                        data-bind="value: StatusId, source: participantStatuses, events: { change: onParticipantStatusChange }" 
                        name="Status" 
                        required="required" 
                        validationMessage="required">
                </select>
            </span> 
    </div>
</script>
Run Code Online (Sandbox Code Playgroud)

视图模型

viewModel = kendo.data.ObservableObject.extend({
    dataSource: new kendo.data.DataSource({
            transport: {
                type: "odata",
                read: {
                    url: function() {
                        return meetings/participants";
                    }
                }
              }        
        }),
    participants: [], //listview data
    participantStatuses: [   // dropdownlist selection 
            { StatusId: 1, StatusDesc: "Invited" } ,
            { StatusId: 6, StatusDesc: "Present" }, 
            { StatusId: 7, StatusDesc: "Absent" } 
        ],
    selectedParticipant: null,
    showListView: function(e) {
        viewModel.dataSource.fetch(function(){
                var data = viewModel.dataSource.data();
                meetingViewModel.set("participants", data);
            });
    },
Run Code Online (Sandbox Code Playgroud)

我期望在页面加载时,参与者的选定 statusId 将通过将参与者绑定StatusIdvalue下拉列表的属性而被下拉列表捕获为 selectedValue ,如下所示data-bind="value:StatusId"。但在我的情况下它很奇怪,它抛出了一个错误

 Uncaught TypeError: Object #<Object> has no method 'get' 
Run Code Online (Sandbox Code Playgroud)

当我删除 时data-bind="value:StatusId",错误消失了,但它没有选择适当的选定值。

关于这个错误的任何想法?

小智 5

我看到两个可能的问题。

首先,您的data-bind="value: StatusId". 是StatusId在你的ViewModel?我没有看到它,但它是一个扩展对象,因此它可以在您粘贴的代码之前定义。

第二个问题,在我看来一点也不明显,是下拉列表从列表数据源返回完整对象;不仅仅是请求的属性/字段。

在他们的网站上查看此演示页面以获取示例:http : //demos.kendoui.c​​om/web/mvvm/widgets.html

具体来说,他们使用一个辅助函数来返回对象的字符串表示。您可以改为StatusId根据需要返回。

<h4>DropDownList </h4>
<select data-role="dropdownlist"
        data-text-field="name" data-value-field="value" data-bind="source: colors, value: dropDownListValue">
</select>
Run Code Online (Sandbox Code Playgroud)

脚本

dropDownListValue: null,
displayDropDownListValue: function() {
    var dropDownListValue = this.get("dropDownListValue");
    return kendo.stringify(dropDownListValue);
}
Run Code Online (Sandbox Code Playgroud)

这似乎相当令人费解,但我只是自己解决了这个问题,未来应该不会有太大的交易。