I'm trying to implement a "block loader" animation on the cart summary
element on the cart page while the cart totals
are loading.
The template file which loads this is Magento_Checkout/templates/cart/totals.phtml
.
It loads the totals with knockout.js
and reads the html templates from checkout_cart_index.xml
:
<!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
{
"#cart-totals": {
"Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
}
}
</script>
EDIT
Magento has a default loader on the cart totals when the customer clicks on the estimated shipping country, but I find this functionality useless, so I've disabled it.
I'm trying to show the loader animation when the cart page is loading. There is always a delay before the totals show up, so the my goal is to show a loader here before they are fully loaded.
I've made several attempts in Magento_Checkout::templates/cart/totals.phtml
Attempt 1 – through x-magento-init script
<div id="cart-totals" class="cart-totals" data-bind="scope:'block-totals'">
<!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
{
"#cart-totals": {
"Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
},
"*": { // I tried with #cart-totals here as well
"Magento_Ui/js/block-loader": "<?= /* @escapeNotVerified */ $block->getViewFileUrl('images/loader-2.gif') ?>"
}
}
</script>
</div>
Attempt 2 – by triggering processStart on the element
<div id="cart-totals" class="cart-totals" data-bind="scope:'block-totals'" data-mage-init='{"loader": {}}'>
<!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
{
"#cart-totals": {
"Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
}
}
</script>
</div>
<script>
require([
'jquery',
'domReady!'
], function ($) {
$('#cart-totals').trigger('processStart');
})
</script>
Attempt 3 – data-mage-init
<div id="cart-totals" class="cart-totals" data-bind="scope:'block-totals'" data-mage-init='{"loader": {}}'>
<!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
{
"#cart-totals": {
"Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
}
}
</script>
</div>
Each attempt failed. It just doesn't show the loader.
What am I missing here?
Best Answer
Did you want to add loader into templates/cart/totals.phtml These some ways to add loader spinner into block or page.
1 Use:
<div data-bind="blockLoader: isLoadding"> /** Block Content HTML */ </div>
,with this option you should add isLoadding : ko.observable(false) into your Component
Example: Block XML Define
Block templates.phtml CODE
Script
Javascript code: Namespace_ModuleName/js/componentjsfile
2: With
<div class="loader">
using rjsResolverjs file named "Namespace_ModuleName/js/abc"
PHTML file