Adding Dynamic Rows for Custom Fields in Magento2 Admin Form
Introduction
In this guide, we will walk you through the process of adding a dynamic row link for a custom field in the Magento2 admin form. We’ll use the provided user form as an example and add a “Add Department” button to dynamically create new rows for the department field.
Current Setup
You’ve already set up your user_form.xml file with fields for name and department. The current configuration looks like this:
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<!--...-->
<field name="dept">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" translate="true" xsi:type="string">Department</item>
<!--...-->
</item>
</argument>
</field>
</form>
The Problem
Currently, the department field is a single input box. To add more departments for a user, we need to create a dynamic row system that allows multiple entries.
The Solution: Adding a Dynamic Row Link
Step 1: Create an Action Button
We’ll start by adding an action button that will trigger the creation of new department rows. Update your user_form.xml file as follows:
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<!--...-->
<argument name="buttons" xsi:type="array">
<item name="add_department" xsi:type="string">Chanz\User\Block\Adminhtml\User\Edit\AddDepartmentButton</item>
<!--...-->
</argument>
</argument>
<!--...-->
</form>
Step 2: Create the Add Department Button
Create a new button class AddDepartmentButton.php in your module:
<?php
namespace Chanz\User\Block\Adminhtml\User\Edit;
class AddDepartmentButton extends \Magento\Backend\Block\Widget\Button
{
public function __construct(
\Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
\Magento\Framework\View\Element\Context $context
) {
parent::__construct($uiComponentFactory, $context);
}
protected function _toHtml()
{
return $this->button('Add Department', 'add', '', '', array(
'class' => 'action-tertiary',
'onclick' => "document.addForm.elements['dept'].value = ''; document.addForm.elements['add_department'].click()",
));
}
}
Step 3: Update the Form to Support Multiple Departments
Update your form’s dataProvider and field definitions:
<dataSource name="user_form_data_source">
<!--...-->
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="isArray" xsi:type="boolean">true</item>
</item>
</argument>
</dataSource>
<field name="departments">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" translate="true" xsi:type="string">Departments</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/components/abstract-select</item>
<item name="template" xsi:type="string">ui/form/field</item>
</item>
</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="componentType" xsi:type="string">array</item>
<item name="children" xsi:type="array">
<!-- Each department will be a child of this field -->
</item>
</item>
</argument>
</field>
Step 4: Create the Add Department JavaScript Function
In your user_form.js file, add the following function:
require(['jquery'], function($) {
$(document).ready(function() {
var departmentIndex = 0;
$('.add-department').click(function(e) {
e.preventDefault();
departmentIndex++;
var newField = document.createElement('input');
newField.setAttribute('name', 'departments[' + departmentIndex + ']');
newField.setAttribute('id', 'department_' + departmentIndex);
$('.departments-field').append(newField);
});
});
});
Step 5: Update the Form Template
Finally, update your user_form.html template to include a container for the dynamic department fields:
<div class="fieldset-wrapper">
<div class="admin__field field field-departments departments-field">
<!-- The dynamic department fields will be added here -->
</div>
</div>
<button type="button" class="add-department">Add Department</button>
Conclusion
By following these steps, you’ve successfully implemented a system for adding dynamic rows to the department field in your Magento2 admin form. This allows users to create multiple departments for each account.
Remember to clear and re-compile your cache after making these changes. If you encounter any issues or need further assistance, feel free to refer to our Magento development best practices guide for more insights.
Happy coding!