douhan4812 2014-05-18 16:27
浏览 31
已采纳

Yii基于下拉选择更新CGridView

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(); ?>
  • 写回答

1条回答 默认 最新

  • dpjr86761 2014-05-18 22:13
    关注

    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.

    CGridView Documents

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度