php - Yii Update CGridView based on dropdown select -


found solution check end of post

i'm been working on 4 weeks , haven't made headway. have found tons of similar examples online , yet tried couldn't work. had burden community of questions.

i have model/table called tbl_symptoms/symptoms has 7 columns/attributes: id, symptomcode, title, shorttitle, inclusions, exclusions, symptomcategory.

i use dropdown select (which gets populated table on server has symptom categories in 1 column).

while can gridview populate symptoms, can't populate same category symptoms dropdown.

this last solution tried out (right it's not rendering gridview, when did had other problems, i'm posting code guys have point of reference potential solutions):

symptoms.php search function:

public function search() {     $criteria=new cdbcriteria;       $criteria->compare('symptomcategory',$this->symptomcategory,true);       return new cactivedataprovider($this, array(         'criteria'=>$criteria,     )); } 

sitecontroller.php functions i'm using:

//returns symptom categories user can choose pick symptom public static function getsymptomcategories() {     return chtml::listdata(yii::app()->db->createcommand()->select('category')->from('tbl_symptomcategory')->queryall(), 'category', 'category'); }  public function actionloadsymptoms() {     $symptomsmodel = new symptoms;      if (isset($_post)){         $symptomsmodel->attributes = $_post;         $dataprovider = $symptomsmodel->search();         $this->renderpartial('_searchsymptomsview', array('dataprovider' => $dataprovider));     } } 

view file _seachsymptomsview:

    <?php $this->widget('zii.widgets.grid.cgridview', array(     'id'=>'searchsymptomsgrid',     'dataprovider'=>$dataprovider,     'columns'=>array(         'symptomcode',         'title',         'inclusions',         'exclusions',         'symptomcategory',         )         ));         ?> 

search.php functions:

<!-- select symptom category dropdown menu --> <div class="search-form">         <?php              echo $form->labelex($model, 'symptomcategory');              echo $form->dropdownlist($model, 'symptomcategory',                      $this->getsymptomcategories(),                     array( 'id'=>'symptomselectdropdown',                            'ajax'=>array('type'=>'post',                                         'url'=>ccontroller::createurl('sitecontroller/loadsymptoms'),                                         'replace'=>'#symptomselectdiv'                             )));             ?>             <!-- select symptom --> <div class="row" id="symptomselectdiv">  </div> 

this code based on similar question asked here couldn't work else i've tried. appreciated, thank you.

solution came with (for future reference)

symptoms model search function: pretty default constructed function, commented out except symptomcategory wanted base search on.

search view file: (revelant stuff)

   yii::app()->clientscript->registerscript('search', "         $('#symptomselectdiv').hide();         $('#categoryselectdropdown').change(function(){             $('#symptomselectdiv').show();             $('#symptoms-grid').yiigridview('update', {                 data: $(this).serialize()             });             return false;         });  ");      ?>      <h1>welcome search symptoms page </h1>      <div class="form">     <?php $form=$this->beginwidget('cactiveform', array( 'id'=>'search-form', 'enableclientvalidation'=>true, 'clientoptions'=>array(     'validateonsubmit'=>true, ),     )); ?>     <p class="note">fields <span class="required">*</span> required.</p> <!-- select symptom category dropdown menu --> <div class="search-form">         <?php        $this->renderpartial('_searchcategory',array('model'=>$model)); ?> </div>              <!-- select symptom --> <div class="row" id="symptomselectdiv" >     <?php $this->widget('zii.widgets.grid.cgridview', array(             'id'=>'symptoms-grid',             'dataprovider'=>$model->search(),             'columns'=>array(                 'symptomcode',                 'title',                 'inclusions',                 'exclusions',                 'symptomcategory',              ),     )); ?> </div>    <div class="row buttons">     <?php  echo chtml::submitbutton('search');  ?> </div>       <?php $this->endwidget(); ?> 

_searchcategory.php view:

  <php $form=$this->beginwidget('cactiveform', array( 'action'=>yii::app()->createurl($this->route), 'method'=>'get',    )); ?> <!-- form automatically submitted when dropdown selection changes --> <div class="row">     <?php echo $form->label($model,'symptomcategory'); ?>     <?php echo $form->dropdownlist($model, 'symptomcategory',                                             $this->getsymptomcategories(),                                             array('submit'=>'',                                                   'id'=>'categoryselectdropdown',                                                   'prompt'=>"select symptom category")); ?>  </div>      <?php $this->endwidget(); ?> 

the solution found, breaks mvc structure. way(skipping solution), did not use filter in gridview.

by default cgridview uses text input @ header perform filtering. if want use dropdown must change column's filter below:

$this->widget('zii.widgets.grid.cgridview', array( 'id'=>'searchsymptomsgrid', 'dataprovider'=>$dataprovider, 'columns'=>array(     'symptomcode',     'title',     'inclusions',     'exclusions',     array(          'name'=>'symptomcategory',          'type' => 'raw',          'filter' => array('key'=>'value','key1'=>'value1'), //note 1          'value'=>'$data->symptomcategory' //note 2     )     )     )); 

note 1 in comments

this array shown in dropdown format in symptomcategory column header. can replace own array (with specific keys , values )

note 2 in comments

it shows value of symptomcategory column. can change own (for example format or else)

yii has powerful , comprehensive document. recommended take @ cgridview know more it. if take look, able perform more customization.

cgridview documents


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -