Color my datatable's rows with condition??

UI Components for JSF
novice
Posts: 8
Joined: 21 Jul 2014, 18:56

21 Jul 2014, 19:17

Hi!! I want to color my datatable's rows with condition. For example If quantity< 10 row takes red else row takes green.
I have already tried this http://www.primefaces.org/showcase/ui/d ... olor.xhtml

Code: Select all

<p:dataTable id="articleTable" value="#{articleCtr.listArticle}" var="article" rowKey="#{article.num_article}"
				selection="#{articleCtr.selectedArticle}" selectionMode="single" rowStyleClass="#{article.quantity le 10 ? 'yes': null} #{article.quantity gt 50? 'yes': null}">
I also want to know how integrate css file in war project.

User avatar
snooperman
Posts: 155
Joined: 06 Nov 2012, 18:43

22 Jul 2014, 10:17

You need a css file, let's say default.css, that you need to include in the head. Then in this file, define a style class for your row. For the example you gave, you have two conditions in your EL, that can be combined as one using "or". I`m guessing what you meant is (quantity <= 10 or quantity > 50):

Code: Select all

<p:dataTable id="articleTable" value="#{articleCtr.listArticle}" var="article" rowKey="#{article.num_article}"
            selection="#{articleCtr.selectedArticle}" selectionMode="single" rowStyleClass="#{(article.quantity le 10 or article.quantity gt 50) ? 'yes': null}">
and in your default.css, you define the "yes" style class:

Code: Select all

.yes {
    background-color: red !important; //or whatever color you like.
}

Later edit:
For example If quantity< 10 row takes red else row takes green.
(you said nothing about >50 here)

For this case, you need another css class, let's say "no":

Code: Select all

<p:dataTable id="articleTable" value="#{articleCtr.listArticle}" var="article" rowKey="#{article.num_article}"
            selection="#{articleCtr.selectedArticle}" selectionMode="single" rowStyleClass="#{(article.quantity lt 10) ? 'yes': 'no'}">

Code: Select all

.yes {
    background-color: red !important; //or whatever color you like.
}

.no {
    background-color: green !important; //or whatever color you like.
}
var details = {
. . PF.version: "5.0.x",
. . JSF.version: "2.x",
. . Tomcat.version: "8.x"
}

novice
Posts: 8
Joined: 21 Jul 2014, 18:56

22 Jul 2014, 21:54

@Snooperman Thank you but il looks like you're code has an error in row style class!!!

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

22 Jul 2014, 23:32

WHAT error???

novice
Posts: 8
Joined: 21 Jul 2014, 18:56

23 Jul 2014, 00:27

The error was mine sorry!! but it doesn't work in my case :? :? :?

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

23 Jul 2014, 08:31

So it DOES work in general, but is not what you want?

novice
Posts: 8
Joined: 21 Jul 2014, 18:56

23 Jul 2014, 19:07

So what can I do to fix it!! Can you help me?

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

23 Jul 2014, 19:10

Please answer my question in stead of asking for general help. It does not work obviously, but do you get error, or not the behaviour you want or???

User avatar
snooperman
Posts: 155
Joined: 06 Nov 2012, 18:43

24 Jul 2014, 09:46

Have you included your css file in head?

Code: Select all

<h:head> 
    <link type="text/css" rel="stylesheet"
			href="#{request.contextPath}/css/default.css" />
</h:head>
var details = {
. . PF.version: "5.0.x",
. . JSF.version: "2.x",
. . Tomcat.version: "8.x"
}

novice
Posts: 8
Joined: 21 Jul 2014, 18:56

24 Jul 2014, 16:58

It doesn't work at all there is no error. I can see the page but the rows are not colored.

I add the css file in the head of my page but nothing happens.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 52 guests