title: How do I conditionally color the background in a table cell?
body: <p>I am rendering a table with <strong>p:dataTable</strong> (PrimeFaces) and what I want to do is color the background of cells depending on the value of their content.  This is different from coloring a row or a column -- it is the individual cell.</p>
  
  <p>First a CSS problem.  If I do this:</p>
  
  <pre><code>                    &lt;p:column headerText="xyzzy"&gt;
                          &lt;div style="background-color: green"&gt;
                              &lt;h:outputText value="#{rowVar.anumber}" &gt;
                                  &lt;f:convertNumber groupingUsed="true" /&gt;
                              &lt;/h:outputText&gt;                        
                          &lt;/div&gt;
                      &lt;/p:column&gt;
  </code></pre>
  
  <p>the background color just of the <em>content</em> gets set, not the whole cell.  In other words the padding is still the default.</p>
  
  <p>Second, I want to make the <strong>style</strong> string a variable expression.  I can add a function to the backing bean, but how do I access the table content in the method?   Will this work?</p>
  
  <pre><code>&lt;div style="#{bean.computeCSS(rowVar.number}"&gt;
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I figured out a way to do the conditional part, but I still need help with the CSS part.  My solution looks like:</p>
  
  <pre><code>                    &lt;p:column headerText="xyzzy"&gt;
                          &lt;div class="#{rowVar.anumber gt 0 ? 'colored' : ''}"&gt;
                              &lt;h:outputText value="#{rowVar.anumber}"&gt;
                                  &lt;f:convertNumber groupingUsed="true" /&gt;
                              &lt;/h:outputText&gt;                        
                          &lt;/div&gt;
                      &lt;/p:column&gt;
  </code></pre>
  
  <p>Although I dislike getting to fancy in EL, this has the advantage of not needed a backing bean method.</p>
  
  <p>However I still only get the background color set, not the whole cell.  </p>