Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2745
Get Selected Value using Knockout
posted

How do we get the selected value from a combo box using an observable binding?

We noticed that there isn't a method of obtaining the value or selected value.  But there is a property for selected items.  I see in the example on Ignite UI that they use a view model method to find the actor internally.  Is this the only way of getting the selected value?

What if our arrays contain thousands of records?  Do we have to loop through all records to find our match?

Parents
  • 4315
    Offline posted

    Hi, Daniel.

    Thank you for using our product and I hope that I will help you with the following answer.

    You can get the igCombo text that is shown in the input using the "text" method:

    $("#combo").igCombo("text")

    You can get the igCombo value using the "value" method:

    $("#combo").igCombo("value")

    You don't need to iterate over all the records. Here you can find reference to all the igCombo methods.

    The loop that you are looking is, I guess, in the following sample. There all the items are added to observable array, because we can add and remove items to the drop down collection. The igCombo supports also this scenario, when you add or deleted items from the data source in the Model, then the igCombo items list are also updated. This happens without the need to rebind the igCombo with the new data. But this is different binding from the one in the igCombo text input. If you want only the text there to be updated, and your list items are static, you need only observable for the igCombo input, which in the previous example is done using the following code:

    this.actorName = ko.observable(self.actors()[0].name);

    The Knockout extension for the igCombo is done in way that the observable item is the "text" one. If you want to make the "value" of the igCombo to be observable, instead of the "text", you need to do this manually. You can use the "selectedIndex" method and then use "itemByIndex" method to get the selected list item. Then having the value, which is observable, you can notify manually that it is changed. I don't know if this is the case your trying to achieve, sorry if you mean something else. If you want to observe the "text", this is done automatically by the igCombo, cause this is the expected observed value in most of the cases.

    For any further questions, don't hesitate to ask.

    Best regards,

    Nikolay Alipiev

     

Reply Children