【问题标题】:YII gridview filter not workingYII gridview 过滤器不工作
【发布时间】:2018-05-16 08:38:23
【问题描述】:

我完全迷失在 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"))                    
    ',
));
?>

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

enter image description here

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

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

谁能帮帮我,好吗?

【问题讨论】:

    标签: gridview filter yii


    【解决方案1】:

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

    【讨论】:

    • 谢谢!它没有解决我的问题。我认为问题出在 ajax 更新后没有重新初始化。如果我使用过滤器(例如选择小时类型或开始日期),表格会刷新,但我不能使用其他过滤器。我希望我能描述得清楚一些
    • 我确实看到您正在使用引导小部件。
    • 我不小心按回车太快了,这是我想要的评论:我确实看到您正在使用“bootstrap.widgets.BsGridView”小部件。对不起,我对那个不熟悉。我会尝试使用默认的 yii CGridView 小部件:'zii.widgets.grid.CGridView'。您可以尝试通过 Gii 工具生成代码以使过滤默认工作,然后从那里修改它。如果我不能提供进一步的帮助,我很抱歉。 yiiframework.com/forum/index.php/topic/…
    • 谢谢!我按照你的建议试试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多