How to Add Loader in Magento 2 Admin on AJAX Request
Learn how to display a loading spinner (loader) in the Magento 2 admin panel during AJAX requests for better user experience.
Overview
When performing AJAX operations in Magento 2 admin, it is a good practice to show a loader to indicate that a process is running. Magento provides a built-in loader using jQuery widgets that can be easily integrated.
Step-by-Step Implementation
- Add a loader container in your admin HTML template.
- Use Magento's built-in
processStartandprocessStopevents. - Trigger the loader before and after your AJAX call.
HTML Template
Content here
JavaScript Example
require(['jquery'], function ($) {
$('#your-button').on('click', function () {
// Start Loader
$('body').trigger('processStart');
$.ajax({
url: 'your/ajax/url',
type: 'POST',
data: {},
success: function (response) {
console.log(response);
},
complete: function () {
// Stop Loader
$('body').trigger('processStop');
}
});
});
});
Alternative: Loader for Specific Container
require(['jquery'], function ($) {
var container = $('#loader-example');
container.trigger('processStart');
// After AJAX completes
container.trigger('processStop');
});
Important Notes
- Magento uses a built-in loader widget, so no external library is required.
processStartshows the loader, andprocessStophides it.- Always stop the loader in the
completecallback to avoid stuck loaders. - You can customize the loader UI via CSS if needed.