vishu vishu - 6 months ago 103
Javascript Question

Yii2 load modal from on click of link

This is my _form

<div class="row">
<div class="col-sm-4">
<?= $form->field($model, 'otm_grading_system')->dropdownList(ot\TestGradingSystem::getTestGradingSystem(), [
'prompt' => Yii::t('ot', '--- Select Grading System ---'),
'onchange' => '$.get( "'.Url::toRoute('dependent/get-grading-system').'", { id : $(this).val() }).done(function(data) {
$( "#seeHere").html(data);
});'
]) ?>
</div>
<div class="col-sm-4" id="seeHere">
</div>
</div>


In form dropDownList on change event get url. show below DependandController.php

class DependentController extends \yii\web\Controller
{
public function actionGetGradingSystem($id)
{
return Html::a('See Here 123', ['test-grading-system/view', 'id' => $id], ['id' => 'seeHereMdl', 'title' => Yii::t('ot', 'View Result')]);
}
}


And this is modal load js

<?php
$this->registerJs(
"$(document).on('ready pjax:success', function() {
$('#seeHereMdl').click(function(e){
e.preventDefault();
$('#grading-sys-modal').modal('show')
.find('.modal-content')
.load($(this).attr('href'));
});
});
", $this::POS_HEAD);
?>
<?php
yii\bootstrap\Modal::begin([
'id'=>'grading-sys-modal',
'size' => 'modal-lg'
]);
yii\bootstrap\Modal::end();
?>


My problem is this can't load view file in modal but it redirect to this view page.

Answer

This is an event handling problem (not related to yii), instead of :

$('#seeHereMdl').click(function(e){});

You should simply try :

$('body').on('click', '#seeHereMdl', function(e) {});

Read more : Event binding on dynamically created elements?

Comments