Kendo MVVM Dropdown - How to set initial value based on other data?

ε祈祈猫儿з 提交于 2019-12-12 03:01:22

问题


I have the following html for a Kendo MVVM DropDownList:

                <select id="responseTypeDDL"
                    data-role="dropdownlist"
                    data-text-field="SystemResponseTypeCode"
                    data-value-field="SystemResponseTypeId"
                    data-bind="value: selectedSystemResponseTypeCode, source: responseTypes">
                </select>

This is my view model:

        SC.ViewModels.Reference.ResponseTypeDataSource.read();

        var responseTypeDDL = kendo.observable({
            responseTypes: SC.ViewModels.Reference.ResponseTypeDataSource,
            selectedSystemResponseTypeCode: null,
            setSelectedSystemResponseTypeCode: function (code) {
                this.selectedSystemResponseTypeCode = code;
            },
        });

        kendo.bind($("#responseTypeDDL"), responseTypeDDL);

        // after reading data, I call the method to set the selected value like this:
        self.ResponseTypeDDL.setSelectedSystemResponseTypeCode(results.code);

The ResponseTypeDataSource.read() method returns a list of "XML", "JSON". This is the SystemResponseTypeCode field. I also read another data item from the database and check its response type. Let's say it is "JSON". How do I set the drop down to have "JSON" selected?


回答1:


First of all this part seems to be wrong

setSelectedSystemResponseTypeCode: function (code) {
    this.selectedSystemResponseTypeCode = code;
},

You should make sure to call set() method while modifying an observed variable, otherwise it might not update the bindings:

this.set("selectedSystemResponseTypeCode", code);

And for your actual question

You need to set data-value-primitive="true" in order to work with just the id (Kendo Docs) (Please note changes below, value: selectedSystemResponseTypeId)

<select id="responseTypeDDL"
    data-role="dropdownlist"
    data-text-field="SystemResponseTypeCode"
    data-value-field="SystemResponseTypeId"
    data-value-primitive="true"
    data-bind="value: selectedSystemResponseTypeId, source: responseTypes">
</select>
SC.ViewModels.Reference.ResponseTypeDataSource.read();

var responseTypeDDL = kendo.observable({
    responseTypes: SC.ViewModels.Reference.ResponseTypeDataSource,
    selectedSystemResponseTypeCode: null,
    selectedSystemResponseTypeId: null,
    setSelectedSystemResponseTypeId: function (id) {
        this.set("selectedSystemResponseTypeId", id);
    },
});

kendo.bind($("#responseTypeDDL"), responseTypeDDL);

// Get your id
var id = ...    
responseTypeDDL.setSelectedSystemResponseTypeId(id);

Working example: http://dojo.telerik.com/AbIm/8




回答2:


I've managed to manually set the value in dropdownlist without resorting to data-value-primitive="true" because i need to access the selected value and display other fields.

Here's the solution:

var id = 1004;
var dataItem = responseTypeDDL.responseTypes.get(id); //get the id in your datasource
responseTypeDDL.set("selectedsystemResponse", dataItem);


来源:https://stackoverflow.com/questions/24897833/kendo-mvvm-dropdown-how-to-set-initial-value-based-on-other-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!