T
T
teodor7teodor72017-06-11 10:59:07
Yii
teodor7teodor7, 2017-06-11 10:59:07

How to display kartik\select2 in a modal window in Yii2 using renderAjax?

You need to display demos.krajee.com/widget-details/select2#usage-tags in a modal window.
in main.php I connect a modal window

<?php
yii\bootstrap\Modal::begin([
    'header' => '<div id="modalHeader"></div>',
    'id' => 'modal',
    'size' => 'modal-lg',
    //keeps from closing modal with esc key or by clicking out of the modal.
    // user must click cancel or X to close

    'clientOptions' => ['backdrop' => 'static']
]);
echo "<div id='modalContent'></div>";
yii\bootstrap\Modal::end();

?>

controller
public function actionTagsForm()
    {
        $method = Yii::$app->request->isAjax ? 'renderAjax' : 'render';
        $model = new UserTagsForm();

        return $this->$method('tagsForm', ['model' => $model]);
    }

display view
<?php


$data = [
    "red" => "red",
    "green" => "green",
    "blue" => "blue",
    "orange" => "orange",
    "white" => "white",
    "black" => "black",
    "purple" => "purple",
    "cyan" => "cyan",
    "teal" => "teal"
]; ?>

<div class="image-tags-index">

    <?php $form = ActiveForm::begin();?>
    <?php echo $form->field($model, 'image_tags')->widget( Select2::classname(),[

        'data' => $data,

        'options' => ['placeholder' => 'Select a state ...'],
        'pluginOptions' => [
            'allowClear' => true
        ],
    ])->label('Tag Multiple');
 ?>
</div>

if you just render the page, then everything is displayed, if through adjax, it gives an error
Uncaught ReferenceError: select2_3f25e3ac is not defined
    at eval (eval at globalEval (jquery.js:343), <anonymous>:2:56)
    at eval (<anonymous>)
    at Function.globalEval (jquery.js:343)
    at domManip (jquery.js:5291)
    at jQuery.fn.init.append (jquery.js:5431)
    at jQuery.fn.init.<anonymous> (jquery.js:5525)
    at access (jquery.js:3614)
    at jQuery.fn.init.html (jquery.js:5492)
    at Object.<anonymous> (jquery.js:9436)
    at fire (jquery.js:3187)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-06-12
@qonand

Apparently you do not have resources for this widget on the page, because renderAjax - doesn't load them. Accordingly, you need to register the corresponding resource sets on the page itself (on which the pop-up window is displayed), for example:
\kartik\select2\Select2Asset::register($this)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question