【问题标题】:Yii2 kartik datetime picker retain value after page submittingYii2 kartik 日期时间选择器在页面提交后保留值
【发布时间】:2020-04-23 13:00:21
【问题描述】:

我在我的yii2 项目中使用yii2-widget-datetimepicker。我想在提交页面后保留它的价值。

<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>

如何保留选定的日期时间值?任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试设置该小部件的value 属性?
  • @MichalHynčica,我试过了,但不确定我的做法是否正确

标签: php yii2 bootstrap-datetimepicker kartik-v


【解决方案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>

但这并不可靠。如果您要跨多个页面收集数据,您应该真正考虑存储/持久性,至少在所有表单完成后会话并保存到数据库,或者跳过会话并直接使用内存数据、数据库等。

【讨论】:

    【解决方案2】:

    您应该为您的 nodel-> 字段分配一个值,并在提交之后(或之前)分配/重新分配您需要的值

    <?=
        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,
            ]
        ]);
     ?>
    

    【讨论】:

    • 我在运行时从用户那里选择日期时间
    • 如果您想在提交后显示插入的值,那么在...之后保留它的值是什么意思? ..你想以第二种形式显示相同的日期??那么您应该将第一次输入的值分配给模型并渲染..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2016-05-11
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多