Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I create a custom field, that reveals a 2nd field if the 1st field is a certain value?

Vincent Paladino
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 3, 2020

Hello!

 

So what i'm currently working on is creating a dropdown search field that allows you to pick from a list of companies, then if the company you want it not listed, you can select 'new company' and then a text box will appear where you can fill in the new company. 

 

So far i've for a simple dropdown menu, and I'm trying to figure out how can i implement that new 2nd field to accompany the first. 

Here's my dropdown field as it is now:

```java

@Scanned
public class ClientField extends SelectCFType {


private final OptionsManager optionsManager;

protected ClientField(@JiraImport CustomFieldValuePersister customFieldValuePersister,
@JiraImport OptionsManager optionsManager,
@JiraImport GenericConfigManager genericConfigManager,
@JiraImport JiraBaseUrls jiraBaseUrls) {

super(customFieldValuePersister, optionsManager, genericConfigManager, jiraBaseUrls);
this.optionsManager = optionsManager;
}

@Override
public Map<String, Object> getVelocityParameters(final Issue issue,
final CustomField field,
final FieldLayoutItem fieldLayoutItem) {

final Map<String, Object> parameters = super.getVelocityParameters(issue, field, fieldLayoutItem);//
FieldConfig fieldConfiguration = null;
if(issue == null)
{
fieldConfiguration = field.getReleventConfig(new SearchContextImpl());
} else
{
fieldConfiguration = field.getRelevantConfig(issue);
}
Options options = this.optionsManager.getOptions(fieldConfiguration);
this.optionsManager.createOption(fieldConfiguration, null, new Long(1), "New Client");
// TODO: Populate other options with 'Company names' from DB
return parameters;
}
}

```

Here is my edit.vm file so far. Basically just populating the dropdown with the options.

Also it checks if the dropdown equals 'New Client' and includes an additional `<input>` tag. This part does not work 

 

edit.vm:

```vm

#customControlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters $auiparams)

<select class="select" name="$customField.id" id="$customField.id">
#if (!$fieldLayoutItem || $fieldLayoutItem.required == false)
<option value="-1">$i18n.getText("common.words.none")</option>
#else
<option value="">$i18n.getText("common.words.none")</option>
#end
#foreach ($option in $configs.options)
#if(!$option.disabled || $option.disabled == false || ($value && $value == $option.optionId.toString()))
<option#if ($value && $value == $option.optionId.toString()) selected="selected"#end value="$option.optionId">$cfValueEncoder.encodeForHtml($option.value)</option>
#end
#end
</select>

#if ($value.equals("New Client"))
<input type="text"
name="$customField.id"
value="$!value" />
#end

#customControlFooter ($action $customField.id $fieldLayoutItem.fieldDescription $displayParameters $auiparams)

```

For readaibility, i put these snippets in pastebin as well:

edit.vm

ClientField.java

 

Side question, is there a different field besides `SelectCFType` that lets the user search all the options? 

 

Any suggestions with how to approach this problem would be really appreciated. Thanks for your help!

0 answers

Suggest an answer

Log in or Sign up to answer