首页 > 解决方案 > YII gridview 过滤器不工作

问题描述

我完全迷失在 Yii gridview 过滤器中。视图页面上有一个gridview,即student/view。

这是来自 StudentController 的控制器:

/**
 * Displays a particular model.
 * @param integer $id the ID of the model to be displayed
 */
public function actionView($id) {
    $this->layout = "//layouts/column1";
    $this->render('view', array(
        'model' => $this->loadModel($id),
    ));
}

但!我想在 HOUR 模型(不是学生模型)的网格视图中显示和过滤给定学生的小时数。所以这里是视图文件的一部分:

<?php
$hour = new Hour();
$hour->student_id = $model->id;
$hour->time_start = $startDate;

$hour->time_end = $endDate;
?>
<?php
$this->widget('bootstrap.widgets.BsGridView', array(
    'id' => 'hour-grid',
//  'dataProvider' => $hourModel->StudentHoursSearch($model->id),
    'dataProvider' => $hour->search(),
    'filter'=>$hour,
    'columns' => array(
        //'student_id',
        array(
            'name' => 'instructor_id',
            'value' => '$data->instructor->name',
            'filter'=>CHtml::listData(Instructor::model()->findAll(), 'id', 'name'),
        ),
        array(
            'name' => 'time_start',
            'value' => 'date("Y-m-d", strtotime($data->time_start))."<br />".date("H:i", strtotime($data->time_start))',
            'type' => 'html',
        ),
        'hour_topic',
        array(
            'name' => 'type',
            'type' => 'html',
            'value' => '$data->ticket_lease == "lease" ? $data->getTypeLabel() : ($data->ticket_lease == "ticket" ? $data->getTypeLabel() . " (jegy)" : ($data->cancel_type == 3 ? $data->getTypeLabel() . "<br />DK lemondás" : (date("Y-m-d", strtotime($data->time_start)) == date("Y-m-d") ? $data->getTypeLabel() : $data->getTypeLabel() . " <br/>Nincs lezárva")))',
            'filter' => Hour::$type_labels
        ),
    /* array(
      'name' => 'hour_topic',
      'value'=>'$data->hour_topic'
      ), */
    ),
    'rowCssClassExpression' => '                    
        $data->cancel_type == 3 ? "drumkiller_cancel" : ($data->type === "presenter" ? "student_presenter" : ($data->type === "training" ? "student_training" : "student_practice"))                    
    ',
));
?>

这很好地显示了学生的所有时间,但过滤功能不起作用。

在此处输入图像描述

我想使用这两个下拉过滤器,但它们都不起作用。

这是 Hour 的模型:

/**
 * Retrieves a list of models based on the current search/filter conditions.
 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
 */
public function search() {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria = new CDbCriteria;

    $criteria->with = array(
        'instructor',
        'student',
        // 'drumKit',
        'leaseOrder',
    );

    $criteria->compare('t.id', $this->id);
    $criteria->compare('t.instructor_id', $this->instructor_id);
    $criteria->compare('t.student_id', $this->student_id, true);
    // $criteria->compare('t.drum_kit_id', $this->drum_kit_id);
    $criteria->compare('t.lease_order_id', $this->lease_order_id);
    $criteria->compare('t.price', $this->price);
    $criteria->compare('t.type', $this->type, true);
    $criteria->compare('t.create_time', $this->create_time, true);
    $criteria->compare('t.hour_topic', $this->hour_topic, true);
    if (isset($_GET['time_start']) || isset($_GET['time_end'])) {
        $criteria->compare('t.time_start', '>=' . date('Y-m-d', strtotime($_GET['time_strat'])));
        $criteria->compare('t.time_end', '<=' . date('Y-m-d', strtotime($_GET['time_end'])));
    } else {

        if ($this->time_start && $this->time_end) {
            $criteria->compare('t.time_start', '>=' . $this->time_start);
            $criteria->compare('t.time_end', '<=' . $this->time_end);
        } else {
            $criteria->compare('t.time_start', $this->time_start, true);
            $criteria->compare('t.time_end', $this->time_end, true);
        }
    }
//  $criteria->compare('cancel_type', array(0,3));
    $criteria->compare('cancel_type', $this->cancel_type, true);
    $criteria->addInCondition('cancel_type',array(0,3)); 


    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
        'sort' => array(
            'defaultOrder' => 't.time_start DESC',
        ),
    ));
}

谁能帮帮我,好吗?

标签: gridviewfilteryii

解决方案


也许您需要从控制器而不是视图传入 Hour 类,然后确保从 gridview 将触发的 GET 参数中设置属性。

也许是这样的:

public function actionView($id) {
    $this->layout = "//layouts/column1";

    $hour = new Hour('search');
    $hour->unsetAttributes();  // clear any default values
    if (isset($_GET['Hour'])) {
        $hour->attributes = $_GET['Hour'];
    }

    $this->render('view', array(
        'model' => $this->loadModel($id),
        'hour' => $hour,
    ));
}

推荐阅读