Reputation: 833
FOUND A SOLUTION CHECK END OF THIS POST
I'm been working on this for 4 weeks and I haven't made any headway. I have found tons of similar examples online and yet everything I tried I couldn't get to work. So I had to burden the community with another of my questions.
I have a model/table called tbl_symptoms/Symptoms which has 7 columns/attributes: id, symptomCode, title, shortTitle, inclusions, exclusions, symptomCategory.
I use a dropdown select (which gets populated from a table on the server that just has the symptom categories in one column).
While I can get the gridview to populate with ALL the symptoms, I can't get it to only populate with the same category symptoms as the dropdown.
This is the last solution I tried out (right now it's not even rendering the gridview, but even when it did I had other problems, so I'm just posting the code so you guys have a point of reference about 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 that the user can choose to pick a 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 is based on a similar question asked here but I couldn't get it to work like everything else I've tried. Any help is greatly appreciated, thank you.
SOLUTION I CAME UP WITH (for future reference)
Symptoms model search function: pretty much the default constructed function, but I commented out everything except the symptomCategory which is what I wanted to base my 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 to the search for 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 with <span class="required">*</span> are 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 is 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(); ?>
Upvotes: 0
Views: 1588
Reputation: 14459
The solution you found, breaks MVC structure. By the way(skipping that solution), you did not use a filter
in your gridView
.
By default CGridView
uses text input at header to perform filtering. If you want to use a dropDown
you must change your column's filter like 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 will be shown in a dropdown
format in symptomCategory
column header. You can replace it with your own array (with specific keys and values )
Note 2 in comments
It shows the value of symptomCategory
column. You can change it with your own (for example format it or something else)
Yii has a powerful and comprehensive document. It is recommended to take a look at CGridView to know more about it. If you take a look, you will definitely be able to perform more customization.
Upvotes: 1