Magento – Magento 2 create admin config table with custom rows and columns

magento-2.1magento2

I want to create admin config with a table to take the data and save in config for that I followed this link.

But I also want to create custom rows in that table programmatically and also without the last action column and add button. Please refer the image
Admin Config

I was unable to find any solution on the web regarding the same nor the file

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

that we extend gave any clues.

Could have easily done this via jquery but want to implement the standard solution.

Code:

<?php
namespace Abc\Paymentmethod\Block\Adminhtml\System\Config\Form\Field;

class Feetable extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
{
    /**
     * @var \Magento\Framework\Data\Form\Element\Factory
     */
    protected $_elementFactory;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Data\Form\Element\Factory $elementFactory,
        array $data = []
    )
    {
        $this->_elementFactory  = $elementFactory;
        parent::__construct($context,$data);
    }
    protected function _construct(){
        $this->addColumn('noi', ['label' => __('NOI'),'readonly'=>'readonly']);
        $this->addColumn('fixed', ['label' => __('Fixed')]);
        $this->addColumn('percent', ['label' => __('Percent')]);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add More');
        parent::_construct();
    }

    protected function _prepareArrayRow(\Magento\Framework\DataObject $row) {
        $options = [1,2,3];
        $row->setData('option_extra_attrs', $options);
    }

}

system.xml

<field id="abc_fee_table" translate="label comment tooltip" sortOrder="17.4" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Abc Fee by Number of Installments(NOI)</label>
                    <frontend_model>Abc\Paymentmethod\Block\Adminhtml\System\Config\Form\Field\Feetable</frontend_model>
                    <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>                    
                </field>

Best Answer

You need to add this code in system.xml like this:

<field id="abc_fee_table" ranslate="label comment tooltip" sortOrder="17.4" showInDefault="1" showInWebsite="1" showInStore="0">
            <label>Additional Emails</label>
            <frontend_model>Abc\Paymentmethod\Block\Adminhtml\Form\Field\AdditionalItem</frontend_model>
    <backend_model>Abc\Paymentmethod\Config\Backend\AdditionalItem</backend_model>
</field>

Please add custom serializer

 <?php
namespace Abc\Paymentmethod\Config\Backend;

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value as ConfigValue;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\SerializerInterface;

class AdditionalItem extends ConfigValue
{
    /**
     * Json Serializer
     *
     * @var SerializerInterface
     */
    protected $serializer;

    /**
     * ShippingMethods constructor
     *
     * @param SerializerInterface $serializer
     * @param Context $context
     * @param Registry $registry
     * @param ScopeConfigInterface $config
     * @param TypeListInterface $cacheTypeList
     * @param AbstractResource|null $resource
     * @param AbstractDb|null $resourceCollection
     * @param array $data
     */
    public function __construct(
        SerializerInterface $serializer,
        Context $context,
        Registry $registry,
        ScopeConfigInterface $config,
        TypeListInterface $cacheTypeList,
        AbstractResource $resource = null,
        AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        $this->serializer = $serializer;
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
    }

    public function beforeSave()
    {
        $value = $this->getValue();
        unset($value['__empty']);
        $encodedValue = $this->serializer->serialize($value);

        $this->setValue($encodedValue);
    }

    protected function _afterLoad()
    {
        $value = $this->getValue();
        $decodedValue = $this->serializer->unserialize($value);

        $this->setValue($decodedValue);
    }
}

Add admin custom filed

<?php
namespace Abc\Paymentmethod\Block\Adminhtml\Form\Field;

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

class AdditionalItem extends AbstractFieldArray
{
    protected function _prepareToRender()
    {
         $this->addColumn('noi', ['label' => __('NOI'),'readonly'=>'readonly']);
        $this->addColumn('fixed', ['label' => __('Fixed')]);
        $this->addColumn('percent', ['label' => __('Percent')]);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add More');
    }
}
Related Topic