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.
-
Can you post your codes with screenshot?– Sukumar GoraiAug 27, 2018 at 9:43
-
I use this link– Magento LearnerAug 27, 2018 at 9:44
-
github.com/webkulabhi/…– Magento LearnerAug 27, 2018 at 9:45
-
Check my answer. Its working fine.– Sukumar GoraiAug 27, 2018 at 10:59
1 Answer
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.