using kartik date range picker for filtering in gridview

Hi I am trying to use the Kartik Date Range picker for filter in gridview.

I have a column date_time field as discharge_date, the widget is showing fine in the gridview but the filtering is not working at all.

This is my code in the Gridview:


[

                'attribute'=>'discharge_date',

                'value'=>'discharge_date',                

                'filterType' => GridView::FILTER_DATE_RANGE,

                'filterWidgetOptions' =>([

                'model'=>$model,

                'attribute'=>'discharge_date',

                'presetDropdown'=>TRUE,                

                'convertFormat'=>true,                

                'pluginOptions'=>[                                          

                    'format'=>'Y-m-d',

                    'opens'=>'left'

                ]

            ])


            ],




Any idea, where I am going wrong.

Thanks.

I have achieved that with the below setup, based on ideas from this link : demos.krajee.com/date-range (check a comment below from Claudio).

[

    'attribute' => 'date',


    'format'=>'date',


    'filterType'=> \kartik\grid\GridView::FILTER_DATE_RANGE,


    'filterWidgetOptions' => [


    'presetDropdown' => true,


    'pluginOptions' => [


    'format' => 'YYYY-MM-DD',


    'separator' => ' TO ',


    'opens'=>'left',


    ] ,


    'pluginEvents' => [


    "apply.daterangepicker" => "function() { aplicarDateRangeFilter('date') }",


    ]    


    ],


    ]

It doesn’t work.

I have gone through the link, but it looks like it uses non English language and some additional code then ur suggestion, which is little difficult to grasp how to adapt that code.

Can you please provide some more detail how you have implemented it.

Thanks.

I made changes to both my index view and search model: Am using Kartik’s select 2 widget for the dropdown filter and the daterange filter for the date filter.

this is how my search model looks like:


public function search($params)

{


    $query = Contributions::find();


    $query->joinWith(['member']);


    $dataProvider = new ActiveDataProvider([


        'query' => $query,


    ]);


    $this->load($params);


    if (!$this->validate()) {


        // uncomment the following line if you do not want to any records when validation fails


        // $query->where('0=1');


        return $dataProvider;


    }


    $query->andFilterWhere([


        'contributionid' => $this->contributionid,


        'amount' => $this->amount,


    ]);


    $query->andFilterWhere(['like', 'members.memberid', $this->memberid]);


    $query->andFilterWhere(['like', 'comment', $this->comment]);


    if(isset($this->date) && $this->date!=''){


    $date_explode = explode("TO", $this->date);


    $date1 = trim($date_explode[0]);


    $date2=  trim($date_explode[1]);


    $query->andFilterWhere(['between', 'date', $date1,$date2]);


    }


    return $dataProvider;


}

below is my index view file:


<?php

use yii\helpers\Html;

//use yii\grid\GridView;

use kartik\grid\GridView;

/* @var $this yii\web\View */

/* @var $searchModel frontend\models\ContributionsSearch */

/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = ‘Contributions’;

$this->params[‘breadcrumbs’][] = $this->title;

?>

<div class="contributions-index">

&lt;h1&gt;&lt;?= Html::encode(&#036;this-&gt;title) ?&gt;&lt;/h1&gt;


&lt;?php // echo &#036;this-&gt;render('_search', ['model' =&gt; &#036;searchModel]); ?&gt;





&lt;p&gt;


    &lt;?= Html::a('Create Contributions', ['create'], ['class' =&gt; 'btn btn-success']) ?&gt;


&lt;/p&gt;





&lt;?php 


use frontend&#092;models&#092;Members;


&#036;members=  Members::find()-&gt;all();


use yii&#092;helpers&#092;ArrayHelper;


&#036;items=  ArrayHelper::map(&#036;members, 'memberid', 'name');    


// Generate a bootstrap responsive striped table with row highlighted on hover


&#036;gridColumns=[


    ['class' =&gt; 'kartik&#092;grid&#092;SerialColumn'],        


    [


        'vAlign'=&gt;'middle',


        'width'=&gt;'180px',


        'value'=&gt;'member.name',


        'attribute' =&gt; 'memberid',


        'filter'=&gt;ArrayHelper::map(Members::find()-&gt;orderBy('name')-&gt;asArray()-&gt;all(), 'memberid', 'name'),  


        'filterType' =&gt; GridView::FILTER_SELECT2,


        'filterWidgetOptions'=&gt;[


        'pluginOptions'=&gt;['allowClear'=&gt;true],


        ],


        'filterInputOptions'=&gt;['placeholder'=&gt;'Select Member'],


        'format'=&gt;'html',


    ], 


    [


        'class' =&gt; '&#092;kartik&#092;grid&#092;DataColumn',


        'attribute' =&gt; 'amount',


        'format'=&gt;['decimal',2],    


        'pageSummary' =&gt; true


    ],


    'comment',


    [


    'attribute' =&gt; 'date',


    'format'=&gt;'date',


    'filterType'=&gt; &#092;kartik&#092;grid&#092;GridView::FILTER_DATE_RANGE,


    'filterWidgetOptions' =&gt; [


    'presetDropdown' =&gt; true,


    'pluginOptions' =&gt; [


    'format' =&gt; 'YYYY-MM-DD',


    'separator' =&gt; ' TO ',


    'opens'=&gt;'left',


    ] ,


    'pluginEvents' =&gt; [


    &quot;apply.daterangepicker&quot; =&gt; &quot;function() { apply_filter('date') }&quot;,


    ]    


    ],


    ],


    ['class' =&gt; 'kartik&#092;grid&#092;ActionColumn']


];


echo GridView::widget([


    'dataProvider'=&gt; &#036;dataProvider,


    'filterModel' =&gt; &#036;searchModel,


    'filterRowOptions'=&gt;['class'=&gt;'kartik-sheet-style'],


    'columns' =&gt; &#036;gridColumns,


    'responsive'=&gt;true,


    'resizableColumns'=&gt;true,


    'floatHeader'=&gt;true,


    'showPageSummary' =&gt; true,


    'hover'=&gt;true,


    'pjax'=&gt;true,


    'pjaxSettings'=&gt;[


    'neverTimeout'=&gt;true,

]

]);


?&gt;

</div>

<script type="text/javascript">

function apply_filter() {

$(’.grid-view’).yiiGridView(‘applyFilter’);

}

</script>


This may not be the most efficient implementation but it works and I would like to get more information and suggestions on this too. The javascript function at the end of the index view file helps to reload the grid once a selection of daterange is changed.Hope this helps