简体   繁体   English

是否可以在yii 1的下拉列表中放置搜索过滤器?

[英]Is it possible to put a search filter on dropdown list in yii 1?

I have a dropdownlist in my form... The content of it came from a different model... Here's my code 我的表单中有一个下拉列表...其内容来自其他模型...这是我的代码

<?php echo $form->labelEx($model,'pr_id'); ?>
<?php echo $form->dropDownList($model,'pr_id', CHtml::listData(PatientRecord::model()->findAll(), 'pr_id', 'first_name'), array('empty'=>'Select Patient')); ?>
<?php echo $form->error($model,'pr_id'); ?>

Suppose that there are a lot of data from that table, I think the user wouldn't be happy scrolling over choices... is there a way to put in a search filter on the dropdown? 假设该表中有很多数据,我认为用户对滚动选择不满意……是否有办法在下拉菜单中放入搜索过滤器? I think it's possible in Yii2 but I'm using Yii1 at the moment for a project so... any suggestions? 我认为Yii2中有可能,但目前我正在为一个项目使用Yii1,所以...有什么建议吗?

Also, is there a way to show both the first_name and last_name on the display of the dropdownlist? 另外,有没有办法在下拉列表的显示屏上同时显示first_name和last_name?

sorry for the beginner question any help would be highly appreciated :D 对初学者的问题感到抱歉,任何帮助将不胜感激:D

You could use this Select2 Extension , download the extension and place it under /protected/extensions directory. 您可以使用此Select2 Extension ,下载该扩展并将其放在/protected/extensions目录下。

Just add it in your view, something like: 只需在您的视图中添加它,就像:

<?php 
    $this->widget('ext.select2.ESelect2',array(
      'model'=>$model,
      'attribute'=>'pr_id',
      'data'=>$model->searchPatient(),//function to provide data
      // or
      //'data'=>CHtml::listData(PatientRecord::model()->findAll(), 'id', 'first_name')
    ); 
?>

and in your model: 并在您的模型中:

<?php
    public function searchPatient() {
        $data         = array();
        $patientModel = PatientRecord::model()->findAll();
        foreach($patientModel as $patient){
            $data[] = $patient->first_name." ".$patient->last_name;
        }
        return $data;
    }
?>

Thats all you need to do, hope that helps :) 那就是您需要做的所有事情,希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM