2amigos / yii2-date-picker-widget

Bootstrap DatePicker Widget for Yii2
https://2amigos.us
Other
130 stars 77 forks source link

Multiple same form with DateRangePicker #4

Closed o-shabashov closed 9 years ago

o-shabashov commented 10 years ago

Hello.

If i display same form multiple times at one page, DatePickerRange dosn`t appear more than 1 time (at first element).

Sample: $form = ActiveForm::begin(); echo $form->field($model, 'date_attended_from')->widget(DateRangePicker::className(), [ 'attributeTo' => 'date_attended_to', 'form' => $form, 'clientOptions' => [ 'autoclose' => true, 'format' => 'yyyy-mm-dd' ] ]); ActiveForm::end();

$form = ActiveForm::begin(); echo $form->field($model, 'date_attended_from')->widget(DateRangePicker::className(), [ 'attributeTo' => 'date_attended_to', 'form' => $form, 'clientOptions' => [ 'autoclose' => true, 'format' => 'yyyy-mm-dd' ] ]); ActiveForm::end();

I think problem in duplicate id attribute. What can i do to fix that?

tonydspaniard commented 9 years ago

You need to use a tabular rendering (it can be solved if the attribute is an array)

yuvraj7737 commented 9 years ago

How did you solved the issue?? Pls explain with the above example

tonydspaniard commented 9 years ago

@yuvraj7737 The problem it has its due that the same ID is given for any DateRangePicker element in the page. The only way to solve it if you do have an array property and collect them independently. Here is a good example on how to work with Tabular Inputs: http://www.yiiframework.com/doc/guide/1.1/en/form.table

yuvraj7737 commented 9 years ago

Hi, Thanks for your response. Let me explain you my use case.

  1. I have a simple form for collecting some basic information (5 fields).
  2. Once the user submits the information it is displayed in a gridview (i am using kartik-v gridview).
  3. In the gridview I am using ExpandRowColumn widget which renders the form with the fields in original form. User can edit the data here.

The problem:

  1. I have one datefield in the form for which I am using, DatePicker widget. I can only get the widget for first entry in the gridview.

How do I instantiate the model for every row in the gridview.

gustavodionisio commented 8 years ago

I have the same problem, any solution?

gustavodionisio commented 8 years ago

I got a result. On my index.php I have the GridView and its expand row, which renders partial another view as seen below:


<?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'export' => false, 'columns' => [ [ 'class' => 'kartik\grid\ExpandRowColumn', 'value' => function ($model, $key, $index, $column) { return GridView::ROW_COLLAPSED; }, 'detail' => function ($model, $key, $index, $column) { $myModel= Model::findOne(['AField' => $model->MyField]); return Yii::$app->controller->renderPartial('_my_view', [ 'model' => $myModel, 'contador' => GridView::$counter, //HERE IS THE MAGIC ]); }, ], ], ]); ?>


On _my_view I have the widget like that:


echo DateTimePicker::widget([ 'model' => $model, 'attribute' => 'Date', 'type' => DateTimePicker::TYPE_COMPONENT_APPEND, 'readonly' => true, 'options' => ['id' => 'calendar'.$contador], //HERE IS THE MAGIC ONE MORE TIME 'pluginOptions' => [ 'autoclose' => true, 'format' => 'dd/mm/yyyy HH:ii', 'hoursDisabled' => '0,1,2,3,4,5,6,7,22,23,24', ] ]); ?>


This is my way to solve the issue. The GridView::$counter has the index of each row, so the 'Id' of the DateTimePicker also changes in each row. Hope it's helpful ;)