kendo-ui autocomplete extend

强颜欢笑 提交于 2019-12-11 08:48:46

问题


I'm trying to extend the kendo-ui autocomplete control: I want the search start when te user hit enter, so basically I've to check the user input on keydown event. I've tried to catch the keydown event with this code:

(function($) {
    ui = kendo.ui,
    Widget = ui.Widget
    var ClienteText = ui.AutoComplete.extend({
        init: function(element,options) {
            var that=this;
            ui.AutoComplete.fn.init.call(this, element, options);                
            $(this).bind('keydown',function(e){ console.log(1,e); });
            $(element).bind('keydown',function(e){ console.log(2,e); });
        },
        options: {
            [...list of my options...]
        },
        _keydown: function(e) {
            console.log(3,e);
            kendo.ui.AutoComplete.fn._keydown(e);
        }
    });
    ui.plugin(ClienteText);
})(jQuery);

None of the binded events gets called, only the _keydown, and then I'm doing something wrong and cannot call the autocomplete "normal" keydown event. I've seen a lot of examples that extend the base widget and then create a composite widget, but I'm not interested in doing that, I only want to add a functionality to an existing widget. Can someone show me what I'm doing wrong?

Thank you!


回答1:


What about avoiding the extend and take advantage of build in options and methods on the existing control : http://jsfiddle.net/vojtiik/Vttyq/1/

//create AutoComplete UI component
 var complete = $("#countries").kendoAutoComplete({
     dataSource: data,
     filter: "startswith",
     placeholder: "Select country...",
     separator: ", ",
     minLength: 50 // this is to be longer than your longest char
 }).data("kendoAutoComplete"); 

 $("#countries").keypress(function (e) {
     if (e.which == 13) {
         complete.options.minLength = 1; // allow search
         complete.search($("#countries").val());
         complete.options.minLength = 50; // stop the search again
     }
 });



回答2:


This code actually work:

  (function($) {
    ui = kendo.ui,
    ClienteText = ui.AutoComplete.extend({
      init: function(element,options) {
        ui.AutoComplete.fn.init.call(this, element, options);                
        $(element).bind('keydown',function(e){
          var kcontrol=$(this).data('kendoClienteText');
          if (e.which === 13) {
            kcontrol.setDataSource(datasource_clientes);
            kcontrol.search($(this).val());
          } else {
            kcontrol.setDataSource(null);
          }
        });
      },
      options: {
        name: 'ClienteText',
      }
    });
    ui.plugin(ClienteText);
  })(jQuery);

but I don't know if it's the correct way to do it.



来源:https://stackoverflow.com/questions/18175988/kendo-ui-autocomplete-extend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!