doushan1157 2014-02-09 07:24
浏览 7
已采纳

条件下拉列表Yii中的文本值

I have a drop down list but I the text value is dependent on another value from the same model.

What I want to happen is if the row from a model has a client_type_id value of 1 then the drop down text should be company_name else it will be the first_name. Kindly refer to the code below.

<?php echo $form->dropDownListRow($model , 'client_id', CHtml::listData(Client::model()->findAll(), 'id', '$data->client_type_id == 1 ? $data->company_name : $data->first_name')); ?>

Is it even possible to achieve it like this?

  • 写回答

1条回答 默认 最新

  • dongshijiao6890 2014-02-09 08:07
    关注

    PHP 5.3+ and Yii 1.1.13+

    You can use an anonymous function:

    echo $form->dropDownListRow($model , 'client_id', 
        CHtml::listData(Client::model()->findAll(), 'id', function($data){
            return $data->client_type_id == 1 ? $data->company_name : $data->first_name
        })
    );
    

    Yii < 1.1.13 and/or PHP <5.3

    You can use the CActiveRecord::afterFind() method to initialize a variable say $list_info and use this as your field:

    class MyClass extends CActiveRecord{
        public $list_info;
        ...
    
        public function afterFind(){
            $this->list_info=$this->client_type_id == 1 ? $this->company_name : $this->first_name
        }
    }
    

    The drop down list then becomes

    $form->dropDownListRow($model , 'client_id', CHtml::listData(
        Client::model()->findAll(), 'id', 'list_info')
    );
    

    Reference: http://www.yiiframework.com/doc/api/1.1/CHtml#listData-detail

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

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了