This article introduces how to display a loading spinner on Kintone Apps using
spin.js
.
What is spin.js
spin.js
is a library that displays a spinner, commonly used as a loading icon. This library does not require any CSS libraries and does not depend on jQuery.
Use-case for Spinners
In a common Kintone use-case, the spinner can be displayed when a large volume of data is being processed with the REST API.
This sample mimics retrieving large amounts of data by using
setTimeout()
. While the timer runs, the spinner is displayed.
Prepare the App
Create the Form
Create an App
that includes fields of any type. Save the form when finished.
(() => {
'use strict';
// Function to display the spinner
const showSpinner = () => {
// Initialize
if (document.querySelectorAll('.kintone-spinner').length === 0) {
// Create elements for the spinner and the background of the spinner
const spinDiv = document.createElement('div');
spinDiv.id = 'kintone-spin';
spinDiv.classList.add('kintone-spinner');
const spinBgDiv = document.createElement('div');
spinBgDiv.id = 'kintone-spin-bg';
spinBgDiv.classList.add('kintone-spinner');
// Append spinner to the body
document.body.appendChild(spinDiv);
document.body.appendChild(spinBgDiv);
// Set a style for the spinner
spinDiv.style.cssText =
'position: fixed; top: 50%; left: 50%; z-index: 510; background-color: #fff; padding: 26px; border-radius: 4px;';
spinBgDiv.style.cssText =
'position: fixed; top: 0px; left: 0px; z-index: 500; width: 100%; height: 100%; background-color: #000; opacity: 0.5;';
// Set options for the spinner
const opts = {color: '#000'};
// Create the spinner
new Spinner(opts).spin(spinDiv);
}
document.querySelectorAll('.kintone-spinner').forEach(element => {
element.style.display = 'block';
});
};
// Function to hide the spinner
const hideSpinner = () => {
document.querySelectorAll('.kintone-spinner').forEach((element) => {
element.style.display = 'none';
});
};
// Record List Event
kintone.events.on('app.record.index.show', () => {
if (document.getElementById('my_index_button') !== null) {
return;
}
// Set a button
const myIndexButton = document.createElement('button');
myIndexButton.id = 'my_index_button';
myIndexButton.textContent = 'Click Me!';
// Button onclick function
myIndexButton.onclick = () => {
showSpinner();
setTimeout(() => {
hideSpinner();
}, 3000);
};
kintone.app.getHeaderMenuSpaceElement().appendChild(myIndexButton);
});
})();
Test the Code
Navigate to the Record List page. A button should be displayed above the list of records. Click the button to start the setTimeout() timer. While the timer runs, an animated spinner should appear in the center of the window.