kartik-v / yii2-widgets

Collection of useful widgets for Yii Framework 2.0
http://demos.krajee.com/widgets
Other
558 stars 175 forks source link

select2 ajax dropdown loading #300

Open sunilsoftkochi opened 8 years ago

sunilsoftkochi commented 8 years ago

I am using two select2 ajax loading dropdown list using Yii2 kartik widget. I need to change selection of second dropdown while changing selection of first dropdown.

First Dropdown $qtnno = ''; $qtn = ServiceQuotation::find()->where(['UDNO' => $model->QTN_UDNO])->one(); if($qtn != null) $qtnno = $qtn->UDNO; echo $form->field($model, 'QTN_UDNO')->widget(Select2::classname(), [ 'initValueText' => $qtnno, // set the initial display text 'options' => ['placeholder' => 'Search for a Quotation ...', 'onchange'=>' $.getJSON( "'.Url::toRoute('getqtndetails').'", { id: $("#servicejobcard-qtn_udno").val() } ) .done(function( data ) { $( "#'.Html::getInputId($model, 'QTNNO').'" ).val( data.qtnno ); $( "#'.Html::getInputId($model, 'QTNDATE').'" ).val( data.qtndate ); $( "#'.Html::getInputId($model, 'PCODE').'" ).select2({ data: [{id: data.pcode, text: data.pname}]});; $( "#'.Html::getInputId($model, 'PCODE').'" ).select2("val",data.pcode); $( "#'.Html::getInputId($model, 'PROJECTNAME').'" ).val( data.projectname ); } );' ], 'pluginOptions' => [ 'allowClear' => true, 'minimumInputLength' => 3, 'ajax' => [ 'url' => $urlQtn, 'dataType' => 'json', 'data' => new JsExpression('function(params) { return {q:params.term}; }') ], 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), 'templateResult' => new JsExpression('function(data) { return data.text; }'), 'templateSelection' => new JsExpression('function (data) { return data.text; }'), ], ]); `

Second Drop Down

`
$cusName = empty($model->PCODE) ? '' : Customer::findOne($model->PCODE)->PNAME; echo $form->field($model, 'PCODE')->widget(Select2::classname(), [ 'initValueText' => $cusName, // set the initial display text 'options' => ['placeholder' => 'Search for a Customer ...', ], 'pluginOptions' => [ 'allowClear' => true, 'minimumInputLength' => 3, 'ajax' => [ 'url' => $url, 'dataType' => 'json', 'data' => new JsExpression('function(params) { return {q:params.term}; }') ], 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), 'templateResult' => new JsExpression('function(party) { return party.text; }'), 'templateSelection' => new JsExpression('function (party) { return party.text; }'), ], ]);

`

Using the above code i'm able to change the selection in second dropdown. But after the change, i'm not able to make selection in second dropdown.

Please help...

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/33053685-select2-ajax-dropdown-loading?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github).