With Yii2 you can use ‘\yii\jui\AutoComplete’. I got this working, but…
I want the ability to choose more values.
According to ‘jqueryui.com/autocomplete/#multiple’ it is possible. When I use this script using an asset it does work, but I don’t want hard coded values. I want values from a tags table.
Ok, but yii2-selectize-widget puts all tags in the field, I want a widget that autocompletes the input in the text input and the ability to input more then one tag.
I think the problem is \yii\jui\AutoComplete widget. When I use clientOptions to setup the config (using new JsExpression)…the config is visible in the browser source view and looks like it should be, but does not work.
No matter how I do it. On the other forums I found more people with the same issues… so I will use my below solution for the time being.
When is use below script in my _form view file, I AM able to select multiple tags…
<?php $this->registerJs("
$(document).ready(function(){
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$('#blogpost-tags' )
// don't navigate away from the field on tab when selecting an item
.bind( 'keydown', function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( 'instance' ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
$.getJSON( '".Url::to('suggest')."', {
term: extractLast( request.term )
}, response );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( '' );
this.value = terms.join( ', ' );
return false;
}
});
});");?>