I am using knockout js and the selected plugin (https://github.com/harvesthq/chosen) to try and make a multi-user choice.
I tried different ways, but I can not get multiselect to work with the data that I use. When I press the multi selector, the values ββare not displayed, even if the parameter binding contains the correct data.
HTML:
<select multiple="multiple" data-bind="options: allCustomers, selectedOptions: event().customers, optionsText: 'name', optionsValue: 'id', chosen: true " ></select>β
A simplified version of the view model:
function Event() { this.customers = ko.observableArray(); }; //for chosen plugin ko.bindingHandlers.chosen = { update: function(element, valueAccessor, allBindingsAccessor, viewModel) { $(element).chosen(); } } function ViewModel() { this.event = ko.observable(new Event()); this.allCustomers = ko.observableArray(); }; var viewModel = new ViewModel(); $.getJSON("/get_json", function(data) { for (var c = 0; c < data.customers.length; c++) { viewModel.allCustomers.push(data.customers[c]); } }); ko.applyBindings(viewModel);
PHP:
function get_json() { $eventData = array( 'customers' => array(array('name' => 'Bob', 'id' => 1), array('name' => 'John', 'id' => 2)), 'moreData' => array(), 'evenMoreData' => array() ); echo json_encode($eventData); }
The selected style selection window is displayed here, but when I click on it, no options appear.
When I create a local JS array in the client view model and pass this to all clients, the multi selector works correctly (see my jsfiddle ), so this is due to receiving data from the server, but I looked at it for a while and did not see a problem!
Any help is much appreciated