我在gridview中使用Kartik DatePicker小部件进行过滤。我需要显示所有数据,这些数据的日期介于 DateRange 中从所选日期设置的范围之间。
rehash
<table>
<tbody>
<tr>
<td style="width: 390px"></td>
<td style="width: 390px">
<?php
$recentDate = NULL;
if (isset($_SESSION['start_date'])) {
$recentDate = $_SESSION['start_date'];
}
if (isset($_SESSION['end_date'])) {
$recentDate = $_SESSION['end_date'];
}
echo '<label>Select Date</label>';
echo DatePicker::widget([
'name' => 'start_date',
'name2' => 'end_date',
'type' => DatePicker::TYPE_RANGE,
'value' => $recentDate,
'options' => ['placeholder' => 'Start ...'],
'options2' => ['placeholder' => 'End ...'],
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'todayHighlight' => true,
'autoclose' => true,
]
]);
?>
</td>
<td style=" width: 390px"></td>
</tr>
<tr>
<td style="width: 390px"></td>
<td style="width: 390px"><?= Html::submitButton('<i class="glyphicon glyphicon-search"></i> SEARCH', ['class' => 'btn btn-success-custom-search'], ['out/index']) ?></td>
<td style="width: 390px"></td>
</tr>
</tbody>
</table>
我也在searchModel中设置了公共变量,
$searchModel = new OutSearch();
$start_date = "";
$end_date = "";
if (isset($_POST['start_date'])) {
$start_date = $_POST['start_date'];
$searchModel->tanggal_faktur = $start_date;
$_SESSION['start_date'] = $start_date;
if (isset($_POST['end_date'])) {
$end_date = $_POST['end_date'];
$searchModel->tanggal_faktur = $end_date;
$_SESSION['end_date'] = $end_date;
}
}
$userId = Yii::$app->user->id;
$searchModel->user_id = $userId;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->pageSize = 100;
return $this->render('index', [
'searchModel' => $searchModel,
'start_date' => $start_date,
'end_date' => $end_date,
'dataProvider' => $dataProvider,
]);
我试图在动作控制器中回应,并且它成功地给了我正确的结果。但它没有过滤。
感谢。
答案 0 :(得分:1)
请更改控制器
// if (isset($_POST['start_date'])) {
// $_SESSION['start_date'] = $_POST['start_date'];
// unset($_POST['start_date']);
// }
// if (isset($_POST['end_date'])) {
// $_SESSION['end_date'] = $_POST['end_date'];
// unset($_POST['end_date']);
// }
$start_date = $searchModel->start_date;
$end_date = $searchModel->end_date;
if (isset( $_POST['start_date'])) {
$start_date = $_POST['start_date'];
$searchModel->start_date = $start_date;
$end_date = $_POST['end_date'];
$searchModel->end_date = $end_date;
}
答案 1 :(得分:0)
yii2正确使用模式。你应该使用ActiveForm类!在OutSearch.php中搜索()函数添加以下内容。
if (!empty($this->start_date)){
$query->andFilterWhere(['>=', 'start_date', $this->start_date]);
}
if (!empty($this->end_date)){
$query->andFilterWhere(['<=', 'end_date', $this->end_date]);
}