Magento2 – Add Column with Select Options in Generated Admin Field

adminhtmlcustom-fieldcustom-optionsmagento2

I want to know if someone has already added a select field in a generated admin fields

Vendor/Name/Block/Adminhtml/Menu/Field/AdditionalMenu.php

<?php

namespace Vendor\Menu\Block\Adminhtml\Menu\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class AdditionalMenu
 */
class AdditionalMenu extends AbstractFieldArray
{
    protected $_itemRenderer;
    /**
     * {@inheritdoc}
     */
    protected function _prepareToRender()
    {
        $this->addColumn(
            'Title',
            ['label' => __('Title'),
             'size' => '200px',
             'class' => 'required-entry']
        );
        //Option test
        /*$this->addColumn('Type',
        array('label'   => __('Type'),
              'type'    =>'options',
              'options' => array('Option 1', 'Option 2')
        ));*/
        $this->addColumn(
            'Link',
            ['label' => __('Link'),
             'size' => '200px']
        );
        $this->addColumn(
            'Ordre',
            ['label' => __('Ordre'),
             'size' => '50px',
             'class' => '']
        );
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Ajouter un Menu');
    }

}

enter image description here

Best Answer

I found the solution:

How to add a select options column :

<?php    
namespace Vendor\Name\Block\Adminhtml\Menu\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class AdditionalEmail
 */
class AdditionalMenu extends AbstractFieldArray
{
    protected $_typeblockOptions;
    protected $_cmsblockOptions;
    /**
     * {@inheritdoc}
     */
    protected function _prepareToRender()
    {
        $this->addColumn(
            'attributname',
            [
                'label' => __('Attributname'),
                'size' => '200px',
                'renderer' => $this->_getGroupRenderer()]
        );

        //OR
        $this->addColumn(
            'title',
            [
                'label' => __('Titre'),
                'size' => '200px',
                'class' => 'required-entry']
        );

        $this->_addAfter = false;
        $this->_addButtonLabel = __('Ajouter un Menu');

protected function _getGroupRenderer()
{
    if (!$this->_cmsblockOptions) {
        $this->_cmsblockOptions = $this->getLayout()->createBlock(
            \Vendor\Name\Block\Adminhtml\Menu\Config\Backend\Options::class,
            '',
            ['data' => ['is_render_to_js_template' => true]]
        );
        $this->_cmsblockOptions->setClass('attributname_group_select');
    }
    return $this->_cmsblockOptions;
}

Vendor\Name\Block\Adminhtml\Menu\Config\Backend\Options.php

<?php

namespace Vendor\Name\Block\Adminhtml\Menu\Config\Backend;

class Options extends \Magento\Framework\View\Element\Html\Select
{

    public function __construct(
        \Magento\Framework\View\Element\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }


    /**
     * @param string $value
     * @return $this
     */
    public function setInputName($value)
    {
        return $this->setName($value);
    }

    /**
     * Render block HTML
     *
     * @return string
     */
    public function _toHtml()
    {
        $html = '<select>';
        $html .= '<option value="First">First value</option>';
        $html .= '<option value="Second">Second value</option>';
        $html .= '</select>';
        return $html;
    }
}