How to set default value in Select dropdown in velocity template file? - java

I am trying to build a custom build Jira Plugin, on the UI of plugin I am displaying select dropdown which consist of list of Issue Types.
I need to make option value "ALL" as default one for that select dropdown. I have written following code for that.
<label for="issue-types" >Issue Type(s):</label>
<select onchange="disableIfNoIssueTypeSelectedMandate()" class="multi-select" multiple="multiple" id="issue-types-mandate" name="issueTypes[]">
#foreach ($issueType in $allIssueTypesInProject)
<option value="$issueType.get("id")">$issueType.get("name")</option>
#end
</select>
</div>```
Can anyone please help me to know how to set default value in this select dropdown?

probably you should use <option selected="selected" value="$issueType.get("id")">$issueType.get("name") but with the default string values that you need

Related

Selenium is not selecting the correct value

Im using selenium to test a web page which has a select option and one input type text.
Manually and using the selenium IDE, it works correctly, but when I export the test case to Java Junit, i can see the dropdown click, but selenium is not selecting the value, it is just expanding the dropdown.
What can i do?
Lets check my code:
Select dropdown = new Select(driver.findElement(By.id("type")));
dropdown.selectByVisibleText("Celcius");
Consider my form, like:
<form action="doit">
<select name="type" id = "type">
<option value = "fail"> Fail </option>
<option value = "celcius"> Celcius </option>
</select>
<input type="number" name="num" id="num">
</form>
Sometimes this method wont work. This may happen if the text between the option tags have spaces before and after the tags.
Eg.
1.<option> Fail </option>
2.<option>Fail</option>
In the above example 1 and 2 are different.
So you can use like,
driver.findElement(By.id("type")).click(); // this will expand the list
driver.findElement(By.xpath("//select[#id='type']/option[contains(text(),'Celcius')]")).click();
Also try to click directly. It work if the element is vivible,
driver.findElement(By.xpath("//select[#id='type']/option[contains(text(),'Celcius')]")).click();
driver.findElement(By.id("type")).sendkeys("Celcius");

how can i make field dependent to another field in jsp?

<td>Branch</td>
<td>
<select name="branch" type="text">
<option value="0">Select Branch</option>
<option value="CE">CE</option>
<option value="IT">IT</option>
</td>
<td>Batch</td>
i.e if i have branch field in my jsp form
so my branch firld contains 2 inputs as 1.CE & 2.IT.
So when i click on CE ,so it have to show me A1,A2,....A5 & when i click on IT, it will have to show B1...B5 from which we can select any one of the field.
So, pls help me for above program.
Here is my code.
Pls help me what code should i write in "Batch" field""
you have to register onChange() event of the branch and call an javascript function which will populate the batch field accordingly.

Spring drop down not working

i am using spring mvc,
<form:select class="form-control" id ="cmsphyexamtesttype_cmsPhysicalExamCategory_id" path="cmsPhysicalExamCategory.id">
<form:option value="0" label="--- Please select the Category ---"/>
<form:options items="${cmsphyexamtestcategorys}" itemLabel="name" itemValue="id" />
</form:select>
html code
<select id="cmsphyexamtesttype_cmsPhysicalExamCategory_id" name="cmsPhysicalExamCategory.id" class="form-control">
<option value="0">--- Please select the Category ---</option>
<option value="2">Genaral</option><option value="3">EYE</option><option value="4">HENT</option><option value="5">CHEST</option>
</select>
this one is working fine with new form , but in edit mode,it is not working do you have any idea it gives selected value while rendered to editing mode, i am new to spring mvc, is there any thing wrong with this code?
You are path binding the drop down value to the
cmsPhysicalExamCategory.id
So, the selected value will be retained in the form when you recall the form again for some other operation like 'edit.
You can change the value in the drop down and the new value will be path binded to the model. Here, its not getting binded. It may be because of the error in path bind/form submit.
Please post the form submit code/controller code for more help.
From your question what I get is that you have drop down and on submitting again , you want to render the same form, you are getting the change value but drop down value still persist to the default , first attribute of drop down, and you want to have the attribute same as value, in case this is the elaborated issue, you need to explicitly check in list for what value you want to select and set the attribute as Selected.

how to get options under a particular optgroup using selenium web driver for java?

I want to select options under particular optgroup from a multiselect picklist field, for example
<select id="xyz">
<optgroup label="Group1">
<option value="1">pick1</option>
<option value="2">pick2</option>
</optgroup>
<optgroup label="Group2">
<option value="3">pick3</option>
<option value="4">pick4</option>
</optgroup>
</select>
if the picklist field is like above, I can use
new Select(driver.findElement(By.id("xyz"))).selectByVisibleText("pick1");
new Select(driver.findElement(By.id("xyz"))).selectByVisibleText("pick2");
for selecting all the options which is under optgroup -Group1 from the above.
Here I know which are all the options present in the Group1 and hence I can select like above.
But my requirement is dynamically I need to select all the options under optgroup-Group1.
If I want to select all the options both including Group 1 & 2 dynamically
Then I can use
new Select(driver.findElement(By.id("xyz"))).getOptions();
to get all options as a List and I can store it as an array, and I can select all the Options one by one using array
But how can I get all the options under only one specific optgroup say Group1 or Group2?
You should use xpath and findElements:
driver.findElements(By.xpath("xpath = "//select[#id='xyz']/optgroup[#label='Group1']/option")))

Spring select tag generating hidden field

I am not very familiar with the Spring tag and seems like i am struck in some issue which i am not able to understand as of now.
I have displaying two select tags in my jsp and they are backed by an Arraylist and map here is the code for them
<form:select path="prsBTOData[${status.index}].colors" items="${prsBTOData.colors}"
cssClass="productDetailsSelect"/>
and
<form:select path="prsBTOData[${status.index}].fonts" items="${prsBTOData.fonts}"
cssClass="productDetailsSelect" >
colors is being backed by Array list while the fonts is being backed by the Map.below is the generated HTML
<select multiple="multiple" class="productDetailsSelect" name="prsBTOData[0].colors"
id="prsBTOData0.colors">
<option selected="selected" value="Red">Red</option>
<option selected="selected" value="Green">Green</option>
<option selected="selected" value="Black">Black</option>
</select>
<input type="hidden" value="1" name="_prsBTOData[0].colors">
i am not sure why its doing multiple="multiple" and not showing any drop down but only showing the RED as selected value, while i was expecting a list with drop down options.
even not sure why this hidden field is getting generated and what is its purpose?
In form:select the items attribute is the list of items that needs to be displayed in select box. And path attribute is the property that is bound with the selected value.
As you have given an arraylist (having multiple values) as the path, spring assumed you wanted a multiple value selected drop down.
You might want to give like this (assuming you have color property for prsBTOData )
<form:select path="prsBTOData.color" items="${prsBTOData.colors}"/>
And consider using separate model objects for maintaining static/reference data (colors,fonts) as below:
<form:select path="prsBTOData.color" items="${referenceData.colors}"/>

Categories