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 is not jQuery dependent.
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 REST API.
This sample mimics the scenario of retrieving large amounts of data, by setting a
setTimeout()
method. While the method is processing, the spinner is displayed.
Prepare the App
Create the Form
Create an App
that includes any types of fields. Save the form when finished.
(function() {
'use strict';
// Function to display the spinner
function showSpinner() {
// Initialize
if ($('.kintone-spinner').length === 0) {
// Create elements for the spinner and the background of the spinner
const spin_div = $('<div id ="kintone-spin" class="kintone-spinner"></div>');
const spin_bg_div = $('<div id ="kintone-spin-bg" class="kintone-spinner"></div>');
// Append spinner to the body
$(document.body).append(spin_div, spin_bg_div);
// Set a style for the spinner
$(spin_div).css({
'position': 'fixed',
'top': '50%',
'left': '50%',
'z-index': '510',
'background-color': '#fff',
'padding': '26px',
'-moz-border-radius': '4px',
'-webkit-border-radius': '4px',
'border-radius': '4px' });
$(spin_bg_div).css({
'position': 'fixed',
'top': '0px',
'left': '0px',
'z-index': '500',
'width': '100%',
'height': '200%',
'background-color': '#000',
'opacity': '0.5',
'filter': 'alpha(opacity=50)',
'-ms-filter': 'alpha(opacity=50)' });
// Set options for the spinner
const opts = {
'color': '#000' };
// Create the spinner
new Spinner(opts).spin(document.getElementById('kintone-spin'));
}
// Display the spinner
$('.kintone-spinner').show();
}
// Function to hide the spinner
function hideSpinner() {
// Hide the spinner
$('.kintone-spinner').hide();
}
// Record List Event
kintone.events.on('app.record.index.show', (event) => {
// Prevent duplication of the button
if (document.getElementById('my_index_button') !== null) {
return;
}
// Set a button
const myIndexButton = document.createElement('button');
myIndexButton.id = 'my_index_button';
myIndexButton.innerHTML = 'Click Me!';
// Button onclick function
myIndexButton.onclick = ()=> {
showSpinner(); // Display the spinner
setTimeout(() => {
hideSpinner(); // Hide the spinner
}, (3000));
};
// Retrieve the header menu space element and set the button there
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() process. During this process, an animated spinner should display in the center of the window.