kartik-v/yii2 select2-widget not working with render partial

912 Views Asked by At

I am trying to add dynamic drop downs, which will added from javascript on clicking add new button.

view file

<?php

 use yii\helpers\Html;
 use yii\widgets\ActiveForm;
 use kartik\select2\Select2;
 use kartik\select2\Select2Asset;
 use yii\helpers\ArrayHelper;

 Select2Asset::register($this);
?>
<div class='row'>
    <div class='form-group'>
    <?= 
        Select2::widget([
            'name' => 'coupons', 
            'value' => $model->coupon_id, 
            'data' => ArrayHelper::map($coupons, 'id', 'name'),
            'options' => ['placeholder' => 'Select a state ...'],
            'pluginOptions' => [
                'allowClear' => true
            ],
        ]);
    ?>
    </div>
</div>

controller

public function actionNewCoupon()
{
    $coupon = new DealCoupon();
    $deal = Yii::$app->request->get('deal');
    $order = Yii::$app->request->get('order');
    $coupon->deal_id = $deal;
    $coupon->order = $order;
    $coupon->save();

    $coupons = Coupon::find('id','name')->all();

    return $this->renderPartial('_form', [
        'model' => $coupon,
        'coupons' => $coupons
    ],true, true);

}

js file

.get(BASE_URL + '/coupon/new-coupon', { deal: dealId, order: order }, function(data) {
        var dealWidget = newStep.find('.coupon-panel');
        $(newStep).find('.coupon-panel').html(data); 
    });

What I am getting

enter image description here

Any help to resolve this issue is appreciable, as I am totaly lost. Thanks in advance.

1

There are 1 best solutions below

1
Michal Hynčica On

yii\web\Controller::renderPartial() doesn't include registered JS or CSS files. Try using yii\web\Controller::renderAjax() instead. It's similar but it injects registered JS/CSS files into rendered html code.