9

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>
6
  • @Divyesh no it won't. Please refer the image to understand my requirement May 15, 2018 at 12:12
  • Can you share code May 15, 2018 at 12:40
  • I was looking for the same thing, but realised that such a table is not really necessary. Every 2d lookup table can be transformed in a 1d table. If you have a excel table, here is a tutorial how to transform it: spreadsheetpage.com/index.php/tip/… . Might not be the solution you are looking for, but would save you alot of work ;)
    – Mo3bius
    Sep 15, 2018 at 7:49
  • Here a more detailed comparison of both data representations: eagereyes.org/basics/spreadsheet-thinking-vs-database-thinking. Magento is based on database tables so it's probably best to also represent data in the backend in such manner.
    – Mo3bius
    Sep 15, 2018 at 8:32
  • 1
    @MartinE After getting no answer, I did it via jQuery. If you wish I can share the code with you. Mar 15, 2019 at 14:30

1 Answer 1

0

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');
    }
}
5
  • Thank you for your anwser, however, this is not the solution. This is just on how to create the default table. I need to know how to create the rows dynamically with data (with a data array for example) and how to remove the action buttons & the "add" button.
    – MartinE
    Mar 18, 2019 at 9:55
  • /root/vendor/dotmailer/dotmailer-magento2-extension/Block/Adminhtml/Config/Automation/Customdatafields.php Mar 18, 2019 at 10:06
  • Please check Customdatafields.php for more details Mar 18, 2019 at 10:06
  • -1 not a working solution. I am looking for a solution too. Like @MartinE said this is for a default table.
    – Akif
    Mar 18, 2019 at 11:10
  • ok i under stand Please refer this link: nwdthemes.com/2018/06/21/… Mar 18, 2019 at 11:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.