PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery.

I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery code does not seem to be functioning.

.xhtml

    <ui:composition template="/pages/layout.xhtml">

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
    <script type="text/javascript">

    $(document).ready(function() {
          $('tr > td.y_n').each(function() {
              colsole.log("in function");
            if ($(this).text() === "Passed") {
                colsole.log("in if");
              $(this).css('background-color', '#FF0000');
            } else {
                colsole.log("in if else");
              $(this).css('background-color', '#3AC709');
            }
          });
        });


    </script>

    <p:dataTable id="idExecution" var="varTS" value="#{executionBean.lstLiveSelectedSuiteData}" style="margin-bottom:20px">

<f:facet name="header"> Steps </f:facet>

<p:column headerText="Status" class="y_n" >
        <h:outputText value="#{varTS.status}"  />
</p:column>

<p:column headerText="Error Message">
      <h:outputText value="#{varTS.errorMessage}" />
</p:column>
</p:dataTable>
 </ui:composition>

Note: The HTML provided in the jsfiddle is generated at runtime. (xhtml > html)

http://jsfiddle.net/z2ty0q8k/

Anxious to see the color change reflecting the status column values.

Answer №1

If you want to enhance the appearance of your data table, consider using Primefaces Datatable RowColor
To begin, create a styleclass in your CSS file.


CSS:

<style type="text/css>
.passed {
    background-color: red !important;
}
.inProgress {
    background-color: green !important;
}   
</style>

Next, apply this styleclass to your datatable using the rowStyleClass attribute (not as indicated in your code).

<p:dataTable var="var" value="#{someBean.dataList}" rowStyleClass="#{someBean.checkStatus(var.status)}">
<p:column headerText="Id">
    <h:outputText value="#{var.id}" />
</p:column>

<p:column headerText="Status">
    <h:outputText value="#{var.status}" />
</p:column>
.
.
.
</p:dataTable>

To determine the appropriate style for each status, you can either create a method in your ManageBean or use an inline if statement similar to the Primefaces example:

public String checkStatus(String status){
    if (status.equals("InProgress")) {
        return "inProgress"; //This is the corresponding styleClass in the CSS
    } else if (status.equals("Passed")){
        return "passed"; //This is the corresponding styleClass in the CSS
    } else {
        return null;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Clicking outside of a Bootstrap 3 dropdown causes it to close

Let's analyze the code below: $(document).ready(function () { $('#dropdownMenu1').attr('aria-expanded', true); $('.dropdown').addClass('open'); $('#dropdownMenu1').on('hide.bs.dropdow ...

Numerous categories housed within various div containers

I am working with an HTML code that contains 6 different div elements: <div class="hisclass1 hisclass2 myclass hisclass3"> <div class="herclass1 herclass2"> <!-- 2nd div --> </div> </di ...

Guide to displaying a pop-up modal containing text and an image when clicking on a thumbnail image

Recently delving into Bootstrap 3, I created a thumbnail grid showcasing images related to various projects. My goal is to have a modal window pop up when clicking on an image. Within the modal, I want to display the selected image alongside some descrip ...

Displaying BlockUI Growl notifications at the bottom right using Jquery

I have successfully implemented a growl notification using BlockUI for jquery. However, I am facing an issue where the notification is appearing at the top right and I want it to be at the bottom right instead. After modifying the CSS property to bottom:1 ...

Is there a CSS Grid that supports IE6+ with percentage-based layouts?

Seeking a sleek CSS grid system that is percentage-based with 12 columns and works well on IE6+ as well as all modern browsers. Support for nested columns would be a bonus but not mandatory. Many grids out there lack compatibility with IE6 or are based sol ...

Efficiently shrink column width in Material-UI's <TableRow/> using ReactJS and Material-UI

At the moment, I am utilizing ReactJS along with Material-UI. One issue I am encountering is that when using Material-UI's <Table>, the columns' width are automatically set based on content which results in all columns having equal width. H ...

Tips for updating mysql records on a web page without the need to refresh it using PHP

Consider this situation: Below is the code that shows all user accounts. <table border="3px" align="center" cellspacing="3px" cellpadding="3px"> <tr> <td>Username</td> <td>Password</td> <td><a href="" oncli ...

JS issue: Having trouble accessing the array values despite the array being present

I am working on an ajax call where I save the success data in an array. However, when I try to access that data outside of the ajax function and use console to log my array, it appears as expected. Here is a glimpse at what I see on the screen: https://i ...

Sleek Navigation in CSS and HTML

Hello, as I work on my website with multiple pages, I am looking to implement smooth scrolling on a specific page without using the html tag for the entire site. Below is the code snippet I am working with: {% if section.settings.display_product_detail_des ...

Identifying the precise image dimensions required by the browser

When using the picture tag with srcset, I can specify different image sources based on viewport widths. However, what I really need is to define image sources based on the actual width of the space the image occupies after the page has been rendered by th ...

Attempting the transformation of jQuery ajax requests to Angular's http service

I am looking to transition my existing mobile application from using jquery ajax to angularjs for handling user authentication with server-side validation. Here is the original jquery ajax code: function validateStaffUser(username, password) { var re ...

Enhance each category changing with jQuery page transitions

Is there a way to add page transitions based on category names and quantities in PHP? For example, when clicking on the "office" category, can a popup overlay be displayed with the category name and quantity while the content loads? The focus is on creat ...

The jQuery clax feature does not seem to be functioning properly when I include the jQuery min

I am facing an issue where ajax is not working when I use jQuery clix, but then jQuery clix stops working when I switch to the latest version of jQuery. <script src="{{asset('build/js/custom.min.js')}}"></script> <script src="{{as ...

During the initial cycle, the CSS keyframes animation may experience glitches, but it will run smoothly in subsequent cycles

Seeking advice on troubleshooting a CSS keyframes animation issue. I have created an animation with 5 frames where the background image fades and transitions to the next image smoothly in all cycles, except for the first cycle where it glitches before each ...

Implementing viewmodel posting in MVC with the help of jQuery

How can I make an ajax call using jQuery to post the ViewModel to a controller's action method using $.ajax? I have chosen not to use the Form element in order to avoid postback... This is how my form currently looks: HTML: @model comp.learn.data ...

Is it possible to modify the border colors of separate elements in Bootstrap 5?

<div class="m-3 p-3 border-5 border-top border-danger border-bottom border-primary"> Can borders be styled with different colors? </div> Is there a method to achieve this, possibly using sass? ...

Slider-activated Pie Chart Controller

I have successfully created a pie chart using highchart and javascript, allowing me to adjust each slice individually using a slider. However, I am facing an issue where I want the maximum value of the pie to be 100%, with the other sliders contributing to ...

A single feature designed to handle various elements

I currently have this HTML code repeated multiple times on my webpage: <form class="new_comment"> <input accept="image/png,image/gif,image/jpeg" id="file" class="file_comment" key=comment_id type="file" name="comment[media]"> <spa ...

Display the precise outcome for each division following a successful AJAX callback

I’m facing a challenge in getting individual results for each item after a successful AJAX callback. Currently, I am able to retrieve results, but when there are multiple items, all displayed results are being added to each div instead of just the corres ...

I am having trouble grasping certain syntax in JavaScript when it comes to using `${method_name}`

I'm having trouble understanding some of the syntax in this code, particularly ${method_name}. I'm not sure what we are achieving by passing the method name within curly braces. global._jsname.prototype.createEELayer = function (ftRule) { if ...