23

Yii2 datepicker in kartik gridview not working

 3 years ago
source link: https://www.codesd.com/item/yii2-datepicker-in-kartik-gridview-not-working.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Yii2 datepicker in kartik gridview not working

advertisements

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:

Xlo86.jpg

Add in rules

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




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK