Bind optionsText to object with array of objects using Knockout?

I have an array of objects stored internally observableArray

in Knockout.js and I am trying to bind one of the bound object properties optionsText

for an element select

, however this does not work, no options are displayed.

The observable array is initially empty and populated with an AJAX request:

self.currentPeople = ko.observableArray([]);

      

By following the AJAX request I can do console.log

this and get the following response:

console.log(self.currentPeople);
// Produces [Object, Object] where each of the objects have properties of `personId` and `personName`

      

However, my dropdown selection remains unpopulated:

<select class="large-3" data-bind="options: currentPeople, optionsText: 'personName', optionsValue: 'personId', optionsCaption: 'All',  value: currentPerson"></select>

      

Only "Everyone" appears. Any ideas?

+3


source to share


1 answer


If it console.log(self.currentPeople)

shows your array of objects, it means that you are (incorrectly) filling it with an assignment instead of calling it as a function.

Fill it in like:



self.currentPeople(newData);

      

+2


source







All Articles