You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Rønnevik,
Eivind <ei...@kadme.com> on 2006/07/27 10:53:00 UTC
Problem with and conditional rowClasses
Hi!
I have a problem which I hoped somebody could have a look at. It might be that what I'm trying to do isn't possible, but I think it should have worked ;)
To give you a quick presentation of my "case" or problem:
- I have a dataTable which is filled with Document objects retrieved from a database.
- The Document object has a property "created" which is a Date timestamp
- In the dataTable I alternate the background of each row with white and blue
- Then, what I would like to do, is to do a test on the "created" property, if the Document has been created within a fixed time, present it with a yellow line instead.
- I set the different background-colors with three different styles, tableViewRow1 - tableViewRow2 and tableViewRow3 (the latest is the yellow line).
To do a test on the dates I convert them into milliseconds and do a simple subtraction - if todayDate minus createdDate is smaller than criteriaDate, then the Document is considered "new" and should have a yellow background.
This is how I've defined my dataTable:
<h:dataTable id="documentsTableView"
binding="#{documentModelFactory.htmlDataTable}"
value="#{documentModelFactory.documents}"
var="document"
columnClasses="tableViewColumn"
rowClasses="#{ (documentModelFactory.todayInMillisec - document.createdInMillisec) < documentModelFactory.criteriaInMillisec ? 'tableViewRow3':'tableViewRow1,tableViewRow2'}"
styleClass="tableView"
footerClass="tableViewFooter"
headerClass="tableViewHeader"
>
So this would be the interesting lines:
rowClasses="#{ (documentModelFactory.todayInMillisec - document.createdInMillisec)< documentModelFactory.criteriaInMillisec ? 'tableViewRow3':'tableViewRow1,tableViewRow2'}"
I don't get an exception, and my expression seems valid. But it seems like it is always rendered to be false, I only get the white and blue rows, and I don't understand why.
I have done different tests on the expression, and this confuses me, because it seems like the result is what it should be.
# Test 1
For each row I created a column printing the result of the expression, false or true.
<h:column>
<f:facet name="header">
<h:outputText value="Test " />
</f:facet>
<h:outputText value="#{ (documentModelFactory.todayInMillisec - document.createdInMillisec)< documentModelFactory.criteriaInMillisec }" />
</h:column>
In all cases this is giving me the correct result, new documents get 'true' and the old ones get 'false'.
So then I thought that there is a problem setting the tableViewRow3 param properly for the <tr class="" > :
#Test 2
I created a selectDocument action which basically just sets the document in my bean when a commandLink in the dataTable is fired.
Then, in my method I retrieve the HtmlDataTable object, and do a logging of the current rowClasses for that row:
htmlDataTable.getRowClasses() - which returns a String
If I selected an "old" document, it returned "'tableViewRow1,tableViewRow2", but if i selected a new document, it returned 'tableViewRow3'.
So it seems like my expression is rendered correctly, the rowClass is set properly, but still I don't get a yellow line. In the html source I have only <tr>'s with
'tableViewRow1' and 'tableViewRow2'.
Anybody? :)
I don't know if it has something to do with the time / when the rowClasses param is "rendered", that it doesn't know what the "document" property is at that time??? I had the impression that the html was encoded "on the fly", and then this shouldn't be a problem? But I'm not exactly an expert so.. :)
I guess an alternative solution is to do the testing in my bean and build up a comma-separated String to specify a rowClass for each row displayed in the dataTable, and then set the rowClasses to something like
rowClasses="#{bean.rowStyles}"
but first solution seems like a much easier approach (less costly as well), and it would also be interesting to know why it doesn't work.
Thank you for your time!
Eivind Roennevik
Re: Problem with and conditional rowClasses
Posted by Frank Langelage <fr...@lafr.de>.
To highlight cells of a datatable with a red background I did this:
<t:columns value="#{productionCapacityOverview.workingDays}"
var="workingDay"
styleClass="#{row.required[workingDay]>row.available[workingDay]?'rvalue
bg_error':'rvalue'}">
<f:facet name="header">
<h:outputText value="#{workingDay}"/>
</f:facet>
<h:outputText
value="#{row.required[workingDay]/row.available[workingDay]}">
<f:convertNumber pattern="##0 %"/>
</h:outputText>
</t:columns>
The styleClass is obviously evaluated for every cell, not only once per
column.
Rønnevik wrote:
> Hi!
>
> I have a problem which I hoped somebody could have a look at. It might
> be that what I'm trying to do isn't possible, but I think it should
> have worked ;)
>
> To give you a quick presentation of my "case" or problem:
>
> - I have a dataTable which is filled with Document objects retrieved
> from a database.
> - The Document object has a property "created" which is a Date timestamp
> - In the dataTable I alternate the background of each row with white
> and blue
> - Then, what I would like to do, is to do a test on the "created"
> property, if the Document has been created within a fixed time,
> present it with a yellow line instead.
> - I set the different background-colors with three different styles,
> tableViewRow1 - tableViewRow2 and tableViewRow3 (the latest is the
> yellow line).
>
> To do a test on the dates I convert them into milliseconds and do a
> simple subtraction - if todayDate minus createdDate is smaller than
> criteriaDate, then the Document is considered "new" and should have a
> yellow background.
>
> This is how I've defined my dataTable:
>
>
> <h:dataTable id="documentsTableView"
> binding="#{documentModelFactory.htmlDataTable}"
> value="#{documentModelFactory.documents}"
> var="document"
> columnClasses="tableViewColumn"
> rowClasses="#{ (documentModelFactory.todayInMillisec -
> document.createdInMillisec) <
> documentModelFactory.criteriaInMillisec
> ? 'tableViewRow3':'tableViewRow1,tableViewRow2'}"
> styleClass="tableView"
> footerClass="tableViewFooter"
> headerClass="tableViewHeader"
> >
>
> So this would be the interesting lines:
> rowClasses="#{ (documentModelFactory.todayInMillisec -
> document.createdInMillisec)< documentModelFactory.criteriaInMillisec
> ? 'tableViewRow3':'tableViewRow1,tableViewRow2'}"
>
> I don't get an exception, and my expression seems valid. But it seems
> like it is always rendered to be false, I only get the white and blue
> rows, and I don't understand why.
>
> I have done different tests on the expression, and this confuses me,
> because it seems like the result is what it should be.
>
> # Test 1
> For each row I created a column printing the result of the expression,
> false or true.
>
> <h:column>
> <f:facet name="header">
> <h:outputText value="Test " />
> </f:facet>
> <h:outputText value="#{ (documentModelFactory.todayInMillisec -
> document.createdInMillisec)< documentModelFactory.criteriaInMillisec
> }" />
> </h:column>
>
> In all cases this is giving me the correct result, new documents get
> 'true' and the old ones get 'false'.
>
> So then I thought that there is a problem setting the tableViewRow3
> param properly for the <tr class="" > :
>
> #Test 2
> I created a selectDocument action which basically just sets the
> document in my bean when a commandLink in the dataTable is fired.
> Then, in my method I retrieve the HtmlDataTable object, and do a
> logging of the current rowClasses for that row:
> htmlDataTable.getRowClasses() - which returns a String
>
> If I selected an "old" document, it returned
> "'tableViewRow1,tableViewRow2", but if i selected a new document, it
> returned 'tableViewRow3'.
>
> So it seems like my expression is rendered correctly, the rowClass is
> set properly, but still I don't get a yellow line. In the html source
> I have only <tr>'s with
> 'tableViewRow1' and 'tableViewRow2'.
>
> Anybody? :)
>
> I don't know if it has something to do with the time / when the
> rowClasses param is "rendered", that it doesn't know what the
> "document" property is at that time??? I had the impression that the
> html was encoded "on the fly", and then this shouldn't be a problem?
> But I'm not exactly an expert so.. :)
>
> I guess an alternative solution is to do the testing in my bean and
> build up a comma-separated String to specify a rowClass for each row
> displayed in the dataTable, and then set the rowClasses to something
> like
>
> rowClasses="#{bean.rowStyles}"
>
> but first solution seems like a much easier approach (less costly as
> well), and it would also be interesting to know why it doesn't work.
>
> Thank you for your time!
>
> Eivind Roennevik
>
>
>
>
>