php - Yii2 Update the DataProvider of a Listview after Ajax -
i try without success update dataprovider of listview when submit form through ajax. working when submit without ajax, refresh page. without refreshing page. don t know wrong in code below:
here form , listview:
jobs.php
... <div style="background-color: white;"> <div class="job_quick_search_container"> <?php $form = activeform::begin([ 'id' => 'quick_search_form', 'enableajaxvalidation'=>false, 'enableclientvalidation'=>true, 'options' => ['class' => 'form-horizontal', 'onkeypress'=>" if(event.keycode == 13){ searchjob(); }" ], ]); ?> <table style="width:100%"> <colgroup> <col style="width:20%"> <col style="width:20%"> <col style="width:20%"> <col style="width:40%"> </colgroup> <tr> <td style="padding-right: 50px;"> <?= $form->field($jobcompanycategorysearchmodel, 'cccategory')->dropdownlist( arrayhelper::map(companycategory::find()->all(),'ccid', 'cccategory'), ['prompt'=>'select category'] ) ?> </td> <td style="padding-right: 50px;"> <?= $form->field($jobusersearchmodel, 'ucountry')->dropdownlist( arrayhelper::map(user::find()->all(), 'ucountry', 'ucountry'), ['prompt'=>'select country'] ) ?> </td> <td style="padding-right: 50px;"> <?= $form->field($jobusersearchmodel, 'ucompanyname')->dropdownlist( arrayhelper::map(user::find()->all(), 'ucompanyname', 'ucompanyname'), ['prompt'=>'select company'] ) ?> </td> <td> <div class="form-group"> <?= html::button('find job', ['class' => 'btn btn-primary', 'onclick' => 'searchjob();', 'style' => 'font-size:18px']) ?> </div> </td> </tr> </table> <?php activeform::end(); ?> </div> <table style="width:100%"> <colgroup> <col style="width:65%"> <col style="width:35%"> </colgroup> <tr> <td> //this partial view contains listview <?= $this->render( 'jobs_partial_view', ['dataprovider'=> $dataprovider] ); ?> </td> ...
the partial view: jobs_partial_view.php
<?php use yii\widgets\listview; ?> <div class="jobs_listview_container"> <?= listview::widget([ 'dataprovider' => $dataprovider, 'options' => [ 'tag' => 'div', 'class' => 'list_jobs_profile', 'id' => 'list_jobs_profile', ], 'layout' => "{items}\n{pager}", 'itemview' => function ($model, $key, $index, $widget) { return $this->render('_job_view',['model' => $model]); }, 'itemoptions' => [ 'tag' => false, ], 'pager' => [ 'firstpagelabel' => 'first', 'lastpagelabel' => 'last', 'nextpagelabel' => 'next', 'prevpagelabel' => 'previous', 'maxbuttoncount' => 3, ], 'emptytext' => "sorry, no jobs found", ]); ?>
here ist jquery function using ajax:
function searchjob(){ var seach_data_job = $("#quick_search_form").serialize(); $.ajax({ type: 'post', url: 'jobs', data: seach_data_job, datatype: 'html', success: function(e) { }, /* error: function(response, status){ console.log(response); console.log(status); } */ }); return false; }
here the controller:
public function actionjobs() { $jobcompanycategorysearchmodel = new jobcompanycategorysearch(); $jobusersearchmodel = new jobusersearch(); if ( $jobusersearchmodel->load( yii::$app->request->post() ) && $jobcompanycategorysearchmodel->load( yii::$app->request->post() ) ) { $idcategorysearch = $jobcompanycategorysearchmodel->cccategory; $companynamesearch = $jobusersearchmodel->ucompanyname; $companycountrysearch = $jobusersearchmodel->ucountry; $dataprovider = new activedataprovider([ 'query' => jobs::find()->where(['jcategory' => $idcategorysearch]) ->orderby('jid desc'), 'pagination' => ['pagesize' => 10,], ]); echo $this->renderpartial( 'jobs_partial_view', ['jobcompanycategorysearchmodel' => $jobcompanycategorysearchmodel, 'jobusersearchmodel' => $jobusersearchmodel, 'dataprovider' => $dataprovider ] ); } else { $dataprovider = new activedataprovider([ 'query' => jobs::find()->orderby('jid desc'), 'pagination' => ['pagesize' => 10,], ]); return $this->render('jobs', ['jobcompanycategorysearchmodel' => $jobcompanycategorysearchmodel, 'jobusersearchmodel' => $jobusersearchmodel, 'dataprovider' => $dataprovider ] ); } }
after submitting forms through ajax, data in controller, funtion renderpartial not updating view new dataprovider.
you use pjax in case. below.
<?php \yii\widgets\pjax::begin(['clientoptions' => ['method' => 'post']]); ?> <?php $form = activeform::begin([ 'id' => 'quick_search_form', 'enableajaxvalidation'=>false, 'enableclientvalidation'=>true, 'options' => ['class' => 'form-horizontal'] ]); ?> ....... ....... <!-- form htmls goes here --> ....... ....... <?php \yii\widgets\activeform::end(); ?> <?= $this->render( 'jobs_partial_view', ['dataprovider'=> $dataprovider] ); ?> <?php \yii\widgets\pjax::end(); ?>
note: no need write ajax code form submission. pjax take case of ajax stuffs. have merge 2 controller actions - actionjobs
, actionjobsearch
single action. actionjobsearch
coding come inside actionjobs
.
Comments
Post a Comment