how to pass multiple attributes to the option in Thymelaef - spring-mvc

I'm trying to using thymeleaf as the view template and i come across one problem. the code I have now is as below.
<select>
<option th:each="res: ${result1}"
th:value="${result1.NAME}"
th:text="${result1.NAME}"></option>
</select>
What I need is like this, where I dont know how to add another attribute for result2, both result1 and result2 are the list object in the controller
<select>
<option th:each="res: ${result1}"
th:value="${res.NAME}"
th:text="${res.NAME+result2.SCHOOL}"></option>
</select>

You can create a simple object with two fields for result1 and result2, if you know their relationship in the controller. Then you can use a list of that objects to iterate in html page.
<select>
<option th:each="item: ${itemList}"
th:value="${item.result1.NAME}"
th:text="${item.result1.NAME + item.result2.SCHOOL}"></option>
</select>

Related

How to get list all items from list

I have a webpage where this code is:
<select tabindex="8" id="custom_field_4" name="custom_field_4[]" size="4" multiple="multiple" xpath="1">
<option value="red">red</option>
<option value="yellow">yellow</option>
<option value="green" selected="selected"> green</option>
<option value="flashing" selected="selected"> flashing</option>
</select>
I need to get all the option values.
I tried
Get List Items //select[#name="custom_field_4[]"] but I get the error: List with locator \'//select[#name="custom_field_4[]/option"]\' not found.'
If you check the source code (line 333) of the Selenium library, you can see that the Get List Items keyword use list as tag name whereas in your DOM it is select. It won't match.
def _get_select_list(self, locator):
el = self.find_element(locator, tag='list')
return Select(el)
You can get all list elements using the Get WebElements keyword instead like:
${list elements}= Get WebElements //select[#id='custom_field_4']/option
You can access the value attribute of each element in the returned list.

Ractive value not being set when select list is first loaded

I am using Ractive and have two select lists. The first select list lets the user select their country. If they select "US", Ractive will check for it and display a list of states for the user to select their state. For example:
<select value="{{country}}">
<option value="CA">Canada</option>
<option value="US">USA</option>
</select>
{{#if country == 'US'}}
<select value="{{chosen_state}}">
{{#states}}
<option value="{{this.state_name}}">{{this.state_name}}</option>
{{/}}
</select>
{{/if}}
When this form first loads "Canada" is selected by default. Once the user selects "US", the states list shows up just fine. However, if I try to check the value of "chosen_state", it is set to empty rather than the first state in the states list. After I manually change the state, then and only then does the "chosen_state" value show as a state value.
Do I have to wait for the change event to be triggered by a manual event, or is there some way to make it so that as soon as the states list is rendered, it will update it's value automatically based on the first state displayed? It would be nice to not have to trigger a change event for every select list when it's rendered.
It turns out that this requires a dirty check using updateModel. I made it so if the select field is changed, it triggers an event that calls updateModel on the instance to dirty check the chosen_state field.
Updated selected code:
<select value="{{country}}" on-change="countrychange">
<option value="CA">Canada</option>
<option value="US">USA</option>
</select>
{{#if country == 'US'}}
<select value="{{chosen_state}}">
{{#states}}
<option value="{{this.state_name}}">{{this.state_name}}</option>
{{/}}
</select>
{{/if}}
And in ractive:
ractive.on('countrychange', function(event) {
this.updateModel();
});
Alternate solutions might include observing the model for updates to the country then running this.updateModel() as well as just running updateModel on the required keypath which is more efficient.
Further documentation on this can be found at:
http://docs.ractivejs.org/latest/ractive-updatemodel

Symfony crawler select tag attributes from dropdown

I need to target "selected" attribute in drop down menu so I can compare if item marked as selected is the item what I need to have selected.
Can you help me with methods, what I can use for this ?
<option value="Title" selected>Title</option>
<option value="First Name">First Name</option>
<option value="Middle Name">Middle Name</option>
Thank you :)
I've found an answer finally -
$crawler->filter('option[selected]')->attr('value');
works for me :)
Maybe something like that (not tested)
$target = $crawler->filterXPath('//.../option/#selected')->text();
or
$target = $crawler->filterXPath('//.../option')->attr('selected')->text();

How to add html document using crawler symfony2?

<select id="dealer_information_city" name="dealer_information[city]" required="required"><option value="" disabled="disabled">Choose a city.</option>
<option value="23">California</option>
<option value="114" selected="selected">Los Angeles</option>
</select>
Here's my code i want to add a new option using crawler symfony but nothing happen anyone can help me thanks!
var_dump($crawler->filter('#used_car_step1_car_model')->addHtmlDocument('<option value="1">New Jersey</option>'));

asp.net mvc3/4 request.form to return the selected value and the selected text from selectlist

How would I get in my controller, the text part of Request.Form["StandID"]
<div class="editor-field">
<select id="StandID" name="StandID">
<option value=""/>
<option value="3">Mark</option>
<option value="5" Selected>Brian</option>
<option value="6">Ian</option>
<option value="7">Vin</option>
</select>
</div>
So in my controller, Request.Form["StandID"] = 5
Is it possible to retrieve the text (I know this isn't posted as part of the form - is there a way to do that)?
So I could like to return "Brian" - as well as the ID of 5?
Thank you,
Mark
No. HTML will only post the value to the controller. However, you could make your value be something like "5 - Brian" and then have your text be "Brian". Then you would have to parse the value to get the real value from it.
as an alternative you could use hidden input field to store your text value. The downside is you have to place your value into this hidden field with javascript (on selected change or on form submit), but pros is you will have separated field for value, so you don't have to parse it on server side and you can avoid possible problems with naming.

Resources