Jump to content

I want to change the background color of the cell in the tabular report conditionally with javascript


Recommended Posts

I am using following code. This code it changing the background of the text perfectly. But I want to change the background color of the cells of the column with the same conditions which are in the code. I have tried different method but unsuccefull. Is there any solution 

 

<div id="mydiv_[@field:Test_ID]"></div>
<script>
    var percentageSupport = parseFloat("[@field:Score2#]");
    var maxScore = parseFloat('[@calcfield:2#]');
    var second = parseFloat('[@calcfield:6#]');
    var third = parseFloat('[@calcfield:7#]');
    var fourth = parseFloat('[@calcfield:8#]');
    var fifth = parseFloat('[@calcfield:9#]');
var sixth = parseFloat('[@calcfield:10#]');
var seven = parseFloat('[@calcfield:11#]');
var eight = parseFloat('[@calcfield:12#]');
var nine = parseFloat('[@calcfield:13#]');
var ten = parseFloat('[@calcfield:14#]');

    if (percentageSupport >= second){
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#006400;font-weight: bold; height: 40px; display: flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 

else if (percentageSupport >=third ) {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#228B22;font-weight: bold; height: 40px; display: flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 

else if (percentageSupport >= fourth 
) {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#808000;font-weight: bold; height: 40px; display: flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 
else if (percentageSupport >= fifth 
) {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#556B2F;font-weight: bold; height: 40px; display: flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 
else if (percentageSupport >= sixth 
) {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#BDB76B;font-weight: bold; height: 40px; display: flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 
else if (percentageSupport >= seven 
) {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#8B4513;font-weight: bold; height: 40px;display:flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 
else if (percentageSupport >= eight 
) {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#B22222;font-weight: bold; height: 40px;display:flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 
else if (percentageSupport >= nine 
) {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:#8B0000;font-weight: bold; height: 40px;display:flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    } 

else {
        document.getElementById("mydiv_[@field:Test_ID]").innerHTML = "<div style='background-color:red;font-weight: bold; height: 40px; display:flex; justify-content: center; align-items: center;'>[@field:Score2#]</div>";
    }
</script>

Link to comment
Share on other sites

Hello @Mushigee2266

if you wish to change the background of cells based on a certain condition, you would need to loop through all rows in your report, compare respective cell values against your condition, and change the background of the cells accordingly.

I am not sure how the names of the columns from this DataPage correspond to variables in the code; I only found where maxScore is taking value from. It is a 25th column. Here is JavaScript draft that you need to adjust to map each variable to cell:


 

<script>

if (document.paintCellsOnCondition == undefined) {
const paintCellsOnCondition = (e) => {
 if (e.detail.appKey != '[@cbAppKey]') { return }

document.querySelectorAll('tr.cbResultSetDataRow').forEach(row=>{
// ONLY THIS PART OF CODE NEEDS UPDATES STARTING FROM HERE
let maxScore = parseFloat(row.querySelector('td:nth-child(25)').innerText) // 25 because respective cell in the 25th column
let second = parseFloat(row.querySelector('td:nth-child(RESPECTIVE "SECOND" COLUMN NUMBER)').innerText);
let percentageSupport = parseFloat(row.querySelector('td:nth-child(RESPECTIVE "percentageSupport" COLUMN NUMBER)').innerText);
.....  // by analogy, you need to map other variables to respective cells using td:nth-child() selector

// conditions will be the same, but actions should be slightly modified, for example, if you need to paint second cell when percentageSupport >= second
    if (percentageSupport >= second) {
     row.querySelector('td:nth-child(RESPECTIVE "SECOND" COLUMN NUMBER)').style = 'background-color: xxxxxx;'
    } 
....
 


// CODE UPDATES END HERE   
})

  
}

document.addEventListener('DataPageReady', paintCellsOnCondition)
document.paintCellsOnCondition = 'Enabled'
}

</script>

This code must be placed either in the header or footer only once.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...