How to get value of Kendo UI AutoComplete onKeyUp inside ViewModel

懵懂的女人 提交于 2019-12-11 11:25:05

问题


I am trying to get the value of a Kendo UI AutoComplete as the user types so I can send that query to the server to filter results. The change event only fires onBlur and not keyup/down/press. The data-value-change binding also appears not to work for this scenario either.

Is there anyway to achieve this from within the ViewModel?

Please see the code below as a working jsFiddle here. You will notice the data-value-change works for a vanilla input but not on the autocomplete.

<div id="view">
<input data-value-update="keyup" 
       data-bind="value: inputValue"
       data-placeholder="text input" />

 <input data-role="autocomplete"
        data-placeholder="autocomplete"
        data-value-primitive="true"
        data-text-field="t"
        data-bind="source: data, value: acValue" />

</div> 



var viewModel = kendo.observable(
{
inputValue: '',
acValue: '',
data: new kendo.data.DataSource([
    {name: 'John', age: 20 },
    {name: 'John 1', age: 201 },
    {name: 'John 2', age: 202 },
    {name: 'John 3', age: 203 },
    ]),
onChange: function()
{
  console.log('change');
}
});
viewModel.bind('change', function(e)
           {
              alert(e.field + ' changed to ' + this[e.field]); 
           });
kendo.bind($("#view"), viewModel);

回答1:


I went through same issue.

I managed to handle it with having another observable property for just display purpose.

But main observable property would be updated on every KeyUp event.

$('textarea').keyup(function () {
        viewModel.set("megaViewModel.ProductionNotes", $(this).val());
    });

I know we can update Main Property on keyup when binded with main property, but that would cause improper behavior Caret behavior.




回答2:


According to some users responses on other sites and blogs this is not supported on the autocomplete widget.




回答3:


Well, it's dirty method, but worked for me:

after initializing widget, set up listener:

$($("#input").data("kendoAutoComplete").element[0]).on('keyup',function(){
  //do your stuff here

})


来源:https://stackoverflow.com/questions/21508550/how-to-get-value-of-kendo-ui-autocomplete-onkeyup-inside-viewmodel

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