The Category data type represents a list of options from which one or more can be selected. You can retrieve, pass, and filter these options using scripts. The following properties are used for this:
You can get all options of a Category field, including their names and codes. For example, to write the available options into a String field when a button is clicked on a page, attach the following script to the button:
asyncfunctionshowCategoryProperties(): Promise<void> {
// Get an array of all option names and convert it to a stringconst allCategoryValues = Context.fields.category.data.variants.map(v => v.name).join(', ');
// Get an array of all option codes and convert it to a stringconst allCategoryCodes = Context.fields.category.data.variants.map(v => v.code).join(', ');
// Define a String-type variable to store the result
Context.data.category_properties = ``;
// Add all option names to the result
Context.data.category_properties += `Options: ${allCategoryValues}
`;
// Add all option codes to the result
Context.data.category_properties += `Codes: ${allCategoryCodes}
`;
}
You can retrieve the category options selected by a user. For example, if an employee selects values in a Category field on a page and clicks a button, the selected option names and their codes can be displayed in a String field. To do this, attach one of the following scripts to the button:
for a Category field (one):
asyncfunctionshowSelectedOption(): Promise<void> {
// Define a String-type variable to store the result
Context.data.selected_option = ``;
// Get the name of the selected category option and write it to the String field// If not specified, display \"not set\
Context.data.selected_option += `Selected option: ${Context.data.category?.name ? Context.data.category.name : 'not set'}
`// Get the code of the selected category option and write it to the String field// If not specified, display \"not set\
Context.data.selected_option += `Selected option code: ${Context.data.category?.code ? Context.data.category.code : 'not set'}
`
}
For a Category field (many):
asyncfunctionshowSelectedOptions(): Promise<void> {
// Get an array of selected option names and convert it to a stringconst allSelectedValues = Context.data.multi_category?.map(v => v.name).join(', ');
// Get an array of selected option codes and convert it to a stringconst allSelectedCodes = Context.data.multi_category?.map(v => v.code).join(', ');
// Define a String-type variable to store the result
Context.data.selected_options = ``;
// Add selected option names to the result
Context.data.selected_options += `Selected options: ${allSelectedValues}
`// Add selected option codes to the result
Context.data.selected_options += `Selected option codes: ${allSelectedCodes}
`
}
You can dynamically create category options using a script, for example, when the page is opened.
To do this, add a Category variable to the page context without specifying options manually, and configure it as follows:
asyncfunctiononInit() {
// Create the souorce array of optionsconst categoryArray = ['repair', 'inspect', 'replace'];
// Convert the string array into an array of category option objects
Context.fields.category.data.variants = categoryArray.map((name): TEnum<any> => ({
name: name,
code: name,
}));
}
You can configure the creation of an app item and save the selected category options into its page using a script.
The user selects the desired options on the page and clicks a button. Then, an item is added to the app with the selected values:
In the app context, create a property of type Category without adding any options. This is necessary so the selected values
from the page can appear in the app item page.
In the page context, add:
Category property with a set of options;
App propety where you specify the app in which the items will be created
using the options selected on the page.
Go to the Template tab and place the Category field and the Button widget on the form..
Attach the following script to the button::
asyncfunctioncreateAppItem(): Promise<void> {
// Create a new app itemconst app = Context.fields.application.app.create();
// Set the name of the item
app.data.__name = `Request`;
// Copy the selected category options from the page context into the app itemя
app.data.empty_category = Context.data.category;
// Save the newly created itemawait app.save();
}
You can apply filters to category options in a script to display only the relevant ones on a page or form.
For example, if the user fills out a String field, some options are excluded from the Category field. To do this, link the following function to the On value change event of the String field:
asyncfunctionconditionChange(): Promise<void> {
// Check if the user has entered a value in the fieldif (Context.data.condition) {
// Filter the category options, removing those with codes 'repair' and 'replace'
Context.fields.category.data.variants = Context.fields.category.data.variants.filter(t => t.code !== 'repair' && t.code !== 'replace');
}
// If the user hasn't entered a condition, all options remain visible
}
Getting started with the Category data type
The Category data type represents a list of options from which one or more can be selected. You can retrieve, pass, and filter these options using scripts. The following properties are used for this:
Category field property EnumField.variants — all category options;
option properties:
Getting all category options
You can get all options of a Category field, including their names and codes. For example, to write the available options into a String field when a button is clicked on a page, attach the following script to the button:
async function showCategoryProperties(): Promise<void> { // Get an array of all option names and convert it to a string const allCategoryValues = Context.fields.category.data.variants.map(v => v.name).join(', '); // Get an array of all option codes and convert it to a string const allCategoryCodes = Context.fields.category.data.variants.map(v => v.code).join(', '); // Define a String-type variable to store the result Context.data.category_properties = ``; // Add all option names to the result Context.data.category_properties += `Options: ${allCategoryValues} `; // Add all option codes to the result Context.data.category_properties += `Codes: ${allCategoryCodes} `; }Gettings category options selected by the user
You can retrieve the category options selected by a user. For example, if an employee selects values in a Category field on a page and clicks a button, the selected option names and their codes can be displayed in a String field. To do this, attach one of the following scripts to the button:
async function showSelectedOption(): Promise<void> { // Define a String-type variable to store the result Context.data.selected_option = ``; // Get the name of the selected category option and write it to the String field // If not specified, display \"not set\ Context.data.selected_option += `Selected option: ${Context.data.category?.name ? Context.data.category.name : 'not set'} ` // Get the code of the selected category option and write it to the String field // If not specified, display \"not set\ Context.data.selected_option += `Selected option code: ${Context.data.category?.code ? Context.data.category.code : 'not set'} ` }async function showSelectedOptions(): Promise<void> { // Get an array of selected option names and convert it to a string const allSelectedValues = Context.data.multi_category?.map(v => v.name).join(', '); // Get an array of selected option codes and convert it to a string const allSelectedCodes = Context.data.multi_category?.map(v => v.code).join(', '); // Define a String-type variable to store the result Context.data.selected_options = ``; // Add selected option names to the result Context.data.selected_options += `Selected options: ${allSelectedValues} ` // Add selected option codes to the result Context.data.selected_options += `Selected option codes: ${allSelectedCodes} ` }Creating new category options via script
You can dynamically create category options using a script, for example, when the page is opened. To do this, add a Category variable to the page context without specifying options manually, and configure it as follows:
async function onInit() { // Create the souorce array of options const categoryArray = ['repair', 'inspect', 'replace']; // Convert the string array into an array of category option objects Context.fields.category.data.variants = categoryArray.map((name): TEnum<any> => ({ name: name, code: name, })); }Creating an app item with user-selected category options
You can configure the creation of an app item and save the selected category options into its page using a script. The user selects the desired options on the page and clicks a button. Then, an item is added to the app with the selected values:
async function createAppItem(): Promise<void> { // Create a new app item const app = Context.fields.application.app.create(); // Set the name of the item app.data.__name = `Request`; // Copy the selected category options from the page context into the app itemя app.data.empty_category = Context.data.category; // Save the newly created item await app.save(); }Filtering category options
You can apply filters to category options in a script to display only the relevant ones on a page or form. For example, if the user fills out a String field, some options are excluded from the Category field. To do this, link the following function to the On value change event of the String field:
async function conditionChange(): Promise<void> { // Check if the user has entered a value in the field if (Context.data.condition) { // Filter the category options, removing those with codes 'repair' and 'replace' Context.fields.category.data.variants = Context.fields.category.data.variants.filter(t => t.code !== 'repair' && t.code !== 'replace'); } // If the user hasn't entered a condition, all options remain visible }