How to Remove Column from Admin Grid Dynamically in Magento 2
Magento 2 CMS facilitates admin users to customize the default features in order to ease the task of administrating the Magento 2 store and effectively manage the orders and customers.
For instance, the admin can add a custom column in products grid.
Now, what if the admin wants to remove column from the admin grid dynamically in Magento 2?
It may happen that when a custom module is created in Magento 2, it requires adding a custom column in the admin grid. And when the custom module is disabled, one would want to remove that custom column too.
In that scenario, you need to also fix Magento 2 reset button in grid column not working and use the below solution to remove column from admin grid dynamically in Magento 2.
Method to Remove Column From Admin Grid in Magento 2 Dynamically:
- Use the below code in the ui_component_file.xml file at app/code/Meetanshi/CustomerLogin/view/adminhtml/ui_component
1234567891011121314<?xml version="1.0" encoding="UTF-8"?><listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"><columns name="sales_order_columns"><actionsColumn name="login_actions" class="Meetanshi\CustomerLogin\Ui\Component\Listing\Column\OrderActions"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="label" xsi:type="string">Customer Login</item><item name="indexField" xsi:type="string">entity_id</item></item></argument></actionsColumn></columns></listing> - Use the below code in the OrderActions.php file at app/code/Meetanshi/CustomerLogin/Ui/Component/Listing/Column
123456789101112131415161718192021222324<?phpnamespace Meetanshi\CustomerLogin\Ui\Component\Listing\Column;use Magento\Framework\Exception\LocalizedException;use Magento\Ui\Component\Listing\Columns\Column;class OrderActions2 extends Column{/*** @return void* @throws LocalizedException*/public function prepare(){if ($condition) { // Your condition goes here$status = true;}$this->_data['config']['componentDisabled'] = true; // for removing the columnparent::prepare();}}
There can be multiple instances when you require to disable UI grid column in Magento 2 based on the conditions. For example, there are multiple admin users in your Magento store and you want to hide certain information in the backend grid from certain user types. Or let’s say you have developed a custom module with a custom column in the admin grid, and you want to hide that grid when the extension is disabled.
You can also add a custom condition in the above code to remove a column from the admin grid based on the conditions.
That’s it.
If you still face any problem, in removing the column from Magento 2 admin panel, do mention them in the Comments section below.
I would be happy to help.
Don’t forget to share this solution with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Top 10 Cheapest, Fastest & Best Courier Services in India
How to Use “checkout_type_multishipping_create_orders_single” Event in Magento 2
Next