我使用的是 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>
如何保留选定的日期-时间值?任何帮助将是非常感激的。
如果不做进一步的调整,你可以这样做
<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,直接使用内存数据,数据库等。
你应该给你的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,
]
]);
?>