Yii2 kartik日期时间选取器在提交页面后仍保留值。

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

我使用的是 yii2-widget-detimepicker(小工具日期选择器) 在我 yii2 项目。我想在提交页面后保留它的值。

<form action="index" method="post" >


<?=
                DateTimePicker::widget([
                    'name' => 'datetime_10',
                    'options' => [
                            'placeholder' => 'Start Date Time',
                            'autocomplete' => 'off',
                        'required' =>true,
                            ],
                    'convertFormat' => false,
                    'pluginOptions' => [
                        'format' => 'yyyy-mm-dd hh:i:ss',
                        //'startDate' => '01-Mar-2014 12:00 AM',
                        'todayHighlight' => true,
                        'autoclose' => true,
                    ]
                ]);
                ?>
                <?=
                DateTimePicker::widget([
                    'name' => 'datetime_11',
                    'options' => [
                            'placeholder' => 'End Date Time',
                            'autocomplete' => 'off',
                            'required' =>true,
                            ],
                    'convertFormat' => false,
                    'pluginOptions' => [
                        'format' => 'yyyy-mm-dd hh:i:ss',
                        //'startDate' => '01-Mar-2014 12:00 AM',
                        'todayHighlight' => true,
                        'autoclose' => true,
                    ]
                ]);
                ?>
</form>

如何保留选定的日期-时间值?任何帮助将是非常感激的。

php yii2 bootstrap-datetimepicker kartik-v
1个回答
1
投票

如果不做进一步的调整,你可以这样做

<form action="index" method="post">
    <?=
    DateTimePicker::widget([
        'name'          => 'datetime_10',
        'value'         => Yii::$app->request->post('datetime_10', null),
        'options'       => [
            'placeholder'  => 'Start Date Time',
            'autocomplete' => 'off',
            'required'     => true,
        ],
        'convertFormat' => false,
        'pluginOptions' => [
            'format'         => 'yyyy-mm-dd hh:i:ss',
            //'startDate' => '01-Mar-2014 12:00 AM',
            'todayHighlight' => true,
            'autoclose'      => true,
        ]
    ]);
    ?>
    <?=
    DateTimePicker::widget([
        'name'          => 'datetime_11',
        'value'         => Yii::$app->request->post('datetime_11', null),
        'options'       => [
            'placeholder'  => 'End Date Time',
            'autocomplete' => 'off',
            'required'     => true,
        ],
        'convertFormat' => false,
        'pluginOptions' => [
            'format'         => 'yyyy-mm-dd hh:i:ss',
            //'startDate' => '01-Mar-2014 12:00 AM',
            'todayHighlight' => true,
            'autoclose'      => true,
        ]
    ]);
    ?>
</form>

但这并不可靠。如果你是在多个页面中收集数据,你真的应该考虑storagepersistence,至少是session &在所有表单完成后保存到数据库,或者跳过session,直接使用内存数据,数据库等。


0
投票

你应该给你的nodel->字段分配一个值,然后在(或在)subimit之后(或之前)分配你需要的值。

<?=
    DateTimePicker::widget([
        'name' => 'datetime_10',
        'value' => $yourModel->yourField, // <------ this 
        'options' => [
                'placeholder' => 'Start Date Time',
                'autocomplete' => 'off',
            'required' =>true,
                ],
        'convertFormat' => false,
        'pluginOptions' => [
            'format' => 'yyyy-mm-dd hh:i:ss',
            //'startDate' => '01-Mar-2014 12:00 AM',
            'todayHighlight' => true,
            'autoclose' => true,
        ]
    ]);
 ?>
© www.soinside.com 2019 - 2024. All rights reserved.