1

I have something like this in my jsf:

<div title="Percentage: #{myBean.numberToBeConvertedToPercentage}"></div>

I wanted to make something like this:

<h:outputText value="#{myBean.numberToBeConvertedToPercentage}">
    <f:convertNumber maxFractionDigits="2" type="percent"/>
</h:outputText>

Of course, not inside an output text, but instead setting the converted number to a variable, so I can use it inside my div. I don't want to take this converted number direct from my bean, since I use it in another places of my view without formatting it, and creating a get just for this, I don't like the idea. There is any way to make this only in my view?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Montolide
  • 773
  • 3
  • 12
  • 27

2 Answers2

1
  1. To go with <h:outputText> + converter, it is the best approach for this purpose in JSF. You can redesign your Html to separate the label "Percentage: " and the data. to use rendered value in JS code to pass it to 'title' attribute.

    <div id="titleDiv" />
    
    <script type="text/javascript">
    $('#titleDiv').title = 'Percentage: <h:outputText value="#{myBean.numberToBeConvertedToPercentage}"><f:convertNumber maxFractionDigits="2" type="percent"/></h:outputText>';
    </script> 
    
  2. If you still want to stay without converters, then you can create one more bean per your view and render values in get methods.

  3. Other option is to use EL 2.2 Method call feature and to call your own format bean

    <div title="Percentage: #{formatBean.formatNumber(myBean.numberToBeConvertedToPercentage, 2)}"></div>
    
udalmik
  • 7,838
  • 26
  • 40
  • I'm not sure how #1 is ever possible in this construct. Do you mean to render the tooltip as a separate hidden `
    `/`` and show it using CSS/JS instead of using `title` attribute?
    – BalusC Aug 28 '12 at 13:49
  • Oh .. I have missed that you need 'title', thought attribute name is not meaningful. In that case - yes, rendered data could be passed to JS, but I do not like number '1' now) – udalmik Aug 28 '12 at 13:58
  • I'm almost getting tired and using some kind of the #2, using another getter like `getNumberToBeConvertedToPercentageAlreadyConverted` :P #3 looks too much of overkill for this case :P – Montolide Aug 28 '12 at 14:22
  • @mudalov what's your idea if I use converters? – Montolide Aug 29 '12 at 11:46
0

I have not checked this, but maybe you can use ui:param and assign a converter to it.

and then use it in your page

<div title="Percentage: #{myVar}></div>

see Defining and reusing an EL variable in JSF page

Community
  • 1
  • 1
roel
  • 2,005
  • 3
  • 26
  • 41