1
$(document).ready(function () { 
var combobox = $("#Id").data("kendoComboBox");
 combobox.dataSource.data(jsonData);
});

I am trying to set the datasource for the combobox on document ready, but, i can't set the combobox is always undefined...

I am declaring combobox in razor partial view..

        @(Html.Kendo().ComboBoxFor(model => model.Id)
        .HtmlAttributes(new { style = "width: 100%;" })
         )

the comobo element is there...but the datasource for the combo is undefined..so may i need to init the datasource event on load? how can i do this?

WingMan20-10
  • 3,594
  • 9
  • 31
  • 42
  • 1
    it is a different scope. If you move the set inside of the document ready where the var combobox is defined it should work for you – Matt Bodily Nov 06 '13 at 21:00
  • 1
    Are you sure that your combobox's id is `#combo`? Your Razor code will generate it with `#id`... And how do you load your partial with Ajax? Because in this case the DOM ready runs before your partial is loaded... – nemesv Nov 06 '13 at 21:03
  • is the #combo element actually there? Stick an alert($('#combo')[0]); in the first line of your doc ready to see if there's actually an element there. – David L Nov 06 '13 at 21:30
  • the comobo element is there...but the datasource for the combo is undefined..so may i need to init the datasource event on load? – WingMan20-10 Nov 07 '13 at 15:08
  • http://stackoverflow.com/a/22726040/2001735 – Lars Höppner Mar 29 '14 at 02:01

3 Answers3

1

In JavaScript variables are scoped to a function. So you var combobox is only visible inside function you execute during ready. Fix move declaration and usage to the same scope:

$(function () { 
  var combobox = $("#combo").data("kendoComboBox");
  combobox.dataSource.data(jsonData);
});

or:

$(function () { });
var combobox = $("#combo").data("kendoComboBox");
combobox.dataSource.data(jsonData);

Note also that .data("kendoComboBox") may not be available yet if you run script before ready.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • @WingMan20-10 - I assume you've looked at HTML and element with that ID exists, you've debugged the script (F12 in most browsers) and found that kendo is initialized *before* your call... Not sure what else - you may want to update your post with actions you've tried to debug the issues. – Alexei Levenkov Nov 06 '13 at 21:29
0

Kendo combobox is not included in the standard core or web packages.

Please use kendo.all.min.js located at http://cdn.kendostatic.com/2014.1.318/js/kendo.all.min.js

in order to gain access to kendoComboBox.

Michael Angstadt
  • 880
  • 10
  • 18
0

Try this:

 var combobox = $("#ID").data("kendoComboBox");
 combobox.dataSource.read();
n4m31ess_c0d3r
  • 3,028
  • 5
  • 26
  • 35