Using custom data attribute in Struts 2 s:select - struts2

I'm trying to use custom data attributes of HTML in Struts2 tags
here is my sample code
<s:select list="myList" listKey="myListVal" listValue="myListDesc" data-inputs="myListInput" ></s:select>
i was expecting something like this for example
<select >
<option value="myListVal1" data-inputs="myListInput1">myListDesc1</option>
<option value="myListVal2" data-inputs="myListInput2">myListDesc2</option>
<option value="myListVal3" data-inputs="myListInput3">myListDesc3</option>
</select>
instead I'm getting this
<select data-inputs="myListInput" >
<option value="myListVal1" >myListDesc1</option>
<option value="myListVal2" >myListDesc2</option>
<option value="myListVal3" >myListDesc3</option>
</select>
Is it possible to describe data-attribute in struts select tags for Options inside it.

Override the <s:select> tag template. Or just use HTML tags with <s:iterator>
<select name="list">
<s:iterator value="myList" status="stat">
<option value="<s:property value="myListVal"/>" data-inputs="myListInput<s:property value="#stat.index"/>"><s:property value="myListDesc"/></option>
</s:iterator>
</select>

You can't inject custom attributes into a Struts2 UI Tag directly.
According to Dave Newton's comment, you can with Struts2 >= 2.1.x
But still it's not possible to apply them to the option elements instead of the select, so I'll leave the answer in case you need to extend the original select tag to define a custom behaviour (like apply certain attributes to the options).
You can extend the <s:select> Struts2 tag to allow it to manage new kind of attributes...: http://bodez.wordpress.com/2009/03/13/customising-struts2-jsp-tags/
,
or create your own tag directly, but in your case would be overkill: http://joshuajava.wordpress.com/2008/12/27/creating-custom-components-with-struts-2/).
Last but not least, you could even add your custom attributes once the page is rendered, using something like jQuery (demo: http://jsfiddle.net/CLNDs/ ); they will be accessible, but not visible in source.

Related

Select with dynamic list of options with TeaVM Flavour

I am trying to set up a select dropdown menu, using TeaVM Flavour HTML templates.
While the doc mentions how to do it with a static list of options, it doesn't show how to handle a dynamic list of options.
The trick is to use html:value, which applies both to <input> tags as well as <option> tags:
<select name="mySelect" html:bidir-value="myResult">
<std:foreach var="myOption" in="myList">
<option html:value="myOption">
<html:text value="myOption"/>
</option>
</std:foreach>
</select>

Struts2 use <s:select> with <s:iterator>

Is it possible to define a Struts Select field, <s:select>, with an Iterator for options, <s:iterator>?
e.g., I don't want to use the Key/Value/List properties,
<s:select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList"
list="requestStatuses"
listValue="description" listKey="id" />
because I have some special symbols such as coming from the server side and they aren't escaped.
The following works regarding escaping , but it's an HTML Select. I don't want to use this either, because it doesn't populate the form values properly on load.
<select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList" class="requestor input-block-level" name="requestors" multiple="multiple">
<s:iterator value="requestStatuses">
<option value="${id}">${description}</option>
</s:iterator>
</select>
My goal is,
<s:select ..>
<s:iterator>
You can set the value on the POHTML select using the normal value attribute.
The <s:select> tag expects values to be ready-to-use; personally I'd transform them on the server side before exposing them to the view layer.
You might be able to use OGNL in the listTitle property (I might have the property name wrong) to un-entity it; I don't recall.

ASP.NET MVC combo dropdown box

Is it possible to create a dropdown box in ASP.NET MVC, that has a checkbox alongside each item in the dropdown list?
I know it sounds simple, in webforms or using telerik this would be pretty simple, but I can't figure how I can implement the same thing in basic HTML.
Thanks
You can use a jQuery plugin called DropDownCheckList to get the wanted effect of a dropdownlist with multiselect options.
Its pretty easy, all you need to do is to create a html listbox and call the jQuery plugion to extend the listbox.
<script type="text/javascript">
$(document).ready(function() {
$("#listbox").dropdownchecklist();
}
</script>
<select id="listbox" multiple="multiple">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
http://dropdown-check-list.googlecode.com/svn/trunk/doc/dropdownchecklist.html
a dropdown with each element of the dropdown having a checkbox? that's not a standard element of HTML, why would you even need that? a multiple option is what you need:
http://www.htmlcodetutorial.com/forms/_SELECT_MULTIPLE.html (first one)

Read only drop down

Is there a good way to make an HTML dropdown read only. Using disabled attribute of select seems to work, but the value is not posted.
<select disabled="disabled">
I have a complex page with lots of javascript and ajax. Some actions in the form cause to drop down to be read only some actions let user decide the value.
Edit: Is there a better way other than using a hidden input?
If you do not want user to pick an option, how is this different from read-only input type="text"?
<select name="selectbox" disabled="disabled">
<option>option 1</option>
<option selected="selected">option 2</option>
<option>option 3</option>
</select>
Correct me if I am wrong, but if an option is selected, the value is sent

Freemarker hash for Struts2 #s.select tag's list property

I'm using Freemarker as the templating engine for a Struts 2 application and having some problems trying to pass a Freemarker hash to the #s.select tag's list value.
Currently I'm trying something like this in my template:
<#s.select name="myDropdown" list={"1":"Foo", "2":"Bar", 3:"Baz"} />
The resulting HTML that's rendered is this:
<select name="myDropdown" id="myDropdown">
<option value="freemarker.ext.beans.HashAdapter$1$1$1#2c9bebb">freemarker.ext.beans.HashAdapter$1$1$1#2c9bebb</option>
<option value="freemarker.ext.beans.HashAdapter$1$1$1#16ca4a">freemarker.ext.beans.HashAdapter$1$1$1#16ca4a</option>
<option value="freemarker.ext.beans.HashAdapter$1$1$1#173ee8">freemarker.ext.beans.HashAdapter$1$1$1#173ee8</option>
</select>
Based on the documentation it seems like this should work, but really the only examples are of using Freemarker lists. Hashes are only mentioned as another option, but I haven't been able to find any code examples that use them.
Ultimately my question is, what Freemarker syntax should I use with the Struts 2 select tag in order to render the following HTML?
<select name="myDropdown" id="myDropdown">
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="3">Baz</option>
</select>
Using the listKey and listValue properties of the select tag seems to do the trick.
The working code is now:
<#s.select name="myDropdown" list={"1":"Foo", "2":"Bar", 3:"Baz"} listKey="key" listValue="value" />
Seems like that should be taken care of automatically by the tag, but I was not able to get it to work without explicitly setting those two additional properties.

Resources