You can select one or multiple items in a grid and then apply mass actions if they are defined. Here you will learn how to define mass actions in the XML layout.
Filters can be added via the XML layout argument mass_actions
. Each mass action has a label, URL and optional URL parameters. The Magento code has a lot of mass action URLs already defined for its own entities. Reuse them where needed.
<block>
<arguments>
<argument name="mass_actions" xsi:type="array">
<item name="enable" xsi:type="array">
<item name="label" xsi:type="string">Enable</item>
<item name="url" xsi:type="string">catalog/product/massStatus</item>
<item name="url_parameters" xsi:type="array">
<item name="status" xsi:type="number">1</item>
</item>
</item>
</argument>
</arguments>
</block>
See the grid reference for more details.
Instead of defining the mass action via XML layout, you can also use a PHP class for this. For this, create a new class that implements Loki\AdminComponents\Grid\MassAction\MassActionInterface
and pass it as follows:
<block>
<arguments>
<argument name="mass_actions" xsi:type="array">
<item name="custom" xsi:type="object">
YireoTraining\Example\Grid\GridFilter\CustomGridFilter
</item>
</argument>
</arguments>
</block>
Your custom class could extend from Loki\AdminComponents\Grid\MassAction\MassAction
but this is not required.
Each mass action definition is being read by the GridViewModel
class. If the definition is an array, it is passed to the Loki\AdminComponents\Grid\MassAction\MassActionFactory
class for creating a new instance of Loki\AdminComponents\Grid\MassAction\MassAction
.
The list of mass action instances is passed on the PHTML template and rendered as a simple dropdown. When a mass action is selected from the dropdown, the Alpine method massAction()
is being called. This method gathers the selected items in the grid, populates a hidden form massActionForm
with this, adds a proper form action URL and submits the form.