最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Modify change event of Kendo UI Dropdownlist - Stack Overflow

programmeradmin6浏览0评论

I'm trying to change the "change" event of a Kendo UI Dropdownlist, but unfortunately I haven't had any luck. Here's the code I've tried so far:

Specifically, here's my dropdownlist initializer:

var onChange = function(e)
{
    alert("something changed");
};
// create DropDownList from input HTML element
$("#dropdown").kendoDropDownList({
    dataTextField: "text",
    dataValueField: "value",
    dataSource: data,
    index: 0,
    change: onChange
});

And this is my attempt to change that function later:

onChange = function(e){alert("attempt 1");};
var dropDownData = $("#dropdown").data("kendoDropDownList");
dropDownData.options.change = function(e){alert("attempt 2");}
dropDownData.refresh();

Specifically, I first tried just changing the onChange function, and then (when that didn't work), I tried changing the options.change function directly. While the options.change function did change (when I examined it in chrome debugger), the dropdownlist's actual functionality remained unchanged. I think I need to refresh the dropdownlist or something to make my edits actually take effect. Does anyone know how I can actually get the kendo grid to refresh and display my data?

I'm trying to change the "change" event of a Kendo UI Dropdownlist, but unfortunately I haven't had any luck. Here's the code I've tried so far: http://trykendoui.telerik./ukeV

Specifically, here's my dropdownlist initializer:

var onChange = function(e)
{
    alert("something changed");
};
// create DropDownList from input HTML element
$("#dropdown").kendoDropDownList({
    dataTextField: "text",
    dataValueField: "value",
    dataSource: data,
    index: 0,
    change: onChange
});

And this is my attempt to change that function later:

onChange = function(e){alert("attempt 1");};
var dropDownData = $("#dropdown").data("kendoDropDownList");
dropDownData.options.change = function(e){alert("attempt 2");}
dropDownData.refresh();

Specifically, I first tried just changing the onChange function, and then (when that didn't work), I tried changing the options.change function directly. While the options.change function did change (when I examined it in chrome debugger), the dropdownlist's actual functionality remained unchanged. I think I need to refresh the dropdownlist or something to make my edits actually take effect. Does anyone know how I can actually get the kendo grid to refresh and display my data?

Share Improve this question edited Jun 6, 2014 at 22:25 Lars Höppner 18.4k2 gold badges47 silver badges73 bronze badges asked Jun 6, 2014 at 19:28 hubatishhubatish 5,2706 gold badges38 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I see you've already found a work-around, but I thought I'd add some more explanations.

The reason why changing it like this

onChange = function(e){alert("attempt 1");};

doesn't work is that you're passing a function to the widget, and the widget calls that function at appropriate times. You're not passing a reference to the variable onChange. So changing the variable won't affect the widget.

The reason why

dropDownData.options.change = function(e){alert("attempt 2");} 

doesn't work is that Kendo stores handlers in its own structure (which is necessary since you can bind multiple handlers for the same event!), so it doesn't call whatever is in options.change at any given moment in time.

However, there is a setOptions method, and with that you could've made your second approach work:

dropDownData.setOptions({
    change: function (e) {
        alert("attempt 2");
    }
});

I ended up using a simple work-around. When I put in the line

change: onChange

Kendo actually hardcoded in the function body into the dropDownData.options.change function, rather than referencing my named javascript function. It looked like this in the debugger:

function(e)
{
  alert("something changed");
};

So my workaround was simply to use an anonymous function in the Kendo call. So I changed change: to

change: function(e) { onChange(e) };

Then when I changed onChange later, kendo referenced the named javascript function and called my updated version. Here's the working demo: http://trykendoui.telerik./AkIW/2

发布评论

评论列表(0)

  1. 暂无评论