Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
5

create fields based on dropdown

Community Beginner ,
Sep 02, 2023 Sep 02, 2023

How to create text fields from a dropdown. So if there are 4 options, 4 text fields are created. 

 

var newTextField = this.addField("MyNewTextField", "text", 0, [201, 311, 300, 215]);

 
ChatGTP code that did not work 

var dropdownField = this.getField("DropdownFieldName"); // Replace with the actual name of your dropdown field
var coordinates = [149, 530, 248, 626]; // Specify the coordinates for field positioning

// Iterate through the options in the dropdown
for (var i = 0; i < dropdownField.numItems; i++) {
    var optionText = dropdownField.getNthItemAt(i);
    var fieldName = "FieldFor_" + optionText.replace(/[^\w]/g, "_"); // Create a valid field name based on the option text

    // Create a new field for each option using the generated field name and specified coordinates
    var newX = coordinates[0]; // Left edge
    var newY = coordinates[1] + (i * (coordinates[3] - coordinates[1])); // Adjust Y-coordinate based on spacing
    var newWidth = coordinates[2] - coordinates[0]; // Width
    var newHeight = coordinates[3] - coordinates[1]; // Height

    var newField = this.addField(fieldName, "text", 0, [newX, newY, newX + newWidth, newY + newHeight]);
    newField.value = "This is a new field for " + optionText; // Set an initial value if desired
}

 

TOPICS
How to , JavaScript , PDF , PDF forms
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Beginner ,
Sep 03, 2023 Sep 03, 2023
LATEST

Yes i figured it out with multifield 

var multilineTextField = this.getField("multilinetext");

// Define coordinates and dimensions
var left = 147;
var bottom = 636;
var right = 250;
var top = 660.96;
var fieldWidth = right - left;
var fieldHeight = top - bottom;

// Get the text from the multiline text field as a string
var textValue = multilineTextField.valueAsString;

// Split the text into an array of lines
var lines = textValue.split(/\r\n|\r|\n/);

// Create an array to store the fields
var createdFields = [];

// Loop through each line and create a new dropdown field for each with fixed coordinates
for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    var fieldName = "master.furnacemodel.dropdown." + line.replace(/[^\w]/g, "_"); // Create a valid field name based on the line

    // Check if a field with the same name already exists
    if (!this.getField(fieldName)) {
        // Create a new dropdown field for each line using the generated field name and fixed coordinates
        var newField = this.addField(fieldName, "combobox", 0, [
            left, // Left
            bottom, // Bottom
            left + fieldWidth, // Right
            top // Top
        ]); // Adjust the size as needed
        newField.setItems([""]); // Add a blank option

        // Add the newly created field to the array
        createdFields.push(newField);
    }
}

// Now all dropdown fields have been created, and you can access them using the "createdFields" array

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 02, 2023 Sep 02, 2023

Would be cool to only show the selected text field based on selection, maybe match the field name to the dropdown option. Thanks

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 02, 2023 Sep 02, 2023

I was able to do this with a multiline text field

// Replace "multilinetext" with the actual name of your multiline text field
var multilineTextField = this.getField("multilinetext");
var fixedCoordinates = [149, 530]; // Fixed X and Y coordinates

// Get the text from the multiline text field as a string
var textValue = multilineTextField.valueAsString;

// Split the text into an array of lines
var lines = textValue.split(/\r\n|\r|\n/);

// Loop through each line and create a new text field for each with fixed coordinates
for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    var fieldName = "FieldFor_" + line.replace(/[^\w]/g, "_"); // Create a valid field name based on the line

    // Create a new text field for each line using the generated field name and fixed coordinates
    this.addField(fieldName, "text", 0, [fixedCoordinates[0], fixedCoordinates[1], fixedCoordinates[0] + 99, fixedCoordinates[1] + 96]); // Adjust the size as needed
    this.getField(fieldName).value = line; // Set the value of the new field to the line text
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 02, 2023 Sep 02, 2023

Why are you creating new fields?

Just create them manually and hide them, then show them with a script depending on dropdown choice.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 02, 2023 Sep 02, 2023

Yes, I already had this setup but I have 100 hundreds of fields that need to have a master set of data. It's ok I'm slowly figuring it out. I tried to set up an excel sheet but it only uploads fields one by one. Would be best to tied an excel sheet to dropdown fields, text fields, and images. But I don't think that is possible. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 02, 2023 Sep 02, 2023

This is not a good approach. Each time the user will select an item in the drop-down, new fields will be created, including duplicates. At the very least you need to add a check to see if those fields already exist, before adding new ones.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 03, 2023 Sep 03, 2023
LATEST

Yes i figured it out with multifield 

var multilineTextField = this.getField("multilinetext");

// Define coordinates and dimensions
var left = 147;
var bottom = 636;
var right = 250;
var top = 660.96;
var fieldWidth = right - left;
var fieldHeight = top - bottom;

// Get the text from the multiline text field as a string
var textValue = multilineTextField.valueAsString;

// Split the text into an array of lines
var lines = textValue.split(/\r\n|\r|\n/);

// Create an array to store the fields
var createdFields = [];

// Loop through each line and create a new dropdown field for each with fixed coordinates
for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    var fieldName = "master.furnacemodel.dropdown." + line.replace(/[^\w]/g, "_"); // Create a valid field name based on the line

    // Check if a field with the same name already exists
    if (!this.getField(fieldName)) {
        // Create a new dropdown field for each line using the generated field name and fixed coordinates
        var newField = this.addField(fieldName, "combobox", 0, [
            left, // Left
            bottom, // Bottom
            left + fieldWidth, // Right
            top // Top
        ]); // Adjust the size as needed
        newField.setItems([""]); // Add a blank option

        // Add the newly created field to the array
        createdFields.push(newField);
    }
}

// Now all dropdown fields have been created, and you can access them using the "createdFields" array
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 03, 2023 Sep 03, 2023

I'm also interested in a text field being created after selecting a value from a dropdown. Where are you putting this code? Wanted to know if you were putting it under a specific property on the dropdown itself, or somewhere else. Or do you have a video walkthrough of the code you put and what it does when working properly?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines