I created dropdown in a table similar to Store > Config > Catalog > Invenotry
, everything is working perfectly except selected
is not working in my dropdown, however values are stored in a database.
I search in core files and follows:
Magento\CatalogInventory\Block\Adminhtml\Form\Field\Minsaleqty
Here is my code:
File: system.xml
<field id="template" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Select Template</label>
<frontend_model>Vendor\Module\Block\Adminhtml\Form\Field\Template</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
File: Template.php
protected function _prepareToRender()
{
$this->addColumn(
'dayoption',
[
'label' => __('Delivery Days'),
'readonly'=>'readonly',
'renderer' => $this->_getRenderer()
]
);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add More');
}
protected function _getRenderer()
{
$this->_itemRenderer = $this->getLayout()->createBlock(
\Vendor\Module\Block\Adminhtml\Form\Field\Options::class, '',
['data' => ['is_render_to_js_template' => true]]
);
$this->_itemRenderer->setClass('option_group_select');
return $this->_itemRenderer;
}
/**
* Prepare existing row data object
*
* @param \Magento\Framework\DataObject $row
* @return void
*/
protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
{
$optionExtraAttr = [];
$optionExtraAttr['option_' . $this->_getRenderer()->calcOptionHash($row->getData('dayoption'))] =
'selected="selected"';
$row->setData(
'option_extra_attrs',
$optionExtraAttr
);
}
File: Option.php
use Magento\Framework\View\Element\Html\Select;
class Options extends Select
{
/**
* @var \Magento\Eav\Model\Config
*/
protected $_eavConfig;
/**
*
* @var array
*/
private $_attributeGroups;
public function __construct(Context $context,
\Magento\Eav\Model\Config $eavConfig,
array $data = [])
{
parent::__construct($context, $data);
$this->_eavConfig = $eavConfig;
}
public function setInputName($value)
{
return $this->setName($value);
}
public function _attributeOptions(){
if ($this->_attributeGroups === null) {
$this->_attributeGroups = [];
$attributeCode = "delivery_days";
$attribute = $this->_eavConfig->getAttribute('catalog_product', $attributeCode);
$options = $attribute->getSource()->getAllOptions();
$attrValues = [];
foreach ($options as $option) {
if ($option['value'] > 0) {
$this->_attributeGroups[$option['label']] = $option['label'];
}
}
}
return $this->_attributeGroups;
}
/**
* Render block HTML
*
* @return string
*/
public function _toHtml()
{
if (!$this->getOptions()) {
foreach ($this->_attributeOptions() as $item) {
$this->addOption($item, addslashes($item));
}
}
return parent::_toHtml();
}
}
_prepareArrayRow
is reason for selected in value in a dropdown, I print value of optionExtraAttr
which is
array ( 'option_614544684' => 'selected="selected"',)
currently after loading the page its shows first value of dropdown, not which is store in core_config_data
.
Thanks