I want to export one attribute of product. For this I have to select all other attributes one by one for excluding. Can anyone please tell me How can I add the Select All option to select all the option at once for exclude.
5 Answers
Add a "select all" button beside "Reset Filter". We need add plugin to class Magento\Backend\Block\Widget\Grid\Extended with getMainButtonsHtml method. As below
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Backend\Block\Widget\Grid\Extended">
<plugin name="VendorName_ModuleName::After" type="VendorName\ModuleName\Plugin\Block\Widget\Grid\Extended" />
</type>
Make a Extended.php file under VendorName\ModuleName\Plugin\Block\Widget\Grid path. We have written selectAll javascript function.
public function afterGetMainButtonsHtml(\Magento\Backend\Block\Widget\Grid\Extended $subject, $result)
{
$result .= '<button id="" title="Select All" type="button" class="action-default scalable action-reset action-tertiary" onclick="export_filter_gridJsObject.selectAll()" data-action="grid-filter-reset" data-ui-id="widget-button-3"><span>Select All</span></button>';
return $result;
}
Now we will have to extend phtml file too. For javascript function (path :magento\module-import-export\view\adminhtml\templates\export\form\filter\after.phtml)
Add this script.
export_filter_gridJsObject.selectAll = function () {
// class admin__control-checkbox is selector
var inputs = document.getElementsByClassName("admin__control-checkbox");
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type == "checkbox") {
inputs[i].checked = true;
}
}
};
Hope this will help.
Instead of creating a module, this can be achieved with the below Javascript. Run the below code in the browser CONSOLE to select all checkbox from the export section.
jQuery('.checkbox.admin__control-checkbox').click();
Hope it helps!!!
Ideally and in proper Magento standard way it should be -
Create a module Vendorname_MagentoImportExportExtend
- Create
app/code/Vendorname/MagentoImportExportExtend/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendorname_MagentoImportExportExtend', __DIR__ );
- Create
app/code/Vendorname/MagentoImportExportExtend/composer.json
{ "name": "vendorname/magento-import-export-extend", "description": "N/A", "require": { "php": "~7.3.0||~7.4.0", "magento/framework": "103.0.*", "magento/module-backend": "102.0.*", "magento/module-import-export": "101.0.*" }, "type": "magento2-module", "version": "1.0.1", "license": [ "MIT" ], "autoload": { "files": [ "registration.php" ], "psr-4": { "Vendorname\\MagentoImportExportExtend\\": "" } } }
- Create
app/code/Vendorname/MagentoImportExportExtend/etc/module.xml
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendorname_MagentoImportExportExtend"> <sequence> <module name="Magento_Backend"/> <module name="Magento_ImportExport"/> </sequence> </module> </config>
- Create
app/code/Vendorname/MagentoImportExportExtend/etc/adminhtml/events.xml
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="backend_block_widget_grid_prepare_grid_before"> <observer name="backend_importexport_filter_grid_add_additional_button" instance="Vendorname\MagentoImportExportExtend\Observer\Backend\Block\Widget\Grid\Extended\Button"/> </event> </config>
- Create
app/code/Vendorname/MagentoImportExportExtend/Observer/Backend/Block/Widget/Grid/Extended/Button.php
<?php declare(strict_types = 1); namespace Vendorname\MagentoImportExportExtend\Observer\Backend\Block\Widget\Grid\Extended; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Backend\Block\Widget\Grid; class Button implements ObserverInterface { /** * Apply select all button * * @param Observer $observer * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function execute(Observer $observer) { /** @var \Magento\Backend\Block\Widget\Grid $grid */ $grid = $observer->getEvent()->getData('grid'); if ($grid instanceof Grid && $grid->getId() == 'export_filter_grid') { $grid->setChild( 'select_all_button', $grid->getLayout()->createBlock( \Magento\Backend\Block\Widget\Button::class )->setData( [ 'label' => __('Select All'), 'onclick' => $grid->getJsObjectName() . '.selectAll()', 'class' => 'action-selectall select action-tertiary' ] )->setDataAttribute(['action' => 'grid-select-all']) ); } } }
- Create
app/code/Vendorname/MagentoImportExportExtend/etc/adminhtml/di.xml
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Backend\Block\Widget\Grid\Extended"> <plugin name="backend_importexport_filter_grid_additional_button" sortOrder="10" type="Vendorname\MagentoImportExportExtend\Plugin\Backend\Block\Widget\Grid\Extended"/> </type> </config>
- Create
app/code/Vendorname/MagentoImportExportExtend/Plugin/Backend/Block/Widget/Grid/Extended.php
<?php declare(strict_types = 1); namespace Vendorname\MagentoImportExportExtend\Plugin\Backend\Block\Widget\Grid; class Extended { /** * @param \Magento\Backend\Block\Widget\Grid\Extended $subject * @param $result * @return mixed|string */ public function afterGetMainButtonsHtml( \Magento\Backend\Block\Widget\Grid\Extended $subject, $result ) { if ($subject->getId() == 'export_filter_grid' && $subject->getFilterVisibility()) { $result .= $subject->getChildHtml('select_all_button'); } return $result; } }
- Create
app/code/Vendorname/MagentoImportExportExtend/view/adminhtml/layout/adminhtml_export_getfilter.xml
<?xml version="1.0"?> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd"> <container name="root" label="Root"> <block class="Magento\ImportExport\Block\Adminhtml\Export\Filter" name="export.filter"/> <block class="Magento\Backend\Block\Template" template="Magento_ImportExport::export/form/filter/after.phtml" name="export.form.after" as="form_after"/> <block class="Magento\Backend\Block\Template" template="Vendorname_MagentoImportExportExtend::export/form/filter/after.phtml" name="wcl.export.form.after" after="-" /> </container> </layout>
- Create
app/code/Vendorname/MagentoImportExportExtend/view/adminhtml/templates/export/form/filter/after.phtml
<?php /** @var \Magento\Backend\Block\Template $block */ /** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ ?> <?php $scriptString = <<<script require([ 'mage/adminhtml/grid', 'mage/translate' ], function(grid, t){ if (window['export_filter_gridJsObject'] != undefined) { export_filter_gridJsObject.selectAll = function () { var element = document.querySelector(".action-selectall"); if (element.classList.contains('select')) { element.classList.remove('select'); element.classList.add('deselect'); var inputs = document.getElementsByClassName("admin__control-checkbox"); for(var i = 0; i < inputs.length; i++) { if(inputs[i].type == "checkbox") { inputs[i].checked = true; } } element.firstChild.innerHTML = t('Deselect All'); } else if (element.classList.contains('deselect')) { element.classList.remove('deselect'); element.classList.add('select'); var inputs = document.getElementsByClassName("admin__control-checkbox"); for(var i = 0; i < inputs.length; i++) { if(inputs[i].type == "checkbox") { inputs[i].checked = false; } } element.firstChild.innerHTML = t('Select All'); } }; } }); script; ?> <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
It is tested with Magento 2.4.2.
We have done coding to add, "Select All" with the Export filter.
Create Czargroup\Exportproduct\etc\adminhtml\di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Backend\Block\Widget\Grid\Extended">
<plugin disabled="false" name="Czargroup_Exportproduct_Plugin_Backend_Magento_Backend_Block_Widget_Grid_Extended" sortOrder="10" type="Czargroup\Exportproduct\Plugin\Backend\Magento\Backend\Block\Widget\Grid\Extended"/>
</type>
</config>
And Czargroup\Exportproduct\etc\module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Czargroup_Exportproduct" setup_version="1.0.0">
<sequence>
<module name="Magento_Backend"/>
</sequence>
</module>
</config>
And Czargroup\Exportproduct\Plugin\Backend\Magento\Backend\Block\Widget\Grid\Extended.php
<?php
namespace Czargroup\Exportproduct\Plugin\Backend\Magento\Backend\Block\Widget\Grid;
class Extended
{
public function afterGetMainButtonsHtml(
\Magento\Backend\Block\Widget\Grid\Extended $subject,
$result
) {
$result .= '<button id="" title="Select All" type="button" class="action-default scalable action-reset action-tertiary" onclick="export_filter_gridJsObject.selectAll()" data-action="grid-filter-reset" data-ui-id="widget-button-3"><span>Select All</span></button>';
return $result;
}
}
And /vendor/magento/module-import-export/view/adminhtml/templates/export/form/filter/after.phtml
Line 18
export_filter_gridJsObject.selectAll = function () {
// class admin__control-checkbox is selector
var inputs = document.getElementsByClassName("admin__control-checkbox");
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type == "checkbox") {
inputs[i].checked = true;
}
}
};
And Czargroup\Exportproduct\registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Czargroup_Exportproduct',
__DIR__
);
And Czargroup\Exportproduct\composer.json
{
"name": "czargroup/module-exportproduct",
"description": "Add Select All option in Export Settings",
"type": "magento2-module",
"license": "proprietary",
"authors": [
{
"email": "[email protected]",
"name": "Mage2Gen"
},
{
"email": "[email protected]",
"name": "Pratik Kamani"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"Czargroup\\Exportproduct\\": ""
},
"files": [
"registration.php"
]
}
}
app/code/Vendor/Exportselectall/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Backend\Block\Widget\Grid\Extended">
<plugin name="vendor_exportselectall::Before" type="vendor\Exportselectall\Plugin\Block\Widget\Grid\Extended" />
</type>
</config>
vendor\Exportselectall\Plugin\Block\Widget\Grid\Extended.php
<?php
namespace vendor\Exportselectall\Plugin\Block\Widget\Grid;
class Extended
{
public function afterGetMainButtonsHtml(\Magento\Backend\Block\Widget\Grid\Extended $subject, $result)
{
$result .= '<button id="select_all_button" title="Select All" type="button" class="action-default scalable action-reset action-tertiary" data-action="grid-filter-reset" data-ui-id="widget-button-3"><span>Select All</span></button>';
return $result;
}
}
vendor/Exportselectall/view/adminhtml/layout/adminhtml_export_getfilter.xml
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="root" label="Root">
<block class="Magento\ImportExport\Block\Adminhtml\Export\Filter" name="export.filter"/>
<block class="Magento\Backend\Block\Template" template="Kensium_Exportselectall::export/form/filter/after.phtml" name="export.form.after" as="form_after"/>
</container>
</layout>
To get proper solution you can past the below code to get select and unselect vendor/Exportselectall/view/adminhtml/templates/export/form/filter/after.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Backend\Block\Template $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<?php $scriptString = <<<script
require([
'mage/adminhtml/grid'
], function(){
if (window['export_filter_gridJsObject'] != undefined) {
export_filter_gridJsObject.resetFilter = function () {
return varienGrid.prototype.resetFilter.call(this, varienExport.modifyFilterGrid);
};
export_filter_gridJsObject.doFilter = function () {
return varienGrid.prototype.doFilter.call(this, varienExport.modifyFilterGrid);
};
}
});
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
<?php $scriptString = <<<script
require(['jquery', 'mage/url', 'mage/translate'], function($, urlBuilder) {
$(document).ready(function() {
// Function to initialize select/unselect functionality
function initializeSelectUnselect() {
if (typeof export_filter_gridJsObject !== 'undefined') {
// Function to select/unselect all checkboxes
export_filter_gridJsObject.toggleSelectAll = function () {
var inputs = document.getElementsByClassName("admin__control-checkbox");
var allChecked = true;
// Check if all checkboxes are already checked
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type === "checkbox" && !inputs[i].checked) {
allChecked = false;
break;
}
}
// Toggle checkboxes based on current state
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type === "checkbox") {
inputs[i].checked = !allChecked;
}
}
// Update button text
var button = $('#select_all_button');
if (allChecked) {
button.find('span').text($.mage.__('Select All'));
} else {
button.find('span').text($.mage.__('Unselect All'));
}
};
// Bind the function to the button click event
$('#select_all_button').on('click', function() {
export_filter_gridJsObject.toggleSelectAll();
});
} else {
// Try again after a short delay if export_filter_gridJsObject is not available
setTimeout(initializeSelectUnselect, 100);
}
}
// Initialize on document ready
initializeSelectUnselect();
// Reinitialize on AJAX grid update
$(document).on('contentUpdated', function() {
initializeSelectUnselect();
});
});
});
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
enter image description here
<config xmlns:...><type name="M..</type>
into? For the second bit I can see where the new php goes into extended.php. Thanks Mike