问题
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