When it is a number display text center else text left

The numbers with the colors and the numbers with the percentage must center align and the text must left align

Im only getting to center align for the numbers with colors. The numbers with the percentage values must also center align but it still showing left align can anyone tell me whats wrong please. Thanks in advance

if (pods[i].data.summaryDetailedStepThroughData !== undefined && pods[i].data.summaryDetailedStepThroughData.length > 0) {

                        rowspanMax = 0;
                        for (var l = 0; l < Object.keys(pods[i].data.summaryDetailedStepThroughData).length; l++) {
                            contentHtml += "<tr>";

                            for (var m = 0; m < pods[i].data.columns.length; m++) {

                                for (var field in pods[i].data.summaryDetailedStepThroughData[l]) {
                                    var rowspan = (pods[i].data.summaryDetailedStepThroughData[l].children !== undefined ? pods[i].data.summaryDetailedStepThroughData[l].children.length : "");
                                    rowspanMax = Math.max(rowspanMax, rowspan);

                                    if (field === pods[i].data.columns[m]) {

                                        var preFix = pods[i].data.summaryDetailedStepThroughData[l]["sPrefix"] !== undefined ? pods[i].data.summaryDetailedStepThroughData[l]["sPrefix"] : "";
                                        var postFix = pods[i].data.summaryDetailedStepThroughData[l]["sPostfix"] !== undefined ? pods[i].data.summaryDetailedStepThroughData[l]["sPostfix"] : "";
                                        var value = pods[i].data.summaryDetailedStepThroughData[l][field];
                                        if (value.toString().substr(0, 3) == "- 1") {
                                            value = "N/A";
                                        }
                                        var color = pods[i].data.summaryDetailedStepThroughData[l][field + "_color"];
                                        if (colName[m] === "sLabel" && pods[i].data.summaryDetailedStepThroughData[l].bStepThrough == true) {
                                            value = "<a href=\"#\" class=\"stepthrough\">" + value + "</a>";
                                        }
                                        color = color !== "" && color !== undefined ? " <span class=\"color\" style=\"background: #" + color + "\"></span>" : " <span class=\"color\"></span>"; contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
                                        if (rowspan > 1) {
                                            var rowspanContent = "<td rowspan1=\"" + rowspan + "\" class=\"" + (rowspan !== "" && rowspan > 1 ? "groups" : "") + " " + (!isNaN(value) || (!isNaN(value.toString().substr(1, value.length)) || value == "N/A" || typeof value == Number) ? "text-center" : "text-left") + "\">" + value + (Number(value) ? preFix : "") + color + (!isNaN(value) ? postFix : "") + "</td>";
                                        }

Your code looks kind of truncated… anyway one approach would be to check if the textContents of the tds start with numbers using a regular expression, like e.g.

<table id="my-table">
  <tbody>
    <tr>
      <td>foo</td>
      <td>10</td>
      <td>20%</td>
    </tr>
  </tbody>
</table>
const data = document.querySelectorAll('#my-table td')

Array.from(data).forEach(td => {
  if (/^\d+/.test(td.textContent)) {
    td.style.textAlign = 'center'
  }
})

fiddle

2 Likes

Is there a reason why you can’t avoid using JavaScript for this and style using only CSS ?

<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Table Cell Text Alignment test</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<style type="text/css">
td {
 text-align: center;
}
td:first-of-type {
 text-align: left; 
}
</style>
</head>
<body>
<h1>Table Cell Text Alignment test</h1>
<table class="position">
 <caption>Left and Center CSS example</caption>
 <thead>
  <tr>
   <th>word</th><th>number</th><th>percent</th><th>number</th><th>percent</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>first</td><td>42</td><td>51%</td><td>1</td><td>2%</td>
  </tr>
  <tr>
   <td>middle</td><td>27</td><td>66%</td><td>18</td><td>71%</td>
  </tr>
  <tr>
   <td>last</td><td>83</td><td>28%</td><td>79</td><td>20%</td>
  </tr>
 </tbody>
</table>
</body>
</html>
2 Likes

The thing is i cant edit the html all the html is build in javascript

That wont help sometimes the text get change to the second or 3rd row

That shouldn’t make any difference eg. this table generated by JavaScript

<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Table Cell Text Alignment test</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<style type="text/css">
td {
 text-align: center;
}
td:first-of-type {
 text-align: left; 
}
</style>
</head>
<body>
<h1>Table Cell Text Alignment test</h1>
<script>
function new_tag(type) {
 var new_tag = document.createElement(type);
 return new_tag;
}
function add_text(tag, text) {
 tag.textContent = text;
}
function add_child(parent, child) {
 parent.appendChild(child);
}
var doc_body = document.getElementsByTagName("body")[0];
var table_tag = new_tag('table');
var caption_tag = new_tag('caption');
 add_text(caption_tag, "Table Generated by JavaScript");
var thead_tag = new_tag('thead');
var thead_tr_tag = new_tag('tr');
var th_tag_1 = new_tag('th');
 add_text(th_tag_1, "Word");
var th_tag_2 = new_tag('th');
 add_text(th_tag_2, "Number");
var th_tag_3 = new_tag('th');
 add_text(th_tag_3, "Percent");
 
 add_child(thead_tr_tag, th_tag_1);
 add_child(thead_tr_tag, th_tag_2);
 add_child(thead_tr_tag, th_tag_3);
 add_child(thead_tag, thead_tr_tag);
var tbody_tag = new_tag('tbody');
var tr_tag_1 = new_tag('tr');
var tr_tag_1_td_tag_1 = new_tag('td');
 add_text(tr_tag_1_td_tag_1, "OneMuchLongerWord");
var tr_tag_1_td_tag_2 = new_tag('td');
 add_text(tr_tag_1_td_tag_2, "5");
var tr_tag_1_td_tag_3 = new_tag('td');
 add_text(tr_tag_1_td_tag_3, "43%");
 
 add_child(tr_tag_1, tr_tag_1_td_tag_1);
 add_child(tr_tag_1, tr_tag_1_td_tag_2);
 add_child(tr_tag_1, tr_tag_1_td_tag_3);
 add_child(tbody_tag, tr_tag_1);
var tr_tag_2 = new_tag('tr');
var tr_tag_2_td_tag_1 = new_tag('td');
 add_text(tr_tag_2_td_tag_1, "Shorty");
var tr_tag_2_td_tag_2 = new_tag('td');
 add_text(tr_tag_2_td_tag_2, "67");
var tr_tag_2_td_tag_3 = new_tag('td');
 add_text(tr_tag_2_td_tag_3, "72%");
 
 add_child(tr_tag_2, tr_tag_2_td_tag_1);
 add_child(tr_tag_2, tr_tag_2_td_tag_2);
 add_child(tr_tag_2, tr_tag_2_td_tag_3);
 add_child(tbody_tag, tr_tag_2);
 
 add_child(table_tag, caption_tag);
 add_child(table_tag, thead_tag);
 add_child(table_tag, tbody_tag);
 add_child(doc_body, table_tag);
</script>
</body>
</html>

Ah, well that would make difference.
Probably six of one, half dozen the other - determining the cell content and styling by “nth” or each cell by cell.

I don’t know, but I imagine a table would need to get very long before it made any appreciable difference.

1 Like

Is there not a solution to my code

I think @m3g4p0p has a good approach.

The trick will be getting the regular expression right.
Then it’s a simple matter of setting a class value for either left or center depending on the regex used.

Finding a good regex can require a lot of thought.

You need to determine what content will always be there, what might be there and what will never be there.

For example,
Will “word” cells always have only alpha and spaces and never any other character?
Will “number” cells always have only digits and never any other character?
Will “percent” cell always have only digits and a percent symbol and never any other character?

Or might some cell content sometimes have punctuation marks?

1 Like

I am not a programmer, so my perspective may sound silly, but try to follow the logic (if there is any).

Where does the data that goes into the table come from? How does it arrive and how does it know which cell it belongs in?

What is to prevent you from adding a regex script that evaluates the data as “contains alpha characters” or not and assigns “text-align:left” to the cell if it does contain alpha characters? Would that be too simplistic?

(oops, ninja’d :lol:)

1 Like

The data comes from the backend

All the data comes from the backend is there not a way to modify my code to use Nan or IsNan operators

What exactly do you have control over? What can you do or change?

I can change nothing in the frontend I need to code the solution in javascript like for my code above

It sounds like the page has to accept whatever data it is fed and you are describing a regex script that will scan the page “on Page Load” (or however it’s done) and temporarily align the cells based on their content.

OK, this has been fun… real programmers are needed, now. :slight_smile:

You need to know what the cells might contain.

eg. is “Windows 10” a valid “word”?

JavaScript does have ways to determine data types.

But as you have discovered “10%” is a String, not a Number.
You could test for parseInt, but “Windows 10” would be truthy.

IMHO this would be ideally handled by the backend code, not JavaScript.

Because you are limited to doing this with JavaScript, coming up with a good regex would be a good way to to it.

Cant you give me a good solution for the regex script :grinning: the words is passed by a string and the numbers is passed as a number in the backend

How is the percent symbol getting there?

The % value is been passed either as a prefix or postfix

Did you examine @m3g4p0p’s recommendation?

Do you know how to write a regex expression?

Know im new to javascript its the first time I here of a regex expression how do I apply it to my code above