Create Dynamic Mass Action In Magento2 Grid : if you want to create dynamic mass action in any custom grid of magento2, to achieve this you have to follow some steps .
1. Write mass action code in your ui component phtml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<massaction name="listing_massaction"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="selectProvider" xsi:type="string">dynamicmassaction_massaction_listing.dynamicmassaction_massaction_listing.dynamicmassaction_massaction_columns.ids</item> <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item> <item name="indexField" xsi:type="string">entity_id</item> </item> </argument> <action name="assign_badge"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="type" xsi:type="string">assign_badge</item> <item name="label" xsi:type="string" translate="true">Assign badge</item> </item> </argument> <argument name="actions" xsi:type="configurableObject"> <argument name="class" xsi:type="string">Webkul\DynamicMassaction\Ui\Component\MassAction\Badge\Assignoptions</argument> <argument name="data" xsi:type="array"> <item name="urlPath" xsi:type="string">dynamicmassaction/managemassaction/massAssignBadge</item> <item name="paramName" xsi:type="string">entity_id</item> <item name="confirm" xsi:type="array"> <item name="title" xsi:type="string" translate="true">Assign badge</item> <item name="message" xsi:type="string" translate="true">Are you sure to assign selected badge to customer?</item> </item> </argument> </argument> </action> </massaction> |
Lets take chunk of the above code for better understanding
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<argument name="actions" xsi:type="configurableObject"> <argument name="class" xsi:type="string">Webkul\DynamicMassaction\Ui\Component\MassAction\Badge\Assignoptions</argument> <argument name="data" xsi:type="array"> <item name="urlPath" xsi:type="string">dynamicmassaction/managemassaction/massAssignBadge</item> <item name="paramName" xsi:type="string">entity_id</item> <item name="confirm" xsi:type="array"> <item name="title" xsi:type="string" translate="true">Assign badge</item> <item name="message" xsi:type="string" translate="true">Are you sure to assign selected badge to customer?</item> </item> </argument> </argument> here you will see a line, where you have to set the path for manipulation of dynamic massaction |
1 2 |
<argument name="class" xsi:type="string">Webkul\DynamicMassaction\Ui\Component\MassAction\Badge\Assignoptions</argument> <argument name="data" xsi:type="array"> |
2. in this path Webkul\DynamicMassaction\Ui\Component\MassAction\Badge\Assignoptions you need to manipulate your massaction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
<?php namespace Webkul\DynamicMassaction\Ui\Component\MassAction\Badge; use Magento\Framework\UrlInterface; use Zend\Stdlib\JsonSerializable; use Webkul\DynamicMassaction\Model\ResourceModel\Badge\CollectionFactory; /** * Class Options */ class Assignoptions implements JsonSerializable { /** * @var array */ protected $options; /** * @var CollectionFactory */ protected $collectionFactory; /** * Additional options params * * @var array */ protected $data; /** * @var UrlInterface */ protected $urlBuilder; /** * Base URL for subactions * * @var string */ protected $urlPath; /** * Param name for subactions * * @var string */ protected $paramName; /** * Additional params for subactions * * @var array */ protected $additionalData = []; /** * Constructor * * @param CollectionFactory $collectionFactory * @param UrlInterface $urlBuilder * @param array $data */ public function __construct( CollectionFactory $collectionFactory, UrlInterface $urlBuilder, array $data = [] ) { $this->collectionFactory = $collectionFactory; $this->data = $data; $this->urlBuilder = $urlBuilder; } /** * Get action options * * @return array */ public function jsonSerialize() { $i=0; if ($this->options === null) { // get the massaction data from the database table $badgeColl = $this->collectionFactory->create()->addFieldToFilter('status',['eq'=>1]); if(!count($badgeColl)){ return $this->options; } //make a array of massaction foreach ($badgeColl as $key => $badge) { $options[$i]['value']=$badge->getEntityId(); $options[$i]['label']=$badge->getBadgeName(); $i++; } $this->prepareData(); foreach ($options as $optionCode) { $this->options[$optionCode['value']] = [ 'type' => 'badge_' . $optionCode['value'], 'label' => $optionCode['label'], ]; if ($this->urlPath && $this->paramName) { $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl( $this->urlPath, [$this->paramName => $optionCode['value']] ); } $this->options[$optionCode['value']] = array_merge_recursive( $this->options[$optionCode['value']], $this->additionalData ); } // return the massaction data $this->options = array_values($this->options); } return $this->options; } /** * Prepare addition data for subactions * * @return void */ protected function prepareData() { foreach ($this->data as $key => $value) { switch ($key) { case 'urlPath': $this->urlPath = $value; break; case 'paramName': $this->paramName = $value; break; default: $this->additionalData[$key] = $value; break; } } } } |
3. Now you will see your dynamically added mass actions are displaying on grid.
So in this way, you can add dynamic massaction in your grid.Hope this blog will help you.
Thanks for sharing snippet for the dynamic mass action,i have tried using above snippet but it is not giving sub mass action.
Can you share sample module code so that we can compare entire Module.
Looking forward for your response.
It will helpful if you guys sharing github link for code which you have shared because there are many instances where people doing minor issue.
Thanks