0
votes

I'm using Kartik DatePicker widget in my gridview for filtering. I need to show all data that has date between range that has been set from selected date in DateRange.

This is the view of my widget:

enter image description here

Code for the view in 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>

and this is my actioncode in controller in 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,
    ]);

and I also have set public variable in my searchModel,

This is my code for searchModel in OutSearch.php

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

I've tried to echo in action controller, and it successfuly and give me right result. But it doesn't filtering.

How do I can make the filtering work? Anything wrong with my code?

Thanks.

2

2 Answers

1
votes

please change in controller

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

            }
0
votes

yii2 Correct use mode.you should use ActiveForm class! in OutSearch.php search() function add following contents.

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