How to Hide Select All From Magento 2 Admin Grids

In this tutorial, we will see how can we hide the “Select All” option in the admin grids in Magento 2.
Use case:
Hiding the “Select All” option can be useful in cases where you want to prevent accidental occurrences of mass delete of certain entities like products. Sometimes the admin user accidentally clicks Select All and then Delete. It may lead to disastrous consequences.

magento2-grid

Approach:
The Select All option is provided by Magento_Ui/js/grid/columns/multiselect.js file. So we will create a mixin that will remove the “Select All” option from the core Magento 2 js file. Please note that we are not overriding the core JS file. We are just adding our mixin so that other parts of the file are not affected.

Step 1: Create your new module Webspeaks_CustomGrid

Step 2: Create the requirejs-config.js

// app\code\Webspeaks\CustomGrid\view\adminhtml\requirejs-config.js
var config = {
    config: {
        mixins: {
            'Magento_Ui/js/grid/columns/multiselect': {
                'Webspeaks_CustomGrid/js/grid/columns/multiselect-mixin': true
            }
        }
    }
}

Step 3: Create the actual mixin file

The below code will remove the “Select All” options from all grids in Magento 2 admin panel.

// app\code\Webspeaks\CustomGrid\view\adminhtml\web\js\grid\columns\multiselect-mixin.js
define(function () {
    'use strict';

    var mixin = {
        initialize: function () {
            var obj = this._super();
            // Remove the Select All options from the dropdown
            this.actions.splice(0,3);
            return obj;
        },

        // Remove the "Select All On This Page" functionality
        selectPage: function() {
            return this;
        }
    };

    return function (target) {
        return target.extend(mixin);
    };
});

Step 4: Remove the options from a particular Grid

If you want to remove the options only from a particular grid then you can check the grid source like below.
Here we will remove the options only from catalog product listing grid.

define(function () {
    'use strict';

    var mixin = {
        initialize: function () {
            var obj = this._super();
            if (this.modules && this.modules.source && this.modules.source.indexOf('product_listing') !== -1) {
                this.actions.splice(0,3);
            }
            return obj;
        },
        selectPage: function() {
            if (this.modules && this.modules.source && this.modules.source.indexOf('product_listing') !== -1) {
                return this;
            } else {
                this._super();
            }
            return this;
        }
    };

    return function (target) {
        return target.extend(mixin);
    };
});

Step 5: Regenerate static content

rm -rf var/view_preprocessed pub/static/adminhtml
php bin/magento setup:static-content:deploy --area adminhtml

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/