Is it possible with thymeleaf to retrieve an input "generated" name attribute ?
I need to create a div with an id equals to an input name attribute.
Input names are generated via the th:field.
Markup:
<input type="radio" id="winterTiresInstalled" th:field="*{winterTiresInstalled.value}" />
Generated markup:
<input id="fld_winterTires" type="radio" name="vehicle.winterTiresInstalled.value" />
What I want :
<div th:id="???" />
to obtain :
<div id="vehicle.winterTiresInstalled.value" />
So, how can I retrieve the generated name attribute value "vehicle.winterTiresInstalled.value" with thymleaf ?
I could do it in js but I would prefer doing it in my template.
Thank you.
David
th-attr is best suited for this use case.
<a href="#" th:attr="data-id=${object.getId()}, data-name=${object.getName()}"/>
The above will result is something like this(haven't tested)
<a href="#" data-id="420" data-name="user-link"/>
Did the solution in javascript for now. I change the id of div after the page loads.
I would prefer do it directly in my template but can't figure out a way to do it with thymeleaf.
Related
I need to set custom attribute (data-validation-matches-message) value from messages resources.
<input data-validation-matches-message="Text from messages resources" />
I can receive and print messages resources value as:
<p th:text="#{user.notfound}"></p>
But how I can set this value for a custom attribute (data-validation-matches-message)?
UPD (I use this)
<input th:attr="data-validation-matches-message=#{user.notfound}"/>
Since Thymeleaf 2.1 you can do this:
<a data-th-attr="data-groupid=${somevalue}, data-groupname=${someothervalue}">...</a>
source
Try this:
<input th:attr="data-validation-matches-message='\'' + #{user.notfound}" + '\''"/>
Using 3.0.9.RELEASE:
<td th:text="${item.description}" th:attr="width=${isSplit} ? '44%' : '59%'" />
This will add width="44%" or width="59%according to a boolean set in the variables.
width might as well have been any other custom attribute.
How to get the attribute of Title in the input element
<input type="image" title="Previous Page">
<input type="image" title="First Page">
<input type="image" title="Next Page">
<input type="image" title="Last Page">
What have you tried? Typically something like the following should work:
WebElement element = driver.findElement(By.tagName("input"));
String title = element.getAttribute("title");
The answer provided by Jim Evans is the correct one imo, but for a more specific one i'd advise something like below. Remeber that copy-pasta might not work and you need to change something to be able to work on your full HTML.
List<WebElement> elements = driver.findElements(By.tagName("input"));
for (WebElement element : elements) {
if (element.getAttribute("type").equals("image")) {
System.out.println(element.getAttribute("title"));
}
}
The above code will loop for all the in your webpage that are from type="image" and print on the console the "title" attribute of each one of those.
Still thing you should vote Jim's answer as the correct one though.
First, you need to identify the input element from which you want to get the value of the attribute title .
Then something like the following must work.
element.getAttribute("title");
Its very simple and work for as well.
String title = driver.getTitle();
I have a table, using display tag, in my application, that is using spring web flow. I would like to have a check box in each row, a button that allows me to select/uselect all and a button to execute a function. After clicking the button, the action will perform some database actions and the page should be render, so we can see these changes.
I don´t know which could be the best option, submitting the whole table
<form method="POST" (more params)>
<display:table id="row">
....
</display:table>
</form>
Or only the checkbox column. I this case I wouldn´t know how to implement it.
I have tryed two different approaches:
1. Using a simple input text, checkbox type. This is not possible, because when I submit the form, I need to set a path to another page.jsp (I am working with flows). Besides, I wouldn´t know how to send these values to java backend.
Using spring tags.
In this case, the problem comes whith the class conversationAction
I found some examples, but allways using MVC and controller cases.
How could I implement this issue??
EDIT
I have found a kind of solution, but I faced a new problem...
flow.xml
var name="model1" class="com.project.Model1"/>
var name="model2" class="com.project.Model2"/>
view-state id="overview" model="formAggregation">
...
</view-state>
page.jsp
form:form modelAttribute="formAggregation.model1" id="overviewForm">
...
/form:form>
...
form:form method="POST" modelAttribute="formAggregation.model2">
display:table id="row" name="displayTagValueList" requestURI="overview?_eventId=tableAction">
display:column title="">
form:checkbox path="conversationIds" value="${row.threadId}"/>
/display:column>
/display:table>
input type="submit" name="_eventId_oneFunction" value="Send>>"/>
/form:form>
FormAggregation.java
#Component("formAggregation")
public class FormAggregation {
private Model1 model1;
private Model2 model2;
//Getters and setters
I need this aggregator, because I need both models. I have tested it one by one and it is working as wished. Any idea about that??
Thanks!!
I couldn´t find a solution to add two model in a view-state. So I made a workaround, adding the fields I needed to the model I was using, com.project.Model1. So the result is:
page.jsp
<form:form method="POST" id="tableForm" modelAttribute="model1">
<display:table id="row">
<display:column title="">
<form:checkbox path="chosenIds" value="${row.id}"/>
</display:column>
<display:footer>
<div class="tableFooter" >
<input type="submit" name="_eventId_workIds" value="Send"/>
</div>
</display:footer>
</display:table>
</form:form>
flow.xml
<var name="model1" class="com.project.Model1"/>
...
<transition on="workIds" to="overview" validate="false">
<evaluate expression="actionBean.workIds(model1.chosenIds)" />
</transition>
java class
public void workIds(List<Long> ids) {
Hope it helps
I am using the DateTimeField component of Wicket, but I am struggling to apply any styling to it. The HTML snippet is this:
<span id="creationDate" wicket:id="creationDate" />
and the accompanying Java is:
add(new DateTimeField("creationDate", new PropertyModel<Date>(this, "creationDate")));
Which works, but produces this (which I have tidied up):
<span id="creationDate" wicket:id="creationDate">
<wicket:panel xmlns:wicket="http://wicket.apache.org">
<span style="white-space: nowrap;">
<input type="text" wicket:id="date" size="12" value="28/11/11" name="creationDate:date" id="date1f"/>
<span class="yui-skin-sam">
<span style="display:none;position:absolute;z-index: 99999;" id="date1fDp"></span>
<img style="cursor: pointer; border: none;" id="date1fIcon" src="wicket/resource/org.apache.wicket.extensions.yui.calendar.DatePicker/icon1-ver-1322148000242.gif" alt="" title=""/>
</span>
<input type="text" wicket:id="hours" size="2" value="14" name="creationDate:hours"/>
<span wicket:id="hoursSeparator"> :</span>
<input type="text" wicket:id="minutes" size="2" value="17" name="creationDate:minutes"/>
</span>
What I am hoping to do is get the components separately, then either add style or class attributes to them. Any way this can be done?
You can use the method public Component add(final Behavior... behaviors) defined in the super class Component.
What you are looking for is the behavior AttributeAppender with which you can add CSS id/classes or every other attribute you want to append.
See the API: http://wicket.apache.org/apidocs/1.5/org/apache/wicket/behavior/AttributeAppender.html
UPDATE
I just got a quick look at the source code of the DateTimeField. Unfortunately, you can't access the components separately.
I can think of two ways how you can style the components:
You can put the whole DateTimeField inside a custom div and then, for example, accessing the textfields via cascading css selectors. (e.g. #myDiv input)
or you create your own DateTimeField class with the existing source code and put your css id/classes there.
Turns out you can just override the HTML, in this case by replacing DateTimeField.html in the org.apache.wicket.extensions.yui.calendar package. This then replaces the default HTML, and is where I added the class details.
Another jQuery noob question - what am I doing wrong??
I have some HTML markup rendered by ASP.NET 3.5 webforms which looks like this:
<input id="ctl01_cphContent_pnlBasicInfo_chkRC"
type="checkbox" name="ctl01$cphContent$pnlBasicInfo$chkRC" />
<label for="ctl01_cphContent_cntPromos_pnlBasicInfo_chkRC">Recurrent Charges</label>
<span id="ctl01_cphContent_cntPromos_pnlBasicInfo_lblPromoValidFor"
class="rcPromo">Validity:</span>
<span class="rcPromo">
<input id="ctl01_cphContent_pnlBasicInfo_rbnDiscountValidFor"
type="radio" name="ctl01$cphContent$pnlBasicInfo$discountValidFor"
value="rbnDiscountValidFor" checked="checked" />
<label for="ctl01_cphContent_cntPromos_pnlBasicInfo_rbnDiscountValidFor">valid for</label>
</span>
<span class="rcPromo">
<input id="ctl01_cphContent_pnlBasicInfo_rbnDiscountValidUntil"
type="radio" name="ctl01$cphContent$pnlBasicInfo$discountValidFor"
value="rbnDiscountValidUntil" />
<label for="ctl01_cphContent_cntPromos_pnlBasicInfo_rbnDiscountValidUntil">valid until</label>
</span>
<input name="ctl01$cphContent$pnlBasicInfo$txtDiscountMonths" type="text"
id="ctl01_cphContent_pnlBasicInfo_txtDiscountMonths"
class="textbox" class="rcPromo" originalValue="" style="width:30px;" />
<span id="ctl01_cphContent_cntPromos_pnlBasicInfo_lblMonths" class="rcPromo"></span>
<input name="ctl01$cphContent$pnlBasicInfo$txtDiscountUntil" type="text"
id="ctl01_cphContent_pnlBasicInfo_txtDiscountUntil"
class="textbox" class="rcPromo" originalValue="" style="width:150px;" />
I have a checked "chkRC" which I want to trap and use to enable/disable other UI controls
I have a number of labels, input (type=radio) and input (type=text) UI controls. These are all marked with the "rcPromo" dummy CSS class
I have a CSS class called "textbox" for the normal textbox and "textboxDisabled" for the disabled state of the textbox, in an externally referenced CSS file, that work OK (when used in server-side code, that is)
What I'm trying to accomplish in jQuery is this: when the "chkRC" checkbox is disabled, I want to disable all relevant UI controls.
My jQuery looks like this:
$(document).ready(function() {
$("#<%= chkRC.ClientID %>").click(function() {
$('.rcPromo > :label').toggleClass('dimmed');
if (this.checked) {
$('.rcPromo').removeAttr('disabled');
$('.rcPromo .textboxDisabled').addClass('textbox').removeClass('textboxDisabled');
}
else {
$('.rcPromo > :input').removeAttr('checked');
$('.rcPromo .textbox').addClass('textboxDisabled').removeClass('textbox');
$('.rcPromo').attr('disabled', true);
}
});
});
It works fine for the labels and the radiobuttons - but I just can't get it to work with the textboxes - they just stay the same all around, nothing changes (they don't get disabled and they don't change their appearance to indicate that they're disabled, either).
I don't understand this - I do see several (a few more than in the sample) textboxes, which are <input type="text"> in HTML, and they do have the class="rcPromo" and class="textbox" on them - so why doesn't jQuery find and update those?
Any ideas?
Marc
I can't think of a way to augment the css class names that are assigned to controls from the skin file (phoenix is correct, the class names need to be added in the same attribute).
I can think of a few workarounds though:
--> You can wrap all the textboxes you want disabled in a div with a given class:
<div class="disable_textbox"><asp:textbox id="".../></div>
and then disable them by selecting:
$('.disable_textbox input').attr('disabled', true);
--> You can include character strings in the ID of the textboxes you want disabled:
<asp:textbox id="txtDiscountUntil_DisableMe" ... />
and then disable them like so:
$("input[id*='DisableMe']").attr('disabled', true);
--> You can add a custom attribute to your textbox:
txtDiscountUntil.Attributes.Add("disableme", "true");
and then disable them like so:
$("input[disableme='true']").attr('disabled', true);
Your HTML markup is not the correct one.
You can't add two classes like the one in your code.
Two classes can be added like this
<input type="text" class="Class1 Class2" />
and not like
<input type="text" class="Class1" class="Class2" />
Why don't you use hasClass to check whether the element has this class set or not?
I think you have to give this in an OR condition for the two classes.