Hi!
Hope you are doing well.
I need your help in implementing ajax request… Couldn’t able to find any useful resources…
I have simple Html::dropdownList() in view
so onchange event of dropdownlist i need to query all the records based on id using ActiveDatProvider in controller and return response to same page and should display as GridView…
How to implement it i am trying from couple of days could able get around the solution…
I am not getting any response from controller…
Codes
In View
<div class="row chit-group ">
<?= Html::tag('br'); ?>
<?= Html::tag('br'); ?>
<?= Html::tag('h3', 'Select Chit Group') ?>
<?= Html::dropDownList('chit_grps', null, $listData, ['prompt'=>'Select Chit Group',
'id'=>'chit_grp',
'onchange'=>'ajaxRequest(this.value)'
]); ?>
</div>
<?php
echo Html::script("function ajaxRequest(value){
$.ajax({
type: 'post',
url:chitgrp, // if i use url here it show error Uncaught ReferenceError: chitgrp is not defined
data:{'chit_grp':'value'},
success: console.log(value)
});
}
");
if(isset($dataProvider1)){
?>
<?= GridView::widget([
'dataProvider' => $dataProvider1,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'chit_grp_name',
'no_of_subscribers',
'chit_duration',
'chit_tot_amt',
'chit_status',
['class' =>
'yii\grid\ActionColumn',
'template' => '{view}',
'urlCreator' =>
function ($action, $model, $key, $index)
{
if ($action === 'view')
{
$url ='chitview?id='.$model->chit_grp_id;
return $url;
}
}
],
['class' =>
'yii\grid\ActionColumn',
'template' => '{update}',
'urlCreator' =>
function ($action, $model, $key, $index)
{
if ($action === 'update')
{
$url ='chitupdate?id='.$model->chit_grp_id;
return $url;
}
}
],
],
]);
?>
<?php
}
?>
In Controller
public function actionChitgrp()
{
$dataProvider = new ActiveDataProvider([
'query' => ChitGroup::find()->where(['organization_id'=>Yii::$app->session->get('organization_id')])
]);
if(Yii::$app->request->isAjax)
{
$id = Yii::$app->request->post()['chit_grp'] ;
$dataProvider1 = new ActiveDataProvider([
'query' => Bidding::find()->where(['id'=>$id])->all()
]);
return $this->renderAjax('chitgrp', ['dataProvider1'=>$dataProvider1]);
}else
{
return $this->render('chitgrp', ['dataProvider'=>$dataProvider]);
}
}
It will really great full if have any reference links to how to use specifically ajax in yii2…
Thank you