Answer the question
In order to leave comments, you need to log in
Yii2-date-range usage example in GridView?
I use kartik-v/yii2-date-range
in my project .
The question arose, how to actually use it in the GridView?
Tried like this:
Search:
...
public function behaviors()
{
return [
[
'class' => DateRangeBehavior::className(),
'attribute' => 'createTimeRange',
'dateStartAttribute' => 'createTimeStart',
'dateEndAttribute' => 'createTimeEnd',
]
];
}
public function rules()
{
return [
[['id', 'card_id', 'card_type_id', 'balance', 'created_at', /*'expires_at',*/
'corporative', 'barter', 'active', 'countNew', 'status'], 'integer'],
[['user_id'], 'safe'],
[['createTimeRange'], 'match', 'pattern' => '/^.+\s\-\s.+$/'],
];
}
public function search($params)
{
...
$query->andFilterWhere(['>=', 'expires_at', Date('Y-m-d 00:00:00', strtotime($this->createTimeStart))])
->andFilterWhere(['<', 'expires_at', Date('Y-m-d 00:00:00', strtotime($this->createTimeEnd))]);
...
}
...
...
[
'attribute' => 'createTimeRange',
'value' => function ($model, $index, $widget) {
return date('Y-m-d', strtotime($model->expires_at));
},
//'format' => 'date',
'width' => '20%',
'filterType' => \kartik\grid\GridView::FILTER_DATE_RANGE,
'filterWidgetOptions' => [
'convertFormat' => false,
'presetDropdown' => true,
'pluginOptions' => [
'format' => 'Y-m-d',
'autoclose' => true,
]
]
],
...
Answer the question
In order to leave comments, you need to log in
What does DateRangeBehavior have to do with it?
Demos.krajee.com/date-range#usage is full of examples of using DateRangePicker . In particular, for filtering it is more convenient to use the example "Advanced configuration using separate start and end attributes to store information". Further filtering is no different from any other field.
Good morning.
In the GridView itself, you can use it like this:
[
'attribute' => 'created_at',
'format' => 'datetime',
'filter' => DatePicker::widget([
'model' => $searchModel,
'attribute' => 'date_from',
'attribute2' => 'date_to',
'type' => DatePicker::TYPE_RANGE,
'separator' => '-',
'pluginOptions' => ['format' => 'yyyy-mm-dd']
])
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question