Kendo MVVM and binding or extending custom events

前端 未结 2 1004
时光说笑
时光说笑 2020-12-17 02:22

I have a ComboBox in my page and I want to bind keypress event to my Kendo ComboBox when the cliend writes down any letter.

As I understand kendo doesn\'t have any k

相关标签:
2条回答
  • 2020-12-17 02:47

    This one was more complicated than I thought it would be, but you can handle this by making a custom MVVM binder to attach to the keyPress event of the input element, like this:

    kendo.data.binders.widget.keyPress = kendo.data.Binder.extend({
        init: function (element, bindings, options) {
            kendo.data.Binder.fn.init.call(this, element, bindings, options);
            var binding = this.bindings.keyPress;
            $(element.input).bind("keypress", function(){binding.get();});
        },
        refresh: function () {}
    });
    

    You would bind that to a function on the view model.

    <input data-role="combobox"
        data-text-field="text"
        data-value-field="value"
        data-bind="keyPress: onKeyPress, source: data"></input>
    
    
    var viewModel = kendo.observable({
        data: [
            {text: "One", value: 1},
            {text: "Two", value: 2}
        ],
        onKeyPress: function () {
            $("#output").append("<div>keyPress</div>");
        }
    });
    

    Here is a working jsFiddle.

    0 讨论(0)
  • 2020-12-17 02:50

    You can capture the keydown event of all ComboBox controls using the following code:

    kendo.ui.ComboBox.fn._keydown = function(e) {
        if (e.which == 13) {
            alert("key pressed!");
        }
    };
    

    This also works with the DropDownList widget that generally doesn't support the keypress events.

    0 讨论(0)
提交回复
热议问题