【问题标题】:Yii2 datepicker in kartik gridview not workingkartik gridview中的Yii2 datepicker不工作
【发布时间】:2015-02-04 09:04:58
【问题描述】:

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

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

Gridview 日期选择器:

        [
            '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' =>  [
        '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'
    ],

搜索模型:

<?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;
}
}

屏幕:

【问题讨论】:

    标签: php gridview yii datepicker yii2


    【解决方案1】:

    添加规则

    [['title','date'], 'safe'],
    

    【讨论】:

    • 好的,是的,这是正确的,我有这个,但我把它漏掉了,因为它不起作用
    • 日期选择器上的验证失败,但我不知道如何解决这个问题
    • 您应该将日期格式设置为普通视图。现在在规则中使用 Y-d-m,您的格式 'format' => 'dd-mm-yyyy',在 date-widget 中
    • [['date'], 'date'], 和 'format' => 'yyyy-mm-dd',在小部件中
    • [['date'], 'date','format' =&gt; 'php:d-m-Y'] and afterValidate 你应该转换$model-&gt;date 将数据库格式化为Y-m-d(用于mysql)以保存
    【解决方案2】:

    添加

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

    之后

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

    并将[['date'], 'safe'], 添加到您的规则中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多