A
A
AlexAll2019-03-19 14:33:48
Yii
AlexAll, 2019-03-19 14:33:48

How to save data from input fields in GridView yii2?

In the GridView, I made input fields that display information (number) for positioning blocks on the site, so that if you wish, you can select positions for all blocks at once
, like this in the code

<?php
    $form = ActiveForm::begin(['action' => ['main-page/sort/'],]);
    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'summary' => false,
        'columns' => [
            [
                'attribute' => 'position',
                'filter' => false,
                'format' => 'raw',
                'headerOptions' => ['width' => '150'],
                'value' => function($model) use ($form) {
                    return $form->field($model, 'position')->textInput(['style' => 'width:50px'])->label(false);
                }
            ],
            'name',
         ?>           
  <div class="form-group">
        <?= Html::submitButton('Сохранить позиции', ['class' => 'btn btn-default']) ?>
     
    </div>
<?php
    ActiveForm::end();
    ?>

Two problems appear.
1) When you click on the save button, an array is sent with a post request that sends only 1 One last field value
Tried like this
return $form->field($model, 'position[]')->textInput(['style' => 'width:50px'])->label(false);

It sends all the values, but the display of the values ​​themselves in these fields will disappear
.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VelAnna, 2019-03-19
@AlexAll

If I understand you correctly, then:

return $form->field($model, 'position['. $model->id. ']')->textInput(['value' => $model->position, 'style' => 'width:50px'])->label(false);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question