Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To the point, you expected that the converter's properties are set every time a datatable row is rendered. This is indeed not true. JSF will create only one converter instance per component when the view is to be built, it will not create/reset the converter each time the row is rendered. </p> <p>There are several ways to get it to work. </p> <ul> <li><p>Pass the dynamic attributes as <code>&lt;f:attribute&gt;</code> of the component and let the <code>Converter</code> intercept on that. You can find an example here: <a href="https://stackoverflow.com/questions/7122460/jsf-convertdatetime-with-timezone-in-datatable/7123153#7123153">JSF convertDateTime with timezone in datatable</a>. This can then be used as</p> <pre class="lang-html prettyprint-override"><code>&lt;h:outputText value="#{item.balanceDate}"&gt; &lt;f:converter converterId="isoDateTimeConverter" /&gt; &lt;f:attribute name="pattern" value="#{item.pattern}" /&gt; &lt;/h:outputText&gt; </code></pre> <hr></li> <li><p>Use an EL function instead of a <code>Converter</code>. You can find an example here: <a href="https://stackoverflow.com/questions/2378826/facelets-and-jstl-converting-a-date-to-a-string-for-use-in-a-field/2381443#2381443">Facelets and JSTL (Converting a Date to a String for use in a field)</a>. This can then be used as</p> <pre class="lang-html prettyprint-override"><code>&lt;h:outputText value="#{mh:convertIsoDate(item.balanceDate, item.pattern)}" /&gt; </code></pre> <hr></li> <li><p>Bind the converter and datatable's <code>DataModel</code> as a property of the same managed bean. This way you will be able to set the converter's properties based on the row data before returning it. Here's a basic kickoff example based on standard JSF components and standard <code>DateTimeConverter</code> (it should work equally good on PrimeFaces components and with your custom converter):</p> <pre><code>&lt;h:dataTable value="#{bean.model}" var="item"&gt; &lt;h:column&gt; &lt;h:outputText value="#{item.date}" converter="#{bean.converter}" /&gt; &lt;/h:column&gt; &lt;/h:dataTable&gt; </code></pre> <p>with</p> <pre><code>@ManagedBean @ViewScoped public class Bean implements Serializable { private List&lt;Item&gt; items; private DataModel&lt;Item&gt; model; private DateTimeConverter converter; @PostConstruct public void init() { items = Arrays.asList( new Item(new Date(), "dd-MM-yyyy"), new Item(new Date(), "yyyy-MM-dd"), new Item(new Date(), "MM/dd/yyyy")); model = new ListDataModel&lt;Item&gt;(items); converter = new DateTimeConverter(); } public DataModel&lt;Item&gt; getModel() { return model; } public Converter getConverter() { converter.setPattern(model.getRowData().getPattern()); return converter; } } </code></pre> <p><em>(the <code>Item</code> class is just a bean with two properties <code>Date date</code> and <code>String pattern</code>)</em></p> <p>this results in</p> <blockquote> <p>23-09-2011<br> 2011-09-23<br> 09/23/2011 </p> </blockquote> <hr></li> <li><p>Use <a href="http://omnifaces.org" rel="nofollow noreferrer">OmniFaces</a> <code>&lt;o:converter&gt;</code> instead. It supports render time evaluation of EL in the attributes. See also <a href="http://showcase.omnifaces.org/taghandlers/converter" rel="nofollow noreferrer">the <code>&lt;o:converter&gt;</code> showcase example</a>.</p> <pre><code>&lt;h:outputText value="#{item.balanceDate}"&gt; &lt;o:converter converterId="isoDateTimeConverter" pattern="#{item.pattern}" /&gt; &lt;/h:outputText&gt; </code></pre></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload