How to Create a Settings Page
Contents
- Overview
- STEP 1: Complete the Steps in the Previous Article
- STEP 2: Create an HTML file for the Plug-in Settings Page
- STEP 3: Create a CSS file for the Plug-in Settings Page
- STEP 4: Create a JavaScript file for the Plug-in Settings Page
- STEP 5: Update the Desktop JavaScript File
- STEP 6: Update the Manifest.json File
- STEP 7: Repackage and Upload the Plug-in
- STEP 8: Test the Plug-in
Overview
This article introduces how to create a Kintone plug-in's settings page.
The App used in this article is based on the following article:
How to Create Kintone Plug-ins
Creating a plug-in settings page allows end users to set values that will be used in the JavaScript customization.
STEP 1: Complete the Steps in the Previous Article
Complete the steps in the following article:
How to Create Kintone Plug-ins
STEP 2: Create an HTML file for the Plug-in Settings Page
Create an "html" directory under the "sample-plugin" directory.
|
|
Next, create an HTML file and name it "config.html". Save it under the "html" directory. This HTML file will be used for creating the plug-in settings page.
Set up the HTML, so that users can input field codes into text boxes.
|
|
In this sample code, the class name is set so that the 51-modern-default style is applied. 51-modern-default styles HTML elements on the plug-in settings page to fit in with Kintone's UI. Refer to the following link for more details:
51-modern-default
The details of the class and id names are as follows.
- Class names starting with
kintoneplugin-
are CSS class names to which 51-modern-default is applied. - The
setting
class is used for the CSS settings . - The id names starting with
checkvalue-field-
are used in the JavaScript settings .
STEP 3: Create a CSS file for the Plug-in Settings Page
Create a "css" directory under the "sample-plugin" directory.
|
|
Next, prepare two CSS files to style the settings page:
-
51-modern-default.css
Save the contents of the 51-modern-default.css file and name it "51-modern-default.css".
51-modern-default.css -
config.css
Save the following contents into a file named "config.css". This code adjusts the margins of the div elements.1 2 3
.setting { padding: 0 0 20px 0; }
Save both files under the "css" directory.
STEP 4: Create a JavaScript file for the Plug-in Settings Page
The JavaScript file preforms the following operations on the settings page:
- If available, get the plug-in settings that were previously set by the user, and display the values as initial values on the form.
- Click the Cancel button to navigate back to the App's Plug-ins page without saving the settings.
- Click the Save button to check the required and duplicated fields. In case of errors, show error messages. If successful, save the settings, and navigate the user back to the App's Plug-ins page.
Save the following contents into a file named "config.js". Save it under the "js" directory.
|
|
Code Explanation
Get the plug-in configuration settings
The configuration settings of the plug-in is retrieved using the Get Config API. The argument pluginId
is the plugin ID.
In this immediately invoked function, the plug-in ID is provided with kintone.$PLUGIN_ID
. This is passed on to the PLUGIN_ID
parameter of the function. The parameter is used by the getConfig()
function to get the plug-in configuration settings.
|
|
Display the retrieved configuration settings
If available, the plug-in settings are retrieved and the values are displayed as initial values on the form.
The form information is retrieved using an id name starting with checkvalue-field-
.
|
|
Set the cancel button
The cancel button is implemented.
The path is set to /k/admin/app/{APP_ID}/plugin/
, which is the Plug-in list page of the App.
The {APP_ID}
is the App ID of the Kintone App and is obtained by using the Get App ID API:
Get App ID
|
|
Set the save button
When the Save button is clicked, the following checks are performed on the values in the form:
- Checks for required values
If any required values are missing, error messages will be displayed. - Checks for duplicated values
If duplicated values are entered in the form, error messages will be displayed.
|
|
To prevent Cross-Site Scripting (XSS), the escapeHtml()
function is used to escape the selected values.
|
|
Save the plug-in settings
If all checks are passed, the plug-in configuration settings will be saved in the plug-in using the Set Config API.
Set Config
The config
parameter for the API is the plug-in configuration settings to be saved. In this case, an object named newConfig
.
The second argument, successCallback
, can be used for the process that follows after the configuration is saved, which is navigating back to the App Settings page. The path to the App Settings page is /k/admin/app/flow?app={APP_ID}
.
|
|
STEP 5: Update the Desktop JavaScript File
Update the Desktop JavaScript file so that it can use values saved in the plug-in configuration settings. This allows developers to avoid hard-coding field code names into the code.
Edit the "desktop.js" file created in the following article:
How to Create Kintone Plug-ins
Use the Get Config API to get the plug-in's configuration settings.
Get Config
Assign the retrieved plug-in configuration settings to the config
variable. Set field codes that will be used in this code, from this object.
|
|
STEP 6: Update the Manifest.json File
Update the Manifest.json file to add the path to the new files created in this tutorial.
The key config.required_params
specifies an array of key names for the required fields. These keys are the keys within the object that are set with the Set Config API.
This allows the message "Required field has not been set" to be displayed on the App's Plug-in list page, if the required fields are not saved with setConfig()
.
Update version
to indicate that the plug-in has been updated.
|
|
Directory structure
Once all the files are prepared, the file tree should now look like the following structure:
|
|
STEP 7: Repackage and Upload the Plug-in
Follow the steps in the following article to repackage the plug-in:
Package the Plug-in Files
To repackage, specify the private key file in the --ppk
option.
|
|
Follow the steps in the following article to install the plug-in into Kintone:
Install the Plug-in into Kintone
If the file was uploaded successfully, the version added in "manifest.json" will be displayed under the plug-in name on the Plug-ins page.
STEP 8: Test the Plug-in
From the App Settings page, click on Plug-ins. Click on the gear button to set the field codes.
Enter the field codes into the text fields.
For the Customer Database App, the field codes are as follows:
Field Name | Field Code |
---|---|
Telephone # | telephone_number |
Confirm that the following operations work:
- Clicking the cancel button will redirect the user to the Plug-ins Settings page
- Clicking the save button without entering a value will cause an error message to appear.
- Setting duplicate values and clicking save will cause an error message to appear.
- After entering all the values and clicking save, users will be redirected to the App Settings page.
- On the Plug-in Settings page, the values that were previously set are displayed as the initial values.
Navigate to the App and add a new record. Enter invalid values for the Email and Telephone Number; an error message should be displayed.