Integrate jquery mobile custom select menu with JSF - java

I'm trying to convert our existing JSF application for mobile devices by integrating jquery mobile. Specifically, I would like to use the custom select menus so it pops up instead of a dropdown as seen here: http://jquerymobile.com/demos/1.0a4.1/docs/forms/forms-selects.html. JSF offers SelectOneMenu in order to retrieve data dynamically from the backing bean, but this clearly is not the desired effect. Is there a way to populate jquery mobile select dynamically from backing bean? Below is the code without jsf that I want to replace the options with values from backing bean.
Thanks!
<select name="select1" id="select1" data-native-menu="false" data-theme="l">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>
</select>

IMHO You should upgrade jQM to version 1.0 and jQuery 1.6.4
Thinking out loud you could try something like this.
Keep the select menu as is (How your application renders the HTML)
You can add the jQM Markup dynamically with something like this:
//refresh and force rebuild
$('select').selectmenu('refresh', true);
Docs:
http://jquerymobile.com/demos/1.0/docs/forms/selects/methods.html

Related

How can you fire off a Java action class from a Velocity Template page using struts?

I have a project where I'm trying to get a selected value from a select form option and get it into a Java action class.
In my .vm file, I have something like this:
<form id="someFormId" name="someFormName" action="someFormAction">
<select id="someSelectId" name="someSelectName" onChange="someAcion">
<option>option1</option>
<option>option2</option>
</select>
</form>
Whenever a new select option gets changed, I want to handle that logic in an Java action class, perhaps using struts. Because my current project is a bit larger, I don't want to use JSP files for struts, I just want to be able to reach that action class from my .vm file. Reading the documentation, I haven't found many solid full examples of how to do my xml configurations to include velocity struts and make that action class fire off. Could someone please provide an example of how exactly to map the select change to an action class in velocity templates?

SpringBoot & Thymeleaf multiple select dropdown not showing selected when editing

My project uses Java 8, Springboot 2 and Thymeleaf 3. I want to add a multiple select drop down to my form. I've tried it a few different ways, like trying to use Select2, and trying to just manually do it with Thymeleaf & Springboot.... I eventually got the select2 to work and make it a drop down view. I can get the functionality of it to work but can't get the selection to show up on the editable view. All options get to the SQL database, and if you only have one choice it will show on the edit page, but if more than one option is selected, it wont show anything on the edit broker page.
If I change to th:name instead of th:field, I can see the selected options, but now I can not save any options as well...
I am not getting any errors so I am not sure why this isn't working. Any idea what I need to do to get the edit view to show all options selected? Is it some sort of binding issue?
<select id="brokerTargetIndustries" th:field="*{brokerTargetIndustries}" multiple="multiple" class="text-left form-control" >
<option value="Social Service WC" th:selected="${broker!=null and broker!=null and broker.targetIndustries!=null and #strings.contains(broker.targetIndustries,'Social Service WC')}">Social Service WC</option>
<option value="Propane Fuel Dealers" selected="${broker!=null and broker.targetIndustries!=null and #strings.contains(broker.targetIndustries,'Propane Fuel Dealers')}">Propane/Fuel Dealers</option>
<option value="Building Services PKG" selected="${broker!=null and broker.targetIndustries!=null and #strings.contains(broker.targetIndustries,'Building Services PKG')}">Building Services PKG</option>
<option value="Contractor WC" selected="${broker!=null and broker.targetIndustries!=null and #strings.contains(broker.targetIndustries,'Contractor WC')}">Contractor WC</option>
</select>
Here is the controller for edit:
#RequestMapping(value="/edit/{id}")
public String editBroker(Model model,#PathVariable("id") Long id, Broker broker){
Broker existing = brokerRepository.findById(id).get();
model.addAttribute("broker",existing);
return "brokerProfile";
}

Geb select element that contains something

How do I select the value of a form select field when it has random numbers in its name?
<select name="select_(random numbers)">
<option value="1"></option>
</select>
I have tried:
formValidity {$("form").(contains('validity')) = "1 year"}
which fails.
There are a few ways to achieve this:
formValidity { $("form select[name*='validity']") }
formValidity.value("1 year")
You were trying to use the Geb way.
Geb Selecting Manual
You can also use CSS and jquery selectors which I used above and also prefer.
CSS Selectors
JQuery Selectors

How to use a modelAttribute value in Jquery for Select box

I have a Map fetched from a database and I want to use the data to populate Select box. The Map is added to the model using sellerCodeList
In Spring it is simple:
<form:select path="orderDetails[0].SellerItemCode" items="${sellerCodeList}">
</form:select>
But I want to use the same in a Jquery function which builds up my select box. (Reason: I have a table with dynamic rows and each row has a select box as one of the element)
When I use the following I start getting error/exception
$("#poFields").append('<tr valign="top"><td>
<form:select path="orderDetails['+rowNum+'].SellerItemCode" items="${sellerCodeList}">
</form:select>');
This throws an exception
I see on SO that there is a way to iterate over the sellerCodeList and feed to options as per the following Dropdown link from SO
I am not good with JSON stuff though ;-)
Can someone please help
You'll need to "print" your items into JS so that your JS code has access to them. Something like this:
<script>
var sellerCodes = {};
<c:forEach items="${sellerCodeList}" var="entry">
sellerCodes['${entry.key}'] = '${entry.value}';
</c:forEach>
// you can now use sellerCodes in your JS code as a map.
</script>

Selenium select dropdown option

I'm going to say the dreaded words - i'm fairly new to java
But I can't find my answer online in the most obvious places so I'm going to ask the question here
The program i'm testing is a customer database, when you select the Country drop down box, other fields may become mandatory
Amongst those fields is "State"
This field can either be a free text or a drop down box
So i've created an if statement that lets me input free text if the conditions for a state drop down box is not met:
if (selenium.isElementPresent("xpath=//*[#id='state']/option[2]"))
{selenium.select("xpath=//select[#id='state']/option","index=2" );}
else {checkfield("xpath=//td[contains(.,'State/Province:')]/
preceding-sibling::td[contains(.,'*')]",
"xpath=//*[#id='address.state']",
state1);}
<td>
<!-- begin state drop down menu -->
<select id="state" name="address.state">
<option selected="selected" value="">Please Select</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="WY">Wyoming</option>
</select>
<!-- end state drop down menu -->
</td>
My question may be a very basic one and you guys can probably already see where my code fails
I want to choose option 2 of the drop down box if a drop down box is present, otherwise enter a string which has been declared in the free text box
up until now i've only ever used selenium.select for selecting a label which has specific text
Firstly: Upgrade to Selenium WebDriver
Selenium 1.0 is just an undead.
That's the way to do it with WebDriver:
if(stateDropdown.isEnabled()&&(!stateField.isEnabled())){
Select state = new Select(stateDropdown);
// state.selectByValue("Illinois");
state.selectByIndex(2);
}else if(stateField.isEnabled()&&(!stateDropdown.isEnabled){
stateField.sendKeys("Salzburg");
}
Isn't it pretty?
Could you try doing it using labels instead?
selenium.select("state", "label=Alaska")
You're likely to know what the values will be unless new states get added (seems unlikely)

Categories