Answer the question
In order to leave comments, you need to log in
YII2 Autocomplete & Ajax form autocomplete not working after ajax load. What am I doing wrong?
Good day. There was a small issue using the autocomplete widget.
There is a form, Ajax adds another field with autocomplete to it, but it turns out that autocomplete does not work for the loaded field. What to do?
This is JS
$(document).on('click', '#addPunkt', function(){
var csrf = $('input[name="_csrf"]').val();
$.ajax({
type: 'POST',
url: "/travels/default/ajax_add_field/",
data: '_csrf=' + csrf,
success: function (data) {
$( "#aditoonalFields" ).append( data );
}
});
return false;
});
public function actionAjax_add_field(){
$listdata = \common\models\db\City::find()
->select(['id as value', 'name as label'])
->asArray()
->all();
return $this->renderPartial('fields-city',['listdata' => $listdata]);
}
<?php
use yii\jui\AutoComplete;
use yii\web\JsExpression;
?>
<?php
echo AutoComplete::widget([
'id' => 'city',
'class' => 'ui-autocomplete-input',
'clientOptions' => [
'source' => $listdata,
'minLength'=>'3',
'autoFill'=>true,
'select' => new JsExpression("function( event, ui ) {
$('#memberssearch-family_name_id').val(ui.item.id);//#memberssearch-family_name_id is the id of hiddenInput.
}")],
]);
?>
Answer the question
In order to leave comments, you need to log in
1. What's inside fields-city?
2. Why renderPartial and not renderAjax?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question