Yii2 gridview日期范围选择器过滤器将不起作用

问题描述 投票:0回答:1

我已在我的表格视图中添加了kartik-v日期范围选择器,但选择日期后该过滤器无法工作。小部件的占位符将显示选定的日期,但gridview中的结果不匹配。 gridview中的结果显示所有数据。

视图中的代码:

<?= GridView::widget([
    'options' => ['class' => 'table-sm'],
    'filterModel' => $searchModel,
    'dataProvider' => $dataProvider,
    'summary' =>'',
    'columns' => [
        [
            'attribute' => 'price',
            'value' => function ($model) {
                return number_format($model->price, 3);
            },
            'contentOptions' => ['class' => 'text-right']
        ],
        [
            'attribute' => 'date',
            'contentOptions' => ['class' => 'text-right'],
            'filter' => DateRangePicker::widget([
                'name'=>'date_range_2',
                'presetDropdown'=>true,
                'convertFormat'=>true,
                'includeMonthsFilter'=>true,
                'attribute' => 'date',
                'model' => $searchModel,
                'pluginOptions' => ['locale' => ['format' => 'Y-m-d']],
                'options' => ['placeholder' => 'Select Date']
            ])
        ],
    ]
]); ?>

下面的代码是我的$ searchModel部分。更新时间:

public function rules()
{
    return [
        [['id'], 'integer'],
        [['name', 'date'], 'safe'],
        [['price'], 'number'],
    ];
}

/**
 * {@inheritdoc}
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = Lme::find()->orderBy([
        'date' => SORT_DESC
    ]);

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => false //['defaultOrder' => ['date' => SORT_DESC]]
    ]);

    $this->load($params);

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

        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
        'price' => $this->price,
        'date' => $this->date,
    ]);

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


    return $dataProvider;
    }
gridview yii2 kartik-v
1个回答
0
投票

DateRangePicker本身为您提供了具有范围的字符串,因此带有'date' => $this->date的简单where子句不起作用。 Kartik V.在软件包kartik\daterange\DateRangeBehavior中提供了行为kartik-v/yii2-date-range

我正在以下列方式成功使用它。请注意,我的时间戳属性是timestamp_visit

在视图中:

echo GridView::widget([
...
    'columns' => [
        [
            'attribute' => 'timestamp_visit',
            'format' => [
                'datetime',
                'medium'
            ],
            'width' => '60px',
            'filterType' => GridView::FILTER_DATE_RANGE,
            'filterWidgetOptions' => [
                'model' => $searchModel,
                'convertFormat' => true,
                'pluginOptions' => [
                    'timePicker' => true,
                    'timePickerIncrement' => 1,
                    'timePicker24Hour' => true,
                    'locale' => [
                        'format' => $searchModel::TIME_FORMAT,
                    ]
                ]
            ]
        ],
    ...
    ]

在搜索模型中:

<?php
...
use kartik\daterange\DateRangeBehavior;

class ...Search extends ....
{
    const TIME_FORMAT = 'Y-m-d H:i:s';

    /**
     * @var string Start date filled up by DateRangeBehavior
     */     
    public $timestampVisitStart;

    /**         
     * @var string End date filled up by DateRangeBehavior
     */         
    public $timestampVisitEnd;
...

       public function rules()
    {
        return [
            [['timestamp_visit'], 'match', 'pattern' => '/^.+\s\-\s.+$/'],
            ...
        ];
    } 
    public function behaviors()
    {
        return [
            [
                'class' => DateRangeBehavior::className(),
                'attribute' => 'timestamp_visit',
                'dateStartAttribute' => 'timestampVisitStart',
                'dateEndAttribute' => 'timestampVisitEnd',
                'dateStartFormat' => self::TIME_FORMAT,
                'dateEndFormat' => self::TIME_FORMAT,
            ]
        ];
    }
...
    public function search($params)
    {
        $query = self::find();

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

        $dataProvider->sort = new Sort([
            'defaultOrder' => [
                'timestamp_visit' => SORT_DESC,
                'id' => SORT_DESC,
            ]
        ]);
...
        if ($this->timestampVisitStart) {
            $query->andWhere(['>=', "timestamp_visit", $this->timeToUTC($this->timestampVisitStart)]);
        }
        if ($this->timestampVisitStart) {
            $query->andWhere(['<=', "timestamp_visit", $this->timeToUTC($this->timestampVisitEnd)]);
        }

        return $dataProvider;
   }

   /**
     * Convert string time in format $format to UTC time format for SQL where clause
     * @param string $time Time in format $format
     * @param string $format Format of $time for the function date, default 'Y-m-d H:i:s'
     */
    private function timeToUTC($time, $format='Y-m-d H:i:s')
    {
        $timezoneOffset = \Yii::$app->formatter->asDatetime('now', 'php:O');
        return date($format, strtotime($time.$timezoneOffset));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.