0

I am able to add multi select options in admin grid using below approach.

My class extends Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray

protected function _prepareToRender()
    {
        $this->addColumn('country', [
            'label' => __('Country'),
            'renderer' => $this->getMultiselectRenderer(),
            'extra_params' => 'multiple="multiple"'
        ]);
$this->_addAfter = false;
        $this->_addButtonLabel = __('Add');
}
private function getMultiselectRenderer()
{
    if (!$this->countryRenderer) {
        $this->countryRenderer = $this->getLayout()->createBlock(
            MultiselectRenderer::class,
            '',
            ['data' => ['is_render_to_js_template' => true]]
        );
    }
    return $this->countryRenderer;
}

Renderer class

<?php

namespace Vendor\Module\Block\Adminhtml\Blocks\Edit\Tab;

use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;

class MultiselectRenderer extends Select
{
    private $countryHelper;

    public function __construct(
        Context $context,
        CollectionFactory $collectionFactory,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->collectionFactory = $collectionFactory;
    }

    public function setInputName($value)
    {
        return $this->setName($value . '[]');
    }

    public function _toHtml(): string
    {
        $collection = $this->collectionFactory->create();

        $result = [];
        $result[] = ['label' => 'select', 'value' => '', 'selected' => 'selected'];
        foreach ($collection as $data) {
            $result[] = ['label' => $data['iso3_code'], 'value' => $data['country_id']];
        }

        if (!$this->getOptions()) {
            $this->setOptions($result);
        }
        $this->setExtraParams('multiple="multiple"');
        return parent::_toHtml();
    }
}

It is working fine as multi select. Now instead of using multi select I have to convert it to checkboxes. How to do that? How to add a checkbox renderer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.