如何使用日期选择器过滤 Gridview?

How do I can filtering Gridview using datepicker?

我在我的 gridview 中使用 Kartik DatePicker 小部件进行过滤。我需要显示日期介于 DateRange.

中选定日期范围内的所有数据

这是我的小部件的视图:

index.php

中的视图代码
<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>

这是我在 OutController.php 控制器中的操作代码:

$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,
    ]);

而且我还在我的 searchModel 中设置了 public 变量,

这是我在 OutSearch.php

中的 searchModel 代码
public $start_date;
public $end_date;
public function rules() {
    return [
        [['start_date', 'end_date'], 'safe'],
    ];
}
 public function search($params) {
    $query = Out::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'faktur_out_id' => $this->faktur_out_id,
        'user_id' => $this->user_id,
        'parent_id' => $this->parent_id,
        'updated_at' => $this->updated_at,
        'created_at' => $this->created_at,
        'status' => $this->status,
    ]);

    $query->andFilterWhere(['like', 'kode_jenis', $this->kode_jenis])
            ->andFilterWhere(['like', 'fg_pengganti', $this->fg_pengganti])
            ->andFilterWhere(['between', 'tanggal_faktur', $this->start_date, $this->end_date])
            // I also have set   ->andFilterWhere(['>=', 'tanggal_faktur', $this->start_date])
            //                   ->andFilterWhere(['<=', 'tanggal_faktur', $this->end_date])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by])
            ->andFilterWhere(['like', 'created_by', $this->created_by]);

    return $dataProvider;
}

我尝试在动作控制器中回显,它成功并给我正确的结果。但它不过滤。

我怎样才能使过滤起作用?我的代码有什么问题吗?

谢谢。

请更换控制器

//        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;

            }

yii2 正确使用mode.you应该使用ActiveFormclass!在 OutSearch.php search() 函数中添加以下内容。

if (!empty($this->start_date)){
    $query->andFilterWhere(['>=', 'start_date', $this->start_date]);
}
if (!empty($this->end_date)){
       $query->andFilterWhere(['<=', 'end_date', $this->end_date]);
}