Default value selection for drop down list / combobox

38 views
Skip to first unread message

jpearsonva

unread,
Sep 29, 2015, 8:24:36 AM9/29/15
to KnockoutJS
Hi all,
I'm newer to knockout and trying to work on a form I inherited.  Basically, have a combobox (it's a dropdownlist but also a combobox since it can dynamically add a new item to the list) and I'm having trouble figuring out how to have a default value selected.  Right now it defaults to the caption text "Select...".

Here's the html and options for the control:

<combobox id="type"
                        params="options: $root.Type.choices,
                                value: type,
                                text: 'name',
                                valueConstructor: function (name) {
                                    return new $root.Type({name: name});
                                }">
                    </combobox>


View Model:

var Type = buildViewModel(
          
            function (data) {
                var self = this;

               
                data = data === undefined ? {} : data;

                self.id = data.id;
                self.name = data.name;
            },
            {
               
                list: "All_Types",
                mapping: {
                    "ID": "id",
                    "Name": "name"
                },
              
                generateSubmit: true,
              
                generateChoices: true,
               
                generateRetrieve: {
                   
                    data: function (auId) {
                        return dr.getMainToListData(
                            auId,
                            "au_Type_ID",
                            "au_Types");
                    }
                }
            }
        );

Later in the form, calling the observable for type:

self.type =
                    self.id() === undefined ?
                    ko.observable() :
                    Type.retrieve(self.id());

I think this is where I need to set the default value somehow (based on some examples I've seen)..but trying something like ko.observable(1) to set it to one of the choices just blows up the form.



Reply all
Reply to author
Forward
0 new messages