1

I add an admin grid using the below mentioned url. Now I add a multi-select field in the form by editing at App/Code/Webkul/Grid/Block/Adminhtml/Grid/AddRow.php file.It is visible fine. I use implode() function to convert the array into string and the value get stored at database as (1,3,5 ).But When I reopen the form by clicking on 'edit' option, form is loaded but in multiselect field only one value is selected. I need a help in this regard. Thank you.

4

1 Answer 1

2

You need to edit the following file:

/app/code/Webkul/Grid/Block/Adminhtml/Grid/Edit/Form.php

Add the below code

$alloptions = array(
                    array('label' => 'ABC1', 'value' => 'ABC1'),
                    array('label' => 'ABC2', 'value' => 'ABC2'),
                    array('label' => 'ABC3', 'value' => 'ABC3'),
                    array('label' => 'ABC4', 'value' => 'ABC4'),
                    array('label' => 'ABC5', 'value' => 'ABC5'),
                    array('label' => 'ABC6', 'value' => 'ABC6'),
                    );

$fieldset->addField(
    'name',
    'multiselect',
    [
        'label' => __('Service Name'),
        'title' => __('Service Name'),
        'name' => 'name',
        'values' => $alloptions
    ]
);

You can change the options and field name according to your requirement.

Also you need to change the below file:

/app/code/Webkul/Grid/Controller/Adminhtml/Grid/Save.php

Add the below line:

$data['name'] = implode(',',$data['name']);

Just after the line:

$rowData = $this->gridFactory->create();

Like below:

$rowData = $this->gridFactory->create();
$data['name'] = implode(',',$data['name']);
$rowData->setData($data);
if (isset($data['id'])) {
    $rowData->setEntityId($data['id']);
}
$rowData->save();

Here I am using field name as "name". You need to create a field name called "name" in the table as well.

No need to change any other files.

1
  • Welcome. Happy Coding. Aug 27, 2018 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.