Put linebreak/newline in tooltip - jsf

I'd like to put a linebreak in a tooltip so that it displays like:
Nom: value1
Quality: value2
I tried:
1.
<h:outputText title="Nom: #{cntc.value1}<br/>Quality: #{cntc.value1}" />
2.
<h:outputText title="Nom: #{ (''.concat(cntc.value1).concat('<br/>')concat('Quality: ').concat(cntc.value2)}" />
None of them worked for me. It seems that the <br/> isn't being interpreted.
How can I achieve this?

It's not a good idea to name the problem "Concatenating strings in EL" if your issue is with neither of those things. You want to create a multi-line title, that's an HTML problem.
Title only accepts text so <br/> will not work but you can put a line break (
) in there:
<h:outputText value="#{cntc.mail }" title="Nom: #{cntc.nom}
Qualite: #{cntc.qualite}" />
Note that this may not work in every browser.

Related

How to bold specific Strings in an h:column

I have a JSF data table that is displaying data based off a search successfully. However, I'm not sure how to selectively bold certain text data in a particular column.
So, for instance, I would like this text...
Here is some text that would be inside the h:column
to show up like this on the page...
Here is some text that would be inside the h:column
Here's what my data table looks like
Results:
<h:dataTable var="results"
value="#{logSearcherBean.results}"
border="1">
<h:column>#{results.logName}</h:column>
<h:column>#{results.matchLine}</h:column>
</h:dataTable>
You could either homebrew an EL function which manipulates the column value and returns the desired HTML,
<h:outputText value="#{my:highlight(results.logName, logSearcherBean.query)}" escape="false" />
(note that this is due to escape="false", which is mandatory to present HTML literally, also sensitive to XSS attacks if the logName is a value which is fully controlled by the enduser)
Or grab JavaScript/jQuery which manipulates the returned HTML, see also this related question: Highlight a word with jQuery.
Hi all,
<p:column id="lastName"
headerText="Last Name">
<h:outputText value="#{person.lastName}" style="#{myBean.getStyle(person.lastName)}"/>
</p:column>
And in the bean:
public String getStyle(String str) {
return str.equals(keyword) ? "background-color: yellow" : "";
}
All best and happy coding!

Escape false is not working in primefaces?

I am using primefaces 3.2 and I have used escape=false for the outputText to display the output. This is not working for me.
val = "<ol><li>sfsfsd</li><li>fgsdsdg</li></ol>"
<h:outputText escape="false" value="#{dummyBean.val}" />
It is not displaying the numbers instead I am getting a dot (.) on display.
I am gettting output like :
. sfsfsd
. fgsdsdg
But the expected output is :
1.sfsfsd
2.fgsdsdg
A weird idea:
what if the html is indeed escaped but you have some styling issue which hides the numbers before the dots. I would try to add some left padding either to the printed list
<ol style="padding-left: 100px;">
<li>First</li>
<li>Second</li>
</ol>
or to the span itself
<h:outputText escape="false" value="#{dummyBean.val}" style="padding-left: 100px;"/>
and see what happens.
What you're looking for is just a different way to style an ordered list.
You just need to add this style
ol {
list-style-type: decimal;
}

Output curly brace in JSF h:outputText

I want to use the following EL inside a custom component:
<ui:param name="valueAfter"
value="#{not empty valueAfter ? valueAfter : false}" />
<h:outputText
value="#{x.label}#{valueAfter == true ? {x.value} : ''}" />
This does not work as I cannot output the curly braces around x.value but now I am looking for a good way to actually output those.
Try this:
<h:outputText value="#{x.label"}/>
<h:outputText value="{#{x.value}}" rendered="#{valueAfter}"/>
Here you go -In case you are using EL 2 (I think), one line with one h:outputText solution thanks to the .concat()
<h:outputText value="#{x.label}#{valueAfter == true?'{'.concat(x.value).concat('}'):''}" />

Concatenating strings within EL expression defined in an attribute of a facelets tag

I need to write an EL expression for an attribute which goes something like this:
#{cc.attrs.appreciatedByCurrentUser ? (cc.attrs.count +'<br/>'+ (cc.attrs.count-1)) : ((cc.attrs.count+1) +'<br/>'+ cc.attrs.count)}
Now the problem is that this gives an error as strings cannot be concatenated, the way I am doing it. So how can I rectify this?
I'm using JSF 2.0 with facelets.
EDIT :
I'm resolving the issue using the following inline javascript
<script type="text/javascript">
var count=#{cc.attrs.count};
document.write(#{cc.attrs.appreciatedByCurrentUser} ? (count-1) +'<br/>'+count : count+'<br/>'+ (count+1));
</script>
Can you think of any issue with this?
It is possible to concatenate Strings in EL using the java.lang.String.concat(String) method. Thus your code could look like:
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? (''.concat(cc.attrs.count).concat('<br/>').concat(cc.attrs.count-1)) : (''.concat((cc.attrs.count+1)).concat('<br/>').concat(cc.attrs.count))}" escape="false" />
In this particular case however I would go with one of the options that Mr BalusC had suggested because the code above doesn't look quite elegant. In some cases knowing this technique could be useful, though.
I would hardly recommend using javascript as a workaround here.
String concatenation in EL is only possible by just inlining in the expression. The + operator is in EL exclusively a sum operator. Further, < and > are invalid characters in XML attributes, so you have to escape them (and instruct <h:outputText> to not escape them once again by escape="false"):
<h:outputText value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" escape="false" rendered="#{cc.attrs.appreciatedByCurrentUser}" />
<h:outputText value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" escape="false" rendered="#{!cc.attrs.appreciatedByCurrentUser}" />
Alternatively, you can also use <c:set> to alias the expression:
<c:set var="appreciated" value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" />
<c:set var="notAppreciated" value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" />
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? appreciated : notAppreciated}" escape="false" />
This is the only thing i can come up with.
<h:panelGroup rendered="#{cc.attrs.appreciatedByCurrentUser}">
<h:outputText value="#{(cc.attrs.count)}" style="display:block;" />
<h:outputText value="#{(cc.attrs.count-1)}" />
</h:panelGroup>
<h:panelGroup rendered="#{not cc.attrs.appreciatedByCurrentUser}">
<h:outputText value="#{(cc.attrs.count+1)}" style="display:block;" />
<h:outputText value="#{(cc.attrs.count)}" />
</h:panelGroup>
Putting <br> in a value attribute will always throw errors in JSF, so you'll have to use display:block.

JSF: h:outputText; how to show a dash when the value is empty string?

I'm using h:outputText tags to display readonly data. Ex:
<h:outputText value="Phone Number:" />
<h:outputText value="#{userHandler.user.phoneNumber}" />
When "phoneNumber" is an empty string or a null, I want to display a dash "-" as the value.
Is there any easy way to do this maybe with expression language or something?
BTW, I thought about adding methods to the User class like getPhoneNumberDisplayText() that could do the check internally, but I since it's a view issue, I'd rather keep the code in the JSF page.
<h:outputText value="#{userHandler.user.phoneNumber != null
? userHandler.user.phoneNumber : '-'}" />
Or, you could make a new outputText:
<h:outputText rendered="#{userHandler.user.phoneNumber == null}" value="-" />

Resources