I´m using a node script that reads a directoty content and stores the values in a variable.
I want to make this result apear is a dropdown menu in the switch script so I can select one of the values.
I see that have this option on Switch Scripter but I have to enter manually the values.
Is it possible to populate this dropdown menu with the results from the variable ?
Populate dropdown list in SwitchScripter
-
- Member
- Posts: 24
- Joined: Wed Sep 08, 2021 4:36 pm
Re: Populate dropdown list in SwitchScripter
You have the property "Select from library". Then you get a list of values that you can populate from different sources.
You get the values from this Entry point in the script
getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]>
You get the values from this Entry point in the script
getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]>
-
- Member
- Posts: 24
- Joined: Wed Sep 08, 2021 4:36 pm
Re: Populate dropdown list in SwitchScripter
Thank you jan !
I´ll give a try !
I´ll give a try !
-
- Member
- Posts: 24
- Joined: Wed Sep 08, 2021 4:36 pm
Re: Populate dropdown list in SwitchScripter
Hi jan !jan_suhr wrote: ↑Fri Feb 10, 2023 7:41 pm You have the property "Select from library". Then you get a list of values that you can populate from different sources.
You get the values from this Entry point in the script
getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]>
Sorry to bother you!
I'm trying to make getLibraryForProperty work,
but I really couldn't understand how to do it.
Below is the example I intend to use.
I have a directory that I need to be the "array" to populate the Library.
Can you give me a tip to solve this?
Thank you very much in advance!
Code: Select all
//requirements
const fs = require("fs");
// replace with the path to your directory
const directoryPath = 'D:\\node_js\\dirlist\\sampleDoc';
//switch
async function jobArrived(s, flowElement, job) {
//log directoryPath
await job.log(LogLevel.Warning, "directoryPath " + directoryPath);
async function getLibraryForProperty(s, flowElement, tag) { //directory list array
fs.readdir(directoryPath, (err, tag) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
// Create a JSON array from the file names
const jsonArray = JSON.stringify(tag);
});
//log array
await job.log(LogLevel.Warning, "directoryPath " + jsonArray);
}
//remove input job
await job.sendToSingle();
}
Re: Populate dropdown list in SwitchScripter
This is an example of how you'd implement the getLibraryForProperty entry point:
Code: Select all
import * as fse from "fs-extra";
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
await job.sendToSingle();
}
async function getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]> {
let props: string[] = [];
if (tag === "directoryPath") {
// replace with the path to your directory
const directoryPath = 'D:\\node_js\\dirlist\\sampleDoc';
//log directoryPath
await flowElement.log(LogLevel.Warning, "directoryPath " + directoryPath);
props = await fse.readdir(directoryPath);
}
return props;
}
-
- Member
- Posts: 38
- Joined: Tue Jun 01, 2021 11:57 am
Re: Populate dropdown list in SwitchScripter
Code: Select all
async function getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]> {
let library = [];
switch (tag) {
case "case1":
library = []; //Some code here;
break;
default:
library = ["No Value found"];//optional
break;
}
return library;
}
-
- Member
- Posts: 24
- Joined: Wed Sep 08, 2021 4:36 pm
Re: Populate dropdown list in SwitchScripter
This is the code that made it work.
thanks Jan , mkayyyy and tdeschampsBluewest !
thanks Jan , mkayyyy and tdeschampsBluewest !
Code: Select all
async function getLibraryForProperty(s, flowElement, tag) {
let props = [];
if (tag === "Template") {
// replace with the path to your directory
const directoryPath = 'D:\\node_js\\dirlist\\sampleDoc\\';
//creates de file list array
props = fs.readdirSync(directoryPath);
}
return props;
}