php - How to change Grid View using Pjax in yii2? -


ok, ill explain have first,

--a database table called cases.

this holds of cases need display in gridview

--three tables called category, subcategory , childcategory

the cases table case linked childcategory.

so have made 3 dropdownlists populated individual category tables in database. example:

_categories.php

 yii\widgets\pjax::begin(['id' => 'categories']);      $form = activeform::begin();      echo $form->field($searchmodel, 'category')                 ->dropdownlist(                     arrayhelper::map($allcategory, 'id', 'name'),                     [                         'onchange'=>'getsubcategory()',                     ]     );      //to stop errors, if first category not chosen make subcategory , empty drop down.     $subcategory = array(         "empty" => ""      );      echo $form->field($searchmodel, 'subcategory')                 ->dropdownlist(                     arrayhelper::map($subcategory, 'id', 'name'),                     [                        'onchange'=>'getchildcategory()',                     ]     );     //to stop errors, if second category not chosen make childcategory , empty drop down.     $childcategory = array(         "empty" => ""     );     echo $form->field($searchmodel, 'childcategory')                 ->dropdownlist(                     arrayhelper::map($childcategory, 'id', 'name'),                     [                        //'onchange'=>'getchildcategory()',                         'onchange'=>'submitnow()',                     ]     );      activeform::end();     yii\widgets\pjax::end(); 

so happens when first category selected runs "onchange" => getsubcategory. send ajax request controller value of selected option. pull subcategories subcategory_id = value of selected option. populated subcategory drop down information.

this function on _categories.php category drop downlists above

 function getsubcategory(){         //#casesearch-category first drop down list         var firstcategory = $('#casesearch-category').val();             var childcategory = document.getelementbyid('casesearch-childcategory');             childcategory.options.length = 0;             $.ajax({                 url: '<?php echo \yii::$app->geturlmanager()->createurl('cases/subcategories') ?>',                 type: 'post',                 datatype: 'json',                 data: {                      firstcategory: firstcategory                  },                 success: function(data) {                            var subcategory = document.getelementbyid('casesearch-subcategory');                     //if select changed again, make options length 0 gets rid of previous appends.                     subcategory.options.length = 0;                       for(var i=0; i<data.length; i++){                         subcategory.options[i] = new option (data[i].name);                         subcategory.options[i].value = data[i].subcategory_id;                     }                     subcategory.options.selectedindex = -1;                     if(subcategory.options.length === 1){                         getchildcategory();                     }                      }             });     } 

so when ajax request reaches controller this:

casescontroller.php

public function actionsubcategories() {    if(isset($_post['firstcategory'])){         $firstcategory = $_post['firstcategory'];         // select * `subcategory` `parent_id` = $firstcategory         $subcategory = subcategory::findsubcategory($firstcategory);      }      return \yii\helpers\json::encode($subcategory); } 

okay little bit understand category side of things. have gridview populated database when page submitted. have done ajax categories need gridview change pjax when ever categories changed.

so in controller actionindex sends through searchmodel , dataprovider gridview so:

casescontroller.php

public function actionindex() {      $model = new cases;     $searchmodel = new casesearch();       $allcategory = category::find()->all();      $dataprovider = $searchmodel->search(yii::$app->request->queryparams);      return $this->render('index', [         'searchmodel' => $searchmodel,         'dataprovider' => $dataprovider,         'allcategory' => $allcategory,         'model' => $model     ]); } 

then on index page displays grid view here::

note::index.php renders category dropdownlists seen above _categories.php

 <?= $this->render('_categories', [     'model' => $model,     'searchmodel' => $searchmodel,     'allcategory' => $allcategory ]) ?>  <?php pjax::begin(['id' => 'cases']) ?> <?= gridview::widget([     'dataprovider' => $dataprovider,     'filtermodel' => $searchmodel,     'columns' => [         'case_id',         'name',         'judgement_date',         'year',         'neutral_citation',         ['class' => 'yii\grid\actioncolumn'],     ], ]); ?> <?php pjax::end() ?> 

ok here part stuck! assume meant somehow update searchmodel , dataprovider gridview unsure how this. if send ajax request controller change it have render page again defeats objective.

at top of _categories.php

function submitnow(){             $.pjax.reload({container:"#cases"});  //reload gridview         } 

this function called when last childcategory selected.i know have happen here not know what. can help?


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -