This image what I already have. I have to make second column as multiselect. How to achieve this? This should be configuration field.
-
Can you add your code to your question what you have done?– Sukumar GoraiDec 26, 2019 at 14:31
-
devdocs.magento.com/guides/v2.3/ext-best-practices/tutorials/…– Duddy WoodyDec 26, 2019 at 14:55
-
This is for select and what you have done? I want to see your code what you have done yet.– Sukumar GoraiDec 26, 2019 at 14:56
-
My code the same at this time, except 2 rows... I have to do 2 columns - 1 select and 1 multiselect... I dont know how to achieve multiselect column, so i asked here.– Duddy WoodyDec 26, 2019 at 15:03
1 Answer
Create Field class like below copied from magento dev docs
<?php
namespace Vendor\Module\Block\Adminhtml\Form\Field;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Vendor\Module\Block\Adminhtml\Form\Field\TaxColumn;
/**
* Class Ranges
*/
class Ranges extends AbstractFieldArray
{
/**
* @var TaxColumn
*/
private $taxRenderer;
/**
* Prepare rendering the new field by adding all the needed columns
*/
protected function _prepareToRender()
{
$this->addColumn('from_qty', ['label' => __('From'), 'class' => 'required-entry']);
$this->addColumn('to_qty', ['label' => __('To'), 'class' => 'required-entry']);
$this->addColumn('price', ['label' => __('Price'), 'class' => 'required-entry']);
$this->addColumn('tax', [
'label' => __('Tax'),
'renderer' => $this->getTaxRenderer()
]);
/* Multiselect code */
$this->addColumn('country', [
'label' => __('Country'),
'renderer' => $this->getCountryRenderer(),
'extra_params' => 'multiple="multiple"'
]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
}
/**
* Prepare existing row data object
*
* @param DataObject $row
* @throws LocalizedException
*/
protected function _prepareArrayRow(DataObject $row): void
{
$options = [];
$tax = $row->getTax();
if ($tax !== null) {
$options['option_' . $this->getTaxRenderer()->calcOptionHash($tax)] = 'selected="selected"';
}
$countries = $row->getCountry();
if(count($countries) > 0){
foreach ($countries as $country) {
$options['option_' . $this->getCountryRenderer()->calcOptionHash($country)]
= 'selected="selected"';
}
}
$row->setData('option_extra_attrs', $options);
}
/**
* @return TaxColumn
* @throws LocalizedException
*/
private function getTaxRenderer()
{
if (!$this->taxRenderer) {
$this->taxRenderer = $this->getLayout()->createBlock(
TaxColumn::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}
return $this->taxRenderer;
}
/* Multiselect renderer for countries */
private function getCountryRenderer()
{
if (!$this->countryRenderer) {
$this->countryRenderer = $this->getLayout()->createBlock(
\Vendor\Module\Block\Adminhtml\Form\Field\CountryColumn::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}
return $this->countryRenderer;
}
}
Create CountryColumn class under
app/code/Vendor/Module/Block/Adminhtml/Form/Field/CountryColumn.php
with below content
<?php
declare(strict_types=1);
namespace Vendor\Module\Block\Adminhtml\Form\Field;
use Magento\Braintree\Helper\Country;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
class CountryColumn extends Select
{
private $countryHelper;
public function __construct(
Context $context,
Country $countryHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->countryHelper = $countryHelper;
}
public function setInputName($value)
{
return $this->setName($value . '[]');
}
public function _toHtml(): string
{
if (!$this->getOptions()) {
$this->setOptions($this->countryHelper->getCountries());
}
$this->setExtraParams('multiple="multiple"');
return parent::_toHtml();
}
}
-
2
-
2
-
I have implemented this, this is saving data to databse but not able to see it selected on load. Can you suggest me? [option_extra_attrs] => Array ( [option_4200685455] => selected="selected" ) I get this extra in $row array. but not showing as selected.– DollyApr 4 at 19:37