functionalCode
functionalCode

Reputation: 563

KnockOut.JS optionsText

I have this code:

self.Groups = ko.observableArray();
function LoginNameObject(name) {
    this.loginName = name;
}

self.Groups.push(new LoginNameObject('Blah'));

I am trying to bind in the html like this:

 <select data-bind="options: Groups, optionsText: loginName"></select>

but keep on getting loginName is undefined when it tries to bind. Any help would be appreciated!

Thanks

Upvotes: 1

Views: 166

Answers (1)

nemesv
nemesv

Reputation: 139778

In the optionsText you need to specify your property name is a string.

So you need to write

<select data-bind="options: Groups, optionsText: 'loginName'"></select>

See also in the documentation: Example 3: Drop-down list representing arbitrary JavaScript objects, not just strings

Alternatively if you need a more complex logic to calculate the option text you can also specify the optiosText as a function:

<select 
   data-bind="options: Groups, optionsText: function(item) { return item.loginName }">
</select> 

Upvotes: 2

Related Questions