gridview - 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',
),
));
}
谁能帮帮我,好吗?
解决方案
也许您需要从控制器而不是视图传入 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,
));
}
推荐阅读
- c# - Unity3D gameObject 标签对我不起作用
- java - 读取和写入同一个文本文件
- python - 使用 numpy 数组进行字典索引
- google-apps-script - Google Scripts On Edit 功能
- python - 在python中循环读取文件时跳过某些文件的行
- apache-kafka - 当我们在 kafka 流中的两个 KTable 之间进行外键连接时生成的主题
- machine-learning - ValueError:数据基数不明确。确保所有数组都包含相同数量的样本?
- javascript - 在嵌套对象中有多个重复值时如何仅获取第一个出现的键 - JavaScript
- rust - 使用 String.replace 替换 Rust 中字符串的多行部分
- javascript - 使用 JavaScript 重定向到新窗口,点击