Yii2 - Dual Listbox

In the project that I want to develop, user can upload an Excel file (xls,xlsx) to system.

The excel sheet has headers in the first row, and value in another row. System has a default excel template that

consist the rule for headers sequence such as (Name, Age, Sex), but sometimes user use their own excel template so sometimes the header sequence become like this (Sex, Name, Age).

To handle this sequence, I’ve a plan to make a mapping process to handle the headers sequence before save the value to database. I wanna use dual list box.

I’ve 2 a table in database for this process:

  1. Header -> has to column (header_id, header_name), all the headers from file has been save in here.

Each headers saved with their own header_id and header_name

  1. Info -> the value from the excel file save here.

and I also has a pure (not generated by Gii) CostumizedHeaderController.php, CostumizeHeader.php, Index.php

This is code in CostumizeHeaderController:


class CostumizeHeaderController extends Controller {


    //put your code here


    public function actionShowHeaders() {

    $model = new CostumizeHeader();

    $model->loadHeaders();

        $items = \backend\models\CostumizeHeader::getAllHeader();


        return $this->render('index', [

                    'model' => $model,

                    'items' => $items

        ]);

    }


}

Code in model (CostumizeHeader.php)


class CostumizeHeader {

    //put your code here


    /**

     * @var array IDs of the favorite foods

     */

    public $list_headers = [];


    public function rules() {

        return [

                [['list_headers'], 'string', 'max' => 255],

        ];

    }


    /**

     * @return array customized attribute labels

     */

    public function attributeLabels() {

        return [

            'list_headers' => 'List Costumized Header',

        ];

    }


    public function loadHeaders() {

        $this->list_headers = [];

        $headers = Header::find()->all();

        foreach ($headers as $ff) {

            $this->list_headers[] = $ff->header_id;

        }

    }


    public static function getAllHeader() {

        $headers = Header::find()->asArray()->all();

        $items = ArrayHelper::map($headers, 'header_id', 'nama_header');

        return $items;

    }



code in index.php


 <?php

    $form = ActiveForm::begin([

                'id' => 'favorite-form',

                'enableAjaxValidation' => false,

    ]);

    ?>


    <?= $form->field($model->list_headers, 'list_headers')->widget(DualListbox::className(), [

                'model' => $model,

                'items' => $items,

                'name'=>'nama_header',

                'attribute' => 'list_headers', 

                'options' => [

                    'multiple' => true,

                    'size' => 15,

                ],

                'clientOptions' => [

                    'nonSelectedListLabel' => 'Available Header',

                    'selectedListLabel' => 'Final Header',

                    'moveOnSelect' => false,

                ],

            ])

            ->hint('Select the header according the sequence.');

    ?>

I’ve try to var_dump in controller and got this Array ( [1] => age [2] => sex [3] => name .

And I’ve been check the header table, and all the headers from excel file have been imported to database.

But I still got an error, like this Call to a member function formName() on a non-object.

I wish anybody can help me to solve this problem. Thankyou

I don’t understand what you are trying to do with your CustomizedHeader using DualListBox.

A DualListBox is just an user-friendly version of a CheckBoxList or a ListBox with multiple selection.

You can use it to define a has_many related items to a certain entity.

For example, imagine there are many posts on one hand and many categories on the other hand. You can define Post model and Category model respectively, with a relation of "A Post may belong to many Categories" and that of "A Category has many Posts".

Under such a circumstance you can use a CheckBoxList (or a ListBox with multiple selection) to define the categories of a certain post. It will show you all the possible categories and you can select multiple categories that the post belongs to.

A DualListBox doesn’t add or change anything to the logical meaning stated above, except that it will give the end user a better experience, since it displays the selected categories on one list and the unselected one on the other. There are 2 lists, but they work as one CheckBoxList as a whole.

[EDIT]

And I don’t think that you need to handle the irregular order of the imported excel like that. I would just read the 1st row (header) to detect the order and proceed to read the body according to it.