Revision #201 has been created by rackycz on Jun 30, 2020, 10:39:08 PM with the memo:
GridView + DatePicker
« previous (#200) next (#202) »
Changes
Title
unchanged
Yii v2 snippet guide
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,beginner,yii2
Content
changed
[...]
<link rel="apple-touch-icon" sizes="180x180" href="https://www.yiiframework.com/favico/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="https://www.yiiframework.com/favico/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="https://www.yiiframework.com/favico/favicon-16x16.png">
```
**GridView + DatePicker in filter + filter reset**
---
If you are using DatePicker as described above, you can use it also in GridView as a filter, but it will not work properly. Current filter-value will not be visible and resetting the filter wont be possible. Use following in views/xxx/index.php to solve the issue:
```php
function getDatepickerFilter($searchModel, $attribute) {
$name = basename(get_class($searchModel)) . "[$attribute]";
$result = \yii\jui\DatePicker::widget(['language' => 'en', 'dateFormat' => 'php:Y-m-d', 'name'=>$name, 'value'=>$searchModel->$attribute, 'options' => ['class' => 'form-control'] ]);
if (trim($searchModel->$attribute)!=='') {
$result = '<div style="display:flex;flex-direction:column">' . $result
. '<div class="btn btn-danger btn-xs glyphicon glyphicon-remove" onclick="$(this).prev(\'input\').val(\'\').trigger(\'change\')"></div></div>';
}
return $result;
}
// ...
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
// ...
[
'attribute' => 'myDateCol',
'value' => 'myDateCol',
'label'=>'My date label',
'filter' => getDatepickerFilter($searchModel,'myDateCol'),
'format' => 'html'
],
// ...
```