$p.on
Overview¶
This function executes the specified process when an event occurs in a monitored column.
Syntax¶
JavaScript¶
Description Of Each Parameter¶
| Parameter name | Description |
|---|---|
| Event name | Ajax event name |
| Monitoring column | Column name in database of the target column |
| Optional processing | Processing when the event is fired |
Usage Examples¶
JavaScript¶
Code Samples¶
Note
Modify 【 ... 】 in the code as necessary.
1. Check for Duplicates by a Combination of Multiple Columns
On the new and edit screens, every time the value of Title, Target Date, or Creator (ClassA, ClassB, ClassC) is changed, a key that combines the values of the three columns is automatically set in ClassG.
By setting "Duplication prohibited" for ClassG, you can detect duplicates in the combination of the three columns.
Practical Use Cases¶
"Duplication prohibited" in Pleasanter's column settings can be set only for a single column.
If you want to prevent duplicates in a combination of multiple columns, such as Title, Target Date, and Creator, you need to prepare a separate column that stores the combined values of the target columns, and set Duplication prohibited for that column.
This sample handles the part that automatically generates that combined key.
Preparations¶
- Prepare the three columns that you want to check for duplicates (example: ClassA = Title, ClassB = Target Date, ClassC = Creator).
- Prepare one class column to store the combined key (example: ClassG).
- On the "Manage table > Editor" tab, select ClassG.
- Select the "Duplication prohibited" check box.
- The script works even if ClassG remains displayed on the screen as a normal column. If you do not want users to see it, consider a separate way to hide it (this sample does not cover hiding it).
- Register the script.
- On the "Manage table > Scripts" tab, select "New".
| Setting | Value |
|---|---|
| Title | Any name (example: Key generation for the three-column duplicate check) |
| Script | Paste the sample code below |
| Output to | Clear the "All" check box, and select "New" and "Edit" |
// Combine the values of ClassA, ClassB, and ClassC, and set the result in ClassG for the duplicate check
function setDuplicateKey() {
$p.set(
$p.getControl('ClassG'),
`${$p.getValue('ClassA')}${$p.getValue('ClassB')}${$p.getValue('ClassC')}`,
);
}
// When ClassA, ClassB, or ClassC is changed, regenerate the combined key
$p.on('change', 'ClassA', function () {
setDuplicateKey();
});
$p.on('change', 'ClassB', function () {
setDuplicateKey();
});
$p.on('change', 'ClassC', function () {
setDuplicateKey();
});