【问题标题】:CGridview filter by dropdown using related modelCGridview 使用相关模型通过下拉列表过滤
【发布时间】:2013-09-20 05:30:46
【问题描述】:

我有两个表: User 和 UserProfile 都有关系。即 UserProfile 属于用户。用户表有“is_active”列,以 0 和 1 格式存储特定用户的活动/非活动记录。

现在我有一个下拉字段,其中包含选项 Active(值 1)和 Inactive(值 0),我想根据活动和非活动用户列表过滤 CGridview 小部件。

以下是我正在做的事情:

<?php echo CHtml::dropDownList('UserProfile[is_active]', $model->user->is_active, 
  array(
    '1'=>"Active",
    '0'=>"Inactive",
  ), array('onchange'=>"$.fn.yiiGridView.update('yw0', {data: $(this).serialize()});") ); ?>

<?php
$this->widget('bootstrap.widgets.TbGridView', array(    
    'type' => 'striped bordered condensed',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'template' => '{pager}{items}',
    'enableSorting' => true,
    'columns' => array(
        'title.name',
        array(
            'name' => 'first_name',
            'type' => 'raw',
            'value' => 'CHtml::link($data->first_name,"/userProfile/$data->user_profile_id")'
        ),
        'last_name',
        'user.username',
        'user.email',        
        array(
            'class' => 'bootstrap.widgets.TbButtonColumn',
            'template' => '{update}&nbsp;&nbsp;&nbsp;&nbsp;{delete}',
        ),
    ),
    'pager' => array(
        'header' => '',
        'hiddenPageCssClass' => 'disabled',
        'maxButtonCount' => 3,
        'cssFile' => false,
        'class' => 'CLinkPager',
        'prevPageLabel' => '<i class="icon-chevron-left"></i>',
        'nextPageLabel' => '<i class="icon-chevron-right"></i>',
        'firstPageLabel' => 'First',
        'lastPageLabel' => 'Last',
    ),
    'pagerCssClass' => 'pagination',
));
?>

控制器:

public function actionManage() {

        $model = new UserProfile('search');        
        $model->unsetAttributes();  // clear any default values

        if (isset($_GET['UserProfile'])) {            
            $model->attributes = $_GET['UserProfile'];
        }

        $this->render('manage', array(
            'model' => $model,
        ));
    }

这是我的搜索方法:

public function search() {

        $criteria = new CDbCriteria;
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('middle_name', $this->middle_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->with = array('user');
        $criteria->join = 'LEFT JOIN AuthAssignment ON AuthAssignment.userid=user_id';
        if (isset($_GET['employee'])):
            $criteria->addCondition("AuthAssignment.itemname <> 'Customer'");
            $criteria->addCondition("user.is_active='$status'");
        else:
            $criteria->addCondition("AuthAssignment.itemname = 'Customer'");
            $criteria->addCondition("user.is_active='$status'");
        endif;

        //Rights::getAssignedRoles(Yii::app()->session['user_id']);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 10,
            ),
        ));
    }

这里我希望$status 变量值是动态的,因为下拉字段更改为活动或不活动。

当更改下拉选项时,它会显示以下错误:

TypeError: settings is undefined
$grid.addClass(settings.loadingClass);

我是 Yii 的新手,需要帮助。

谢谢

【问题讨论】:

    标签: php ajax jquery yii cgridview


    【解决方案1】:

    首先在配置文件模型中声明一个属性 user_status

    喜欢

    public $user_status;
    

    然后在profile model的搜索方法中

    $criteria->with = array( 'user' );
    $criteria->compare( 'user.is_active', $this->user_status, true );
    
    return new CActiveDataProvider($this, array(
          'criteria'=>$criteria,
          'sort'=>array(
            'attributes'=>array(
              'user_status'=>array(
                'asc'=>'user.is_active',
                'desc'=>'user.is_active DESC',
              ),
              '*',
            ),
          )
        ));
    

    然后在配置文件gridview里面的columns数组

    添加

    array(
          'name'=>'user_status',
          'header' => 'Status',
          'value'=>'$data->user->is_active',
          'filter' => array( '1'=>"Active",'0'=>"Inactive" )
          'type' => 'raw',
        ),
    

    不需要那个 chtml 下拉菜单

    【讨论】:

    • 它显示的下拉字段带有活动和非活动选项,但功能不起作用
    • 不要忘记在模型规则中添加user_status 属性作为安全(在搜索场景中)
    • @soju:当我单击从下拉列表中选择值时,它显示 ajax 错误:TypeError: settings is undefined [Break On This Error] $grid.addClass(settings.loadingClass);
    • 我已经用 'type' => 'raw' 更新了代码,这可能会解决
    • 我认为是jquery冲突问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多