Wilson Ng Wilson Ng - 2 months ago 30
jQuery Question

Get selected rows value from kartik gridView in modal and pass it to parent form

I try to get value from modal bootstrap (contain table kartik gridview), and i want to pass the selected value to parent form (action create form). How to do that? Please advice.
modal form contains fields like this

<?php
$gridColumns = [
'id',
'date',
[
'attribute' => 'agen_id',
'value' => 'agen.agen_name'
],
[
'attribute' => 'price',
'format' => ['decimal', 0],
],
'remark_1:ntext',
'remark_2:ntext',

] ?>


and this is the button in parent form

<div class="col-xs-8">
<div class="form-group field-poagen-price">
<br>
<?= Html::button('Price List', ['value' => Url::to('../pricelist/list'), 'class' => 'btn btn-primary', 'id' => 'BtnModalPriceList']) ?>
<?php
Modal::begin([
'header' => 'List Harga',
'id' => 'modal',
'size' => 'modal-md'
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
</div>
</div>


and i register my jquery like this

$('#BtnModalPriceList').click(function(){
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));});


I want to get the value from modal to pass to the parent in order to fill the required field.
I have searched for a few days and still have no luck.
Please advice for the master. Thanks before.

Answer

I have solved it. This is what i do. Create custom button in the gridView

[
    'class' => 'yii\grid\ActionColumn',
    'template' => '{select}',
    'buttons' => [
        'select' => function($url, $model, $key){
            return Html::button('Select', [
                'title' => Yii::t('yii', 'Select'),
                'aria-label' => Yii::t('yii', 'Select'),                        
                'class' => 'btn btn-primary select-row',
                'data-id' => $model->id,
            ]);
        },
    ],            
],

and then register to jquery to process the data

$(document).on('click', '.select-row', function(){
    // get id from custom button
    var id = $(this).attr('data-id');
    $.get('../pricelist/get-price', {id : id}, function(data){            
            $('#poagen-price').val(data);
        });
        $('#modalPriceList').modal('hide');
        $('#poagen-fee').focus();
});

got help from @milos-ozegovic