K
K
king19902016-08-04 12:01:45
Yii
king1990, 2016-08-04 12:01:45

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;
    });

This is the controller:
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]);
    }


This is the view:

<?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 answer(s)
M
Maxim Timofeev, 2016-08-04
@king1990

1. What's inside fields-city?
2. Why renderPartial and not renderAjax?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question