3

I'm trying to add dynamic rows in the product edit page in adminhtml. I'm using Magento 2.3.4. I have successfully added a dynamic row but the problem is the 'Add' button is not working after one row is inserted. Also, it is not giving any error in the console. But I notice that when I click 'Delete' button on the first row, it is also giving error 'Cannot set property 'delete' of undefined' at line no 858 in dynamic-rows.js

Below is my code.

In di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool" type="Magento\Ui\DataProvider\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="product_feature_new" xsi:type="array">
                <item name="class" xsi:type="string">
                    Vendor\Module\Ui\DataProvider\Product\Form\Modifier\Feature
                </item>
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
    </arguments>
</virtualType>
</config>

DataProvider class Feature.php

namespace Vendor\Module\Ui\DataProvider\Product\Form\Modifier;


use Magento\Framework\Stdlib\ArrayManager;
use Magento\Ui\Component\Container;
use Magento\Ui\Component\Form\Element\DataType\Number;
use Magento\Ui\Component\Form\Element\DataType\Text;
use Magento\Ui\Component\Form\Element\Input;
use Magento\Ui\Component\Form\Field;
use Magento\Ui\DataProvider\Modifier\ModifierInterface;

class Feature implements ModifierInterface
{
    /**
     * @var ArrayManager
     */
    protected $arrayManager;

    /**
     * Feature constructor.
     * @param ArrayManager $arrayManager
     */
    public function __construct(
        ArrayManager $arrayManager
    )
    {
        $this->arrayManager = $arrayManager;
    }

    public function modifyData(array $data)
    {
        return $data;
    }

    public function modifyMeta(array $meta)
    {
        $meta =  array_merge_recursive(
            $meta,
            [
                'feature_product' => [
                    'arguments' => [
                      'data' => [
                          'config' => [
                              'componentType' => 'dynamicRows',
                              'label' => __('Custom FieldSet'),
                              'renderDefaultRecord' => false,
                              'recordTemplate' => 'record',
                              'dataScope' => '',
                              'dndConfig' => [
                                  'enabled' => false,
                              ],
                              'required' => false,
                              'sortOrder' => 1000,
                              'addButton' => true,
                              'addButtonLabel' => 'Add New Record',
                              'deleteValue' => true,
                          ],
                      ],
                    ],
                    'children' => [
                        'record' => [
                            'arguments' => [
                                'data' => [
                                    'config' => [
                                        'componentType' => Container::NAME,
                                        'isTemplate' => true,
                                        'is_collection' => true,
                                        'component' => 'Magento_Ui/js/dynamic-rows/record',
                                        'dataScope' => '',
                                    ],
                                ],
                            ],
                            'children' => [
                                'feature' => [
                                    'arguments' => [
                                        'data' => [
                                            'config' => [
                                                'formElement' => Input::NAME,
                                                'componentType' => Field::NAME,
                                                'dataType' => Number::NAME,
                                                'label' => __('Feature'),
                                                'dataScope' => 'feature',
                                                'sortOrder' => 10,
                                                'validation' => [
                                                    'required-entry' => true,
                                                    'validate-greater-than-zero' => true,
                                                    'validate-digits' => false,
                                                    'validate-number' => true,
                                                ],
                                            ],
                                        ],
                                    ],
                                ],
                                'actionDelete' => [
                                    'arguments' => [
                                        'data' => [
                                            'config' => [
                                                'componentType' => 'actionDelete',
                                                'dataType' => Text::NAME,
                                                'label' => 'Actions',
                                                'sortOrder' => 20,
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ]
        );
        return $meta;
    }

}

Can anybody tell me if I miss anything? or any correction needed to make it work?

1
  • have you got any solutions for this issue because as I am also facing the same issue in product form. Commented May 7, 2021 at 9:23

2 Answers 2

2

You have missed datascope for your custom fieldset. The dynamic rows working with link property, which is need a proper datascope to add records.

Try the below code:

public function modifyMeta(array $meta)
    {
        $meta =  array_merge_recursive(
            $meta,
            [
                'feature_product' => [
                    'arguments' => [
                      'data' => [
                          'config' => [
                              'componentType' => 'dynamicRows',
                              'label' => __('Custom FieldSet'),
                              'renderDefaultRecord' => false,
                              'recordTemplate' => 'record',
                              'dataScope' => 'data.product.feature_product', //You have missed this datascope
                              'dndConfig' => [
                                  'enabled' => false,
                              ],
                              'required' => false,
                              'sortOrder' => 1000,
                              'addButton' => true,
                              'addButtonLabel' => 'Add New Record',
                              'deleteValue' => true,
                          ],
                      ],
                    ],
                    'children' => [
                        'record' => [
                            'arguments' => [
                                'data' => [
                                    'config' => [
                                        'componentType' => Container::NAME,
                                        'isTemplate' => true,
                                        'is_collection' => true,
                                        'component' => 'Magento_Ui/js/dynamic-rows/record',
                                        'dataScope' => '',
                                    ],
                                ],
                            ],
                            'children' => [
                                'feature' => [
                                    'arguments' => [
                                        'data' => [
                                            'config' => [
                                                'formElement' => Input::NAME,
                                                'componentType' => Field::NAME,
                                                'dataType' => Number::NAME,
                                                'label' => __('Feature'),
                                                'dataScope' => 'feature',
                                                'sortOrder' => 10,
                                                'validation' => [
                                                    'required-entry' => true,
                                                    'validate-greater-than-zero' => true,
                                                    'validate-digits' => false,
                                                    'validate-number' => true,
                                                ],
                                            ],
                                        ],
                                    ],
                                ],
                                'actionDelete' => [
                                    'arguments' => [
                                        'data' => [
                                            'config' => [
                                                'componentType' => 'actionDelete',
                                                'dataType' => Text::NAME,
                                                'label' => 'Actions',
                                                'sortOrder' => 20,
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ]
        );
        return $meta;
    }

Please refer more information from the answers:

Magento 2 - Unable to add record in dynamic rows in admin product edit page

Magento 2: Creating dynamic matrix in new/edit product page

Hope it will help others!

0

Can you please used this i thought 'formElement' is missing

            public function modifyMeta(array $meta)
            {
                $meta =  array_merge_recursive(
                    $meta,
                    [
                        'feature_product' => [
                            'arguments' => [
                              'data' => [
                                  'config' => [
                                      'componentType' => 'dynamicRows',
                                      'formElement' => 'component',
                                      'label' => __('Custom FieldSet'),
                                      'renderDefaultRecord' => false,
                                      'recordTemplate' => 'record',
                                      'dataScope' => '',
                                      'dndConfig' => [
                                          'enabled' => false,
                                      ],
                                      'required' => false,
                                      'sortOrder' => 1000,
                                      'addButton' => true,
                                      'addButtonLabel' => 'Add New Record',
                                      'deleteValue' => true,
                                  ],
                              ],
                            ],
                            'children' => [
                                'record' => [
                                    'arguments' => [
                                        'data' => [
                                            'config' => [
                                                'componentType' => Container::NAME,
                                                'isTemplate' => true,
                                                'is_collection' => true,
                                                'component' => 'Magento_Ui/js/dynamic-rows/record',
                                                'dataScope' => '',
                                            ],
                                        ],
                                    ],
                                    'children' => [
                                        'feature' => [
                                            'arguments' => [
                                                'data' => [
                                                    'config' => [
                                                        'formElement' => Input::NAME,
                                                        'componentType' => Field::NAME,
                                                        'dataType' => Number::NAME,
                                                        'label' => __('Feature'),
                                                        'dataScope' => 'feature',
                                                        'sortOrder' => 10,
                                                        'validation' => [
                                                            'required-entry' => true,
                                                            'validate-greater-than-zero' => true,
                                                            'validate-digits' => false,
                                                            'validate-number' => true,
                                                        ],
                                                    ],
                                                ],
                                            ],
                                        ],
                                        'actionDelete' => [
                                            'arguments' => [
                                                'data' => [
                                                    'config' => [
                                                        'componentType' => 'actionDelete',
                                                        'dataType' => Text::NAME,
                                                        'label' => 'Actions',
                                                        'sortOrder' => 20,
                                                    ],
                                                ],
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ]
                );
                return $meta;
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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