【问题标题】:CGridView custom column filterCGridView 自定义列过滤器
【发布时间】:2012-08-04 09:38:32
【问题描述】:

问:如何为我的 gridview 创建过滤器?

状态:客户名称 = first_name。姓氏

这是我的网格视图

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'customer-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        array(
              'header'=>'Customer Name',
              'name'=>'$data->first_name',
              'value'=>'$data->first_name.\' \'.$data->last_name',
              ),        
        'company_name',
        'country',
        'state',
        'city',     
        'address1',         
        'phone1',       
        'email',        
        array('name' => 'company_id',
               'value'=>'$data->companies->name',
               'filter'=>CHtml::listData($records, 'id', 'name'),
        ),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

【问题讨论】:

    标签: php yii filter cgridview


    【解决方案1】:

    在模型中创建一个变量

    class Customer extends CActiveRecord
    {
        public $customer_name;
        public function search()
        {            
            $criteria->compare('CONCAT(first_name, \' \', last_name)',$this->customer_name,true);            
        }
    }
    

    在视图中

    <?php $this->widget('zii.widgets.grid.CGridView', array(
            'id'=>'customer-grid',
            'dataProvider'=>$model->search(),
            'filter'=>$model,
            'columns'=>array(
                array(           
                    'name'=>'customer_name',
                    'value'=>'ucwords($data->first_name.\' \'.$data->last_name)',
                      ),        
                'company_name',
                'country',
                'state',
                'city', 
                'address1',
                'phone1',
                'email',
                array(
                    'class'=>'CButtonColumn',
                ),
            ),
        )); ?>
    

    并且不要忘记在模型的 rules() 方法中将新属性声明为“安全”,否则您的输入将不会被考虑

    public function rules()
    {
        return array(
        /* ... */
            array('customer_name', 'safe', 'on'=>'search'),
        );
    }
    

    【讨论】:

      【解决方案2】:

      第一个变化:

      'name'=>'$data->first_name',
      

      到:

      'name'=>'first_name',
      

      在模型的search 方法中,您必须添加LIKE 条件:

      $model->addSearchCondition( 'CONCAT( first_name, " ", last_name )', $this->first_name );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-15
        相关资源
        最近更新 更多