Search code examples
phpyii2

Yii2 Load schedule using unclead/yii2-multiple-input


I am using unclead / yii2-multiple-input widget.

I want to generate different number of rows with values from my database. How can i do this?

I can design my columns in view and edit data manualy after page generated. But miss how to program the number of rows and its values in the view.

My code in view:

 <?= $form->field($User, 'User')->widget(MultipleInput::className(), [
        'min' => 0,
        'max' => 4,
        'columns' => [
            [
                'name'  => 'name',
                'title' => 'Name',
                'type' => 'textInput',
                'options' => [
                    'onchange' => $onchange,
                ],
            ],
            [
                'name'  => 'birth',
                'type'  => \kartik\date\DatePicker::className(),
                'title' => 'Birth',
                'value' => function($data) {
                    return $data['day'];
                },

                'options' => [
                    'pluginOptions' => [
                        'format' => 'dd.mm.yyyy',
                        'todayHighlight' => true
                    ]
                ]
            ],

        ]
        ])->label(false);

How can I make (for example) 8 rows with different values, and also have the ability to edit/remove/update some of them?


Solution

  • You need to look into the documentation as it says that you need to assign a separate field into the model which will store all the schedule in form of JSON and then provide it back to the field when editing/updating the model.

    You have not added the appropriate model to verify how are you creating the field User in your given case above. so, i will try to create a simple example which will help you implement it in your scenario.

    For Example.

    You have to store a user in the database along with his favorite books.

    User
    id, name, email
    
    Books
    id, name
    
    • Create a field/column in you User table with the name schedule of type text, you can write a migration or add manually.
    • Add it to the rules in the User model as safe.

      like below

      public function rules() {
          return [
              ....//other rules
              [ [ 'schedule'] , 'safe' ]
          ];
      }
      
    • Add the widget to the newly created column in ActiveForm

      see below code

      echo $form->field($model,'schedule')->widget(MultipleInput::class,[
          'max' => 4,
          'columns' => [
              [
                  'name'  => 'book_id',
                  'type'  => 'dropDownList',
                  'title' => 'Book',
                  'items' => ArrayHelper::map( Books::find()->asArray()->all (),'id','name'),
              ],
          ]
      
      ]);
      
    • When saving the User model convert the array to JSON string.

      like below

      if( Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) ){
          $model->schedule = \yii\helpers\Json::encode($model->schedule);
          $model->save();
      }
      
    • Override the afterFind() of the User model to covert the json back to the array before loading the form.

      like below

      public function afterFind() {
          parent::afterFind();
          $this->schedule = \yii\helpers\Json::decode($this->schedule);
      }
      

    Now when saved the schedule field against the current user will have the JSON for the selected rows for the books, as many selected, for example, if I saved three books having ids(1,2,3) then it will have json like below

    {
      "0": {
        "book_id": "1"
      },
      "2": {
        "book_id": "2"
      },
      "3": {
        "book_id": "3"
      }
    }
    

    The above JSON will be converted to an array in the afterFind() so that the widget loads the saved schedule when you EDIT the record.

    Now go to your update page or edit the newly saved model you will see the books loaded automatically.