在kartik gridview中的Yii2 datepicker不工作 [英] Yii2 datepicker in kartik gridview not working

查看:409
本文介绍了在kartik gridview中的Yii2 datepicker不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 kartik gridview ,我想添加一个 datepicker 到搜索字段
我也使用 datecontrol



这是我所得到的,但验证不起作用: p>

Gridview日期选择器:

  [
'attribute'= >'date',
'value'=>函数($ model,$ index,$ widget){
return Yii :: $ app-> formatter-> asDate($ model-> date);
},
'filterType'=> GridView :: FILTER_DATE,
'filterWidgetOptions'=> [
'pluginOptions'=> [
'format'=> 'dd-mm-yyyy',
'autoclose'=> true,
'todayHighlight'=> true,
]
],
'width'=> '160px',
'hAlign'=> 'center',
],

日期控制设置:

 'datecontrol'=> [
'class'=> 'kartik\datecontrol\Module',

//显示每个日期属性(ICU格式示例)的格式设置
'displaySettings'=> [
Module :: FORMAT_DATE => 'php:d-m-Y',
Module :: FORMAT_TIME => 'php:H:m:s a',
Module :: FORMAT_DATETIME => 'dd-MM-yyyy HH:mm:ss a',
],

//保存每个日期属性的格式设置(PHP格式示例)
'saveSettings'= > [
Module :: FORMAT_DATE => 'php:U'//保存为unix时间戳
Module :: FORMAT_TIME => 'php:H:i:s',
Module :: FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],

//设置你的显示时区
'displayTimezone'=> '欧洲/布鲁塞尔',

//将您的时区设置为保存到db
'saveTimezone'=> '欧洲/布鲁塞尔',

//自动使用上述各种格式的kartik\widgets
'autoWidget'=> true,

//当autoWidget为true时使用的kartik\widgets中每个小部件的默认设置
'autoWidgetSettings'=> [
Module :: FORMAT_DATE => ['pluginOptions'=> [
'autoclose'=> true,
'todayHighlight'=> true,
//'todayBtn'=> true
]],
Module :: FORMAT_DATETIME => [],//如果需要,设置
Module :: FORMAT_TIME => [],//如果需要,设置
],
//使用自定义转换动作
'convertAction'=> '/ cms / parse / convert-date-control'
],

Searchmodel: / p>

 <?php 

名称空间infoweb\\\
ews\models;

使用Yii;
使用yii\base\Model;
使用yii\data\ActiveDataProvider;

/ **
* NewsSearch代表关于infoweb\empoyees\models\News的搜索表单后面的模型。
* /
class NewsSearch扩展新闻
{
/ **
* @inheritdoc
* /
public function rules()
{
return [
[['active'],'integer'],
[['title','date'],'safe'],
] ;
}

/ **
* @inheritdoc
* /
公共功能场景()
{
//绕行在parent类中的scenario()实现
返回Model :: scenario();
}

/ **
*创建带有搜索查询的数据提供者实例
* @return ActiveDataProvider
* /
public function search ($ params)
{

//将日期转换为unix时间戳以用于搜索查询
if(isset($ params ['NewsSearch'] ['date '])){
$ origDate = $ params ['NewsSearch'] ['date'];
$ params ['NewsSearch'] ['date'] = strtotime($ params ['NewsSearch'] ['date']);
}

$ query = News :: find();

$ query-> andFilterWhere(['language'=> Yii :: $ app-> language]);

$ dataProvider = new ActiveDataProvider([
'query'=> $ query,
'sort'=> ['defaultOrder'=> ['date'= > SORT_ASC]],
'pagination'=> [
'pageSize'=> 50,
],
]);

//将实体模型作为关系加入
$ query-> joinWith(['translations']);

//启用相关列的排序
$ dataProvider-> sort-> attributes ['title'] = [
'asc'=> ['title'=> SORT_ASC],
'desc'=> ['title'=> SORT_DESC],
];

if(!($ this-> load($ params)&& $ this-> validate())){
return $ dataProvider;
}

$ query-> andFilterWhere([
'id'=> $ this-> id,
'date'=> $ this - > date,
'active'=> $ this-> active,
'created_at'=> $ this-> created_at,
'updated_at'=> $ this-> updated_at,
]);

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

//格式化显示日期
if(isset($ params ['NewsSearch'] ['date'])){
$ this-> date = $起源
}

return $ dataProvider;
}
}

屏幕:



解决方案

添加

  //将日期转换为Unix时间戳
if !空($ params [StringHelper :: basename(self :: className())] ['date'])){
$ query-> andFilterWhere([
'date'=> strtotime ($ params [StringHelper :: basename(self :: className())] ['date'])
]);
}

after



<$ p如果(!($ this-> load($ params)&& $ this-> validate())){
return $ dataProvider;
}

并添加 [['date'],'安全'],您的规则


I'm using kartik gridview and I want to add a datepicker to the search fields I'm also using datecontrol

This is what I got, but the validation is not working:

Gridview date picker:

        [
            'attribute'=>'date',
            'value' => function ($model, $index, $widget) {
                return Yii::$app->formatter->asDate($model->date);
            },
            'filterType' => GridView::FILTER_DATE,
            'filterWidgetOptions' => [
                'pluginOptions' => [
                    'format' => 'dd-mm-yyyy',
                    'autoclose' => true,
                    'todayHighlight' => true,
                ]
            ],
            'width' => '160px',
            'hAlign' => 'center',
        ],

Datecontrol settings:

    'datecontrol' =>  [
        'class' => 'kartik\datecontrol\Module',

        // format settings for displaying each date attribute (ICU format example)
        'displaySettings' => [
            Module::FORMAT_DATE => 'php:d-m-Y',
            Module::FORMAT_TIME => 'php:H:m:s a',
            Module::FORMAT_DATETIME => 'dd-MM-yyyy HH:mm:ss a',
        ],

        // format settings for saving each date attribute (PHP format example)
        'saveSettings' => [
            Module::FORMAT_DATE => 'php:U', // saves as unix timestamp
            Module::FORMAT_TIME => 'php:H:i:s',
            Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
        ],

        // set your display timezone
        'displayTimezone' => 'Europe/Brussels',

        // set your timezone for date saved to db
        'saveTimezone' => 'Europe/Brussels',

        // automatically use kartik\widgets for each of the above formats
        'autoWidget' => true,

        // default settings for each widget from kartik\widgets used when autoWidget is true
        'autoWidgetSettings' => [
            Module::FORMAT_DATE => ['pluginOptions' => [
                'autoclose' => true,
                'todayHighlight' => true,
                //'todayBtn' => true
            ]],
            Module::FORMAT_DATETIME => [], // setup if needed
            Module::FORMAT_TIME => [], // setup if needed
        ],
        // Use custom convert action
        'convertAction' => '/cms/parse/convert-date-control'
    ],

Searchmodel:

<?php

namespace infoweb\news\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * NewsSearch represents the model behind the search form about `infoweb\empoyees\models\News`.
 */
class NewsSearch extends News
{
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['active'], 'integer'],
        [['title', 'date'], 'safe'],
    ];
}

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

/**
 * Creates data provider instance with search query applied
 * @return ActiveDataProvider
 */
public function search($params)
{

    // Transform the date to a unix timestamp for usage in the search query
    if (isset($params['NewsSearch']['date'])) {
        $origDate = $params['NewsSearch']['date'];
        $params['NewsSearch']['date'] = strtotime($params['NewsSearch']['date']);
    }

    $query = News::find();

    $query->andFilterWhere(['language' => Yii::$app->language]);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => ['defaultOrder' => ['date' => SORT_ASC]],
        'pagination' => [
            'pageSize' => 50,
        ],
    ]);

    // Join the entity model as a relation
    $query->joinWith(['translations']);

    // enable sorting for the related column
    $dataProvider->sort->attributes['title'] = [
        'asc' => ['title' => SORT_ASC],
        'desc' => ['title' => SORT_DESC],
    ];

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'date' => $this->date,
        'active' => $this->active,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ]);

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

    // Format the date for display
    if (isset($params['NewsSearch']['date'])) {
        $this->date = $origDate;
    }

    return $dataProvider;
}
}

Screen:

解决方案

add

// Convert date to Unix timestamp
if (!empty($params[StringHelper::basename(self::className())]['date'])) {
    $query->andFilterWhere([
        'date' => strtotime($params[StringHelper::basename(self::className())]['date'])
    ]);
}

after

if (!($this->load($params) && $this->validate())) {
    return $dataProvider;
}

and add [['date'], 'safe'], to your rules

这篇关于在kartik gridview中的Yii2 datepicker不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆