0
votes

I have a primefaces autocomplete element which works great except one thig. The problem is that when I enter a valid text (which is mappable to the data behind) but I don't select the element from the propositions, and don't press tab or enter, nothing happens.

So I enter a value and click into another field, the element is not selected and the validation fails. However, I don't want the user to force to explicitly select an item.

My ideas were, that i put an onchange listener to the input element and trigger the primefaces itemSelect event within. But I don't know how to do that, if it's even possible.

Or maybe there are other solutions? Thanks in advance!

1

1 Answers

0
votes

I found a way, although it might not be the most beautiful and easy one. Maybe it helps somebody...

This is a specific solution for primefaces (5.3), but it should work for other versions too.

$('#form\\:txtAutoComplete_input').val('Foo');
$('#form\\:txtAutoComplete_input').trigger('keydown');
$('#form\\:txtAutoComplete_input').trigger('input');
$('#form\\:txtAutoComplete_panel .ui-autocomplete-item').trigger('click');

For some reason, after the value is entered into the input field, you have to trigger the keydown and the input event in that order. After these events the autocomplete list shows with the matching values. There you have to trigger click on a certain element, so all the backing bean stuff is properly executed.