表单之日期组件——Yii2框架(Ⅳ)

0x00 介绍

预览效果

日期组件是date('Y-m-d')类型,

0x01 安装
composer require kartik-v/yii2-widget-datepicker "@dev"
0x02 使用

安装好了后我们开始使用日期组件

use kartik\date\DatePicker; 
<?php echo DatePicker::widget([ 
'name' => 'Article[created_at]', 
'options' => ['placeholder' => '...'], 
//value值更新的时候需要加上 
'value' => '2016-05-03', 
'pluginOptions' => [ 
    'autoclose' => true, 
    'format' => 'yyyy-mm-dd', 
    'todayHighlight' => true, 
] 
]); ?>

针对ActiveForm生成的日期组件的用法:

<?= $form->field($model, 'day')->widget(DatePicker::classname(), [
        'options' => ['placeholder' => ''],
        'pluginOptions' => [
            'autoclose' => true,
            'todayHighlight' => true,
            'format' => 'yyyy-mm-dd',
       ]
]); ?>

有些小伙伴说为啥自己的是英文的,需要在你的工程配置文件里面加上 'language'=>'zh-CN', 即可。

0x03 时间组件
composer require kartik-v/yii2-widget-datetimepicker "*"

用法1

use kartik\datetime\DateTimePicker; 
<?= $form->field($model, 'created_at')->widget(DateTimePicker::classname(), [ 
'options' => ['placeholder' => ''], 
'pluginOptions' => [ 
    'autoclose' => true, 
    'todayHighlight' => true, 
] 
]); ?>

用法2

use kartik\datetime\DateTimePicker; 
echo '<label>时间</label>'; 
echo DateTimePicker::widget([ 
'name' => 'Article[created_at]', 
'options' => ['placeholder' => ''], 
//注意,该方法更新的时候你需要指定value值 
'value' => '2016-05-03 22:10:10', 
'pluginOptions' => [
    'autoclose' => true, 
    'format' => 'yyyy-mm-dd HH:ii:ss', 
    'todayHighlight' => true 
] 
]);

图书管理系统日期组件参考自:
yii2基础之超好用的日期组件和时间组件

Updated At: Author:xmsec
Chief Water dispenser Manager of Lancet, delivering but striving.
Github
comments powered by Disqus