Introduction
This article describes how to modify table cells by adding cell text, cell color and blanking cells.
Requirements
- A table with a Rule applied.
- This article assumes you are already in the Edit > Edit JavaScript Rule view specified in How to Customize a Rule.
- You have read How to Access Statistics from a Table in a Rule.
Method
For each of the below methods we will use the same basic condition: When the values are less than 50, perform this change. The examples include:
- Appending an asterisk next to the first statistic in the cell.
- Changing the color of the cell to red.
- Blanking the contents of the cell.
We begin by setting the target and extracting the primary statistic values from the table:
// Set target
var min = 50;
// Get the values for the first statistic shown on the table.
var values = table.get(table.statistics[0]);
Changing or adding cell text
In order to change the cell text property, we need to first return its contents as an array so we can update it:
var cell_texts = table.cellText;
Next, we loop through each row and column, return the current value for that cell, and insert an asterisk as an array into our declared cell_texts
variable:
// Loop through each cell...
for (var row = 0; row < table.numberRows; row++) {
for (var column = 0; column < table.numberColumns; column++) {
var value = values[row][column];
if (value < min) {
cell_texts[row][column] = ['*'];
}
}
}
Finally, we pass the updated cell text array back to the table:
table.cellText = cell_texts;
Adding cell color
In order to change the cell color property, we need to first return its contents as an array so we can update it:
var cell_colors = table.cellColors;
We then need to set the color using either the HTML color hex value or the RGB color value:
var color = "#FF0000";
var color = [255, 0, 0];
See JavaScript Color Table for examples.
Next, we loop through each row and column, return the current value for that cell, and set the color in our declared cell_colors
variable:
// Loop through each cell...
for (var row = 0; row < table.numberRows; row++) {
for (var column = 0; column < table.numberColumns; column++) {
var value = values[row][column];
if (value < min) {
cell_colors[row][column] = color;
}
}
}
Finally, we pass the updated cell color array back to the table:
table.cellColors = cell_colors;
Blanking cells
Unlike the previous examples, this example doesn't require any further cell declarations or table setting.
We loop through each row and column, return the current value for that cell, and use the table.blankCell
function to remove its contents:
// Loop through each cell...
for (var row = 0; row < table.numberRows; row++) {
for (var column = 0; column < table.numberColumns; column++) {
var value = values[row][column];
if (value < min) {
table.blankCell(row, column);
}
}
}
Next
How to Create User Input Fields in a Rule
See Also
How to Add Text to Table Cells with Large Numbers
How to Change the Color of Table Cells with Small Sample Sizes
How to Blank Table Cells with Small Values
How to Access Statistics from a Table in a Rule
How to Work with JavaScript Arrays and Loops
Comments
0 comments
Article is closed for comments.