preselect select field with freemarker - forms

In my struts2 action that prepares the ftl page i have
private static List<Product> listOfProducts;
with getter and setters. This list is filled with products. First product in the list has type B.
In ftl page I am iterating over the list of products
<#list listOfProducts as product>
<select name = product[0].type>
<option value="A">fistType</option>
<option value="B">secondType</option>
<option value="C">thirdType</option>
</select>
</#list>
Problem is that firstType is preselected each time even if in the list i have a product with type B.
Can you tell me what am i missing here? Why option B was not selected when the ftl is loaded?
Thanks

See http://www.w3schools.com/tags/tag_select.asp on the correct syntax for a select
The attribute "name" sets the name of the control - it does not influence the selection
See How can I set the default value for an HTML <select> element?
on how to do that

Use Struts select tag.
<#s.select theme="simple" name="selectedProduct" list="listOfProducts" listKey="productId" listValue="productName" value="defaultProduct"/>
Please go through the example in below link for more understanding.
http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/

Related

angular 4 - select option change

I need a select option in angular 4, but data binding doesn´t work.
I have a function "ChangeTable" in (ngModelchange) but the parameter always that i send in the function is "undefined".
I have tried to went $event.target.value but it also go "undefined"
this is my code html
<select [(ngModel)]="selectTable" (ngModelChange)="ChangeTable($event)" >
<option>Select item</option>
<option *ngFor="let table of tables" [value]="table.id">
{{table.Description}}
</option>
</select>
this is my code in component
ChangeTable(ev:any){
var tabla=this.selectTabla;
}
You can help me, please.thanks
ngModelChange itself extracts the value from $event so you no more need to do $event.target.value. In your case there must be no id exist in table object that's may be the reason you are getting undefined whenever your binding value property with table's id([value]=table.id).
Check your table's data once and you can also refer this stackblitz link where I've demonstrated your use-case.

Is there a way to create Algolia query strings that facet on page load?

Essentially we have a "dumb" search bar on our website. When the form is submitted, it just sends the user to /search?q={query} where our Algolia instantsearch.js resides.
We would like to add a dropdown to that dumb search bar to filter down to Car, Truck, Boat, or Airplane. That value is in our Algolia index as category and is facted.
Is documentation available on constructing a query string with facets that Algolia instantsearch.js will pick up? Thank-you!
After some more Google-fu I was able to find this issue on Algolia's Github which had exactly what I needed. Here is the code that worked for us:
<form method="GET" action="/search">
<input type="text" name="q" />
<select name="dFR[category][0]">
<option disabled="disabled" selected="selected">All types</option>
<option value="Car">Car</option>
<option value="Truck">Truck</option>
</select>
</form>
If the user doesn't select a type, it will send them to /search?q={query}. If the user does select a type, for example "Car", they will be sent to /search?q={query}&dFR[category][0]=Car (url encoded of course.)
This applies the proper filter, assuming faceting is setup on category and you're using a fairly vanilla implementation of instantsearch.js.

How to have Chosen plugin retain selection option following page reload

I'm using a series of select elements on a form within an MVC5 view, initialised with the jQuery Chosen plugin. The plugin looks as it should and saves the selected option back to the model successfully. However if any other element on the page fails validation and the page is reloaded, the select elements are back to their original state and the selected option is no longer shown. How can I get the select list to retain its selected option following a reload?
<select class="chosen" id="SE_Visit1" name="#Html.NameFor(model => model.SE_Visit1)">
<option value=""></option>
<option value="0">0 (25)</option>
<option value="1">1 (23)</option>
<option value="2">2 (37)</option>
<option value="3">3 (34)</option>
<option value="4">4 (12)</option>
</select>
$(document).ready(function () {
// initialise the Chosen plugin on the select lists
$('.chosen').chosen({ placeholder_text_single: "Select visit...", width: "75px" });
});
I've tried to add a change event handler on the element to save the selected value which could be used to assign the value again after reload but this just seems to break the page. Any help much appreciated since this particular form has 20 select elements so a lot of values for the user to have to complete more than once.

JSP Dropdown from postgresql

I'm very very new to jsp and postgresql and I want to add a dropdown menu to my jsp file.
There is table with Names and Numbers and all I want is getting the names displayed in the drop down
This is what I got so far. An empty dropdown with the right numbers of rows (4 as I have only for rows in my table) is displayed but no names!
<sql:query
dataSource="${db}"
var="result">
select name from customer
</sql:query>
<select>
<c:forEach
var="result"
items="${result.rows}">
<option value=${param.name}>${param.name}
</option>
</c:forEach>
</select>
Thanks for any help in advance!
PS: i tried to find an answer but all the answers were about mysql but we are using psql :/
Your option list has the right number or options but they're empty because the variable param doesn't exist.
You have:
<c:forEach var="result" items="${result.rows}">
<option value=${param.name}>${param.name}</option>
</c:forEach>
var is the variable to be created from items, so you don't want it named the same as the items list. This is where you should be creating the variable param. It should be:
<c:forEach var="param" items="${result.rows}">
<option value='<c:out value="${param.name}"/>'><c:out value="${param.name}"/></option>
</c:forEach>
You should probably also do it like <option value='${param.name}'> since the field name is probably text rather than numeric.

how to use jquery live() to add attribute to element

I have a select element that is in the DOM from page load. Lets say it has an id of my_select like so:
<select name="my_select" id="my_select">
</select>
At some point though when the user does something some values are pulled in via ajax to populate this select element so that it now contains options. So it will now look like this:
<select name="my_select" id="my_select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
So how do I then add an attribute to one of these options?
I want to do this:
$('#my_select option[value="2"]').attr("selected", "selected");
But I think I need to use live() because the options were added via ajax after the DOM had been created.
Thanks
$("body").delegate('#my_select", 'YOUREVENT', function(){
$("#my_select option[value='2']").attr("selected", "selected");
});
Just setting the selected option can be done like this: $('#my_select').val(2); Are you looking for more?
$.live is the same as $.bind, for event binding, the diference is "live" works for elements than not exists yet, and will be created by DOM scripting (i.e. ajax response).
the right form to set val = 2 is $('#my_select').val(2), when it's already in the dom ;)