3

I am really struggling to select an option from my combo box based on the text rather than value. I have a combo box which has a datasource attached to it which are countries. These countries are stored in the database. I want the default country to be "United Kingdom". At the moment I am doing the following:

combobox.select(combobox.text("United Kingdom"));

However, this only shows the text and doesn't actually select it because the select function does not trigger. Any help with this?? I want the value to be applied. I have an alert in the select function which is not appearing.

Simmy Dhanda
  • 139
  • 1
  • 4
  • 13

2 Answers2

4

Use the select method of the widget and pass a predicate

combobox.select(function(dataItem) {
    return dataItem.text === "Apples"; //note that 'text' === dataTextField
});

Here is a runnable demo demonstrating this approach.

George K
  • 1,763
  • 1
  • 9
  • 17
  • I've tried that as well and it still doesn't work. It just display the right text rather than actually selecting it. – Simmy Dhanda Sep 03 '14 at 09:51
  • when I do what you said and try and alert the value via: alert(combobox.value()); it alerts the text rather than the actual value which is a number. – Simmy Dhanda Sep 03 '14 at 09:54
  • Updated the answer withe a runnable demo. Nevertheless, here it is: http://dojo.telerik.com/OBAm – George K Sep 03 '14 at 09:59
  • $("#combobox").kendoComboBox({ dataSource: [ { id: 1, name: "Apples" }, { id: 2, name: "Oranges" } ], dataTextField: "name", dataValueField: "id", select:function(e){ alert(999); } }); i included the select function and the alert does not show on page load?? – Simmy Dhanda Sep 03 '14 at 10:02
  • As it is explained in the docs, select event is triggered only on user interaction. http://docs.telerik.com/kendo-ui/api/web/combobox#events-select – George K Sep 09 '14 at 08:04
3

Selecting kendo comboBox value explicitly from javascript don't trigger the "Select" event.

In order achieve that you have to trigger the "Select" event after setting the required value. e.g.

 var myComboBox = $('#comboBoxId').data('kendoComboBox');
 myComboBox.text("United Kingdom");
 myComboBox.trigger("select");

Hope this will solve your purpose. Also check here.

Satya Ranjan Sahoo
  • 1,086
  • 7
  • 24