javascript - knockout and Select2 get selected object -


I am working on a project where im Net Web API, Knockout and in this example, I am using jquery plugin. / P>

What to do to set some field values ​​after the change of selection I Select2 control list is loaded after the AJAX call and the objects are more data than just the id and the text. How can I get the rest of the data, so I can fill in other inputs with it? For some time, I am trying to update a view-model after the change of selection (but I get the data when this plugin calls AJAX).

Here is a sample data that should be in the selected object:

<">" Customer ": {" id ": 13," no ":" 0000012 "," name ":" "SS", "Phone": "458447478", "Customer Location": [], "Something", "Address": "Some More", "Zipcode": "324231", "City": "Simcity", "Area" }}

Here's where I am now:

Sample html:

  & lt; input type = "hidden "Data-Bind =" Selection 2: {optionsText: 'name', optionWallow: 'ID', SourceUrl: apiUrls.customer, model: $ root.customer ()}, Value: Customer ID "id =" Customer Name "name =" customer name "/> input type =" text "data-bind =" ........ "/> & lt; input type =" text "data- Bind = "........" /> etc ...  

And it's custom binding:

  ko.bindingHandlers .select2 = {init: function (element, valueAccessor, allBindingsAccessor, viewModel, binding contaxes) {var obj = valueAccessor (), allBinding = allBindingAccessor (); Var optiontext = ko.utils.unwrapObservable (obj.optionsText); Var options value = c.walls.ewind obsovable (obj.optionsValue); Var sourceUrl = ko.utils.unwrapObservable (obj.sourceUrl); Var selectedID = ko.utils.unwrapObservable (allBindings.value); Var model = ko.utils.unwrapObservable (obj.model); // I need to set / set $ object (set) Select2 ({placeholder: "select ...", minimum input length: 3, int selection: function (element, callback) {if (model & selected ID! == "") {callback ({id: model [ OptionsValue] Text: Model [optionsText] ()})}}}, Ajax: {cool emilis: 500, url: sourceUrl, datatype: 'jason', data: function (search, page) {return page: page , Search: search}}, result: function (data) {var results = []; $ each (data.list, function (key, value) {result.push ({id: value [optionsValue], text: value [OptionsText]})}} Var more = data.paging.currentPage & lt; data.paging.pageCount; Return {Result: Par ;};}}, Update: function (element, function, element, function) (element, function) {$ (element) .select2 ('deleted');});}, ValueAvorator, AlbuqueroesAvrier, Viewloadel, Binding Contax) {var obj = valueAccessor (), allBindings = allBindingAccessor (); Var model = ko.utils.unwrapObservable (obj.model); // the object that I need to get / set var , SelectID = ko.utils.unwrapObservable (allBindings.value); $ (Element) .select2 ('val', selectedID); $ (Element). On ("change", function (e) {// ...}); }};  

Obtaining the selected ID or text is not a problem, but how to balance the rest after the AJAX call?

You

When you create any intentions for your results, Then added the complete object as the "object" property.

  result.push ({id: value [optionsValue], text: value [optionsText], data: value});  

Then handle 2 selected events selected by select2. In the event object, this object should be literal as its object object.

  $ element.on ('select2-selected', function (eventData) {if (eventData.choice) {// item selected var dataObj = eventData.choice.data; var selectedId = eventData .choice.id;} and {// cleared the item}});  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -