YII gridview过滤器无法正常工作

时间:2018-05-16 08:38:24

标签: gridview filter yii

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

这是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"))                    
    ',
));
?>

这显示了学生的所有时间,但过滤功能无效。

enter image description here

我想使用两个下拉过滤器,但它们都没有工作。

这是小时模型:

/**
 * 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',
        ),
    ));
}

任何人都可以帮帮我吗,请问?

1 个答案:

答案 0 :(得分:0)

也许你需要从控制器而不是视图传递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,
    ));
}