【问题标题】:How to set defalut value in CGridView如何在 GridView 中设置默认值
【发布时间】:2013-04-15 05:14:40
【问题描述】:

如果 $data->pic=="" 在我的 dataprovider 值中,我如何设置默认值。设置数据图片为na.jpg

widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
    array(
        'type'=>'raw',
        'name'=>'pic',
        'value'=>'CHtml::image("http://localhost/studentpics/".$data->pic)',
    ),
));

【问题讨论】:

    标签: yii cgridview


    【解决方案1】:
    widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
     array(
                        'name'=>'your Image URL',
                        'type'=>'image',
                        'htmlOptions'=>array('width'=>'30px','height'=>'30px'),
                ),
    ));
    

    【讨论】:

      【解决方案2】:

      value 被评估为 php 表达式,因此可以在其中使用条件

      'value'=>'CHtml::image(($data->pic)?"http://localhost/studentpics/".$data->pic:"default_image_url")',
      

      P.S 在任何地方都使用绝对网址是个坏主意。

      【讨论】:

        【解决方案3】:

        我认为你可以重写 CActiveRecord 的 afterFind 方法

        protected function afterFind()
        {
         if($this->pic ===null)
           {
            $this->pic = na.jpg;
           }
          parent::afterFind();
        }
        

        【讨论】:

          猜你喜欢
          • 2015-10-10
          • 2011-08-19
          • 2010-11-14
          • 2021-09-22
          • 2020-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多