Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
JS align each column dynamically based on data

There is a message recieved from the backend which looks like this:

[
    {
        "Name": "ABC",
        "ColumnType": "NUMBER"
    },
    {
        "Name": "XYZ",
        "ColumnType": "STRING"
    },
    {
        "Name": "EFG",
        "ColumnType": "NUMBER"
    },
    {
        "Name": "JKL",
        "ColumnType": "STRING"
    },
    {
        "Name": "TOP",
        "ColumnType": "TIMESTAMP"
    },
    
]

The "Name" is used as the column name in the table and the data in the column is aligned w.r.t the column type i.e For a ColumnType "NUMBER" and "TIMESTAMP" right align and for ColmnType "STRING" left align.

I have tried the following,

 var td = document.getElementsByTagName('td');
        let tdArr = Array.from(td);
        for (var item in snapshot) {
          let DataHeader = snapshot[item].DataHeader;
          for(let x in DataHeader){
              let colType = snapshot[item].DataHeader[x].columnType;
                 for(let z in tdArr){
                    if(colType === 'STRING' && colType !== 'NUMBER'){
                      tdArr[z].style.textAlign= "left";
                    }
                    else if(colType === 'NUMBER' && colType !== 'STRING'){
                      tdArr[z].style.textAlign= "right";
                    }
                    else if(colType === 'TIMESTAMP'){
                      tdArr[z].style.textAlign= "right";
                    }
                 }
            }
        }

This however sets all tds to either right align or left align. How do I work around this? I know I'm setting all td elements and I'm not sure how I can grab td elements of a particular column.

The above shown message is part of a snapshot object which has a DataHeader. The DataHeader contains the "Name" and the "ColumnType".

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When using .forEach() or .map() you get the index as second parameter, which can be very helpful

let columnInfos = [
    {
        "Name": "ABC",
        "ColumnType": "NUMBER"
    },
    {
        "Name": "XYZ",
        "ColumnType": "STRING"
    },
    {
        "Name": "EFG",
        "ColumnType": "NUMBER"
    }
]

columnInfos.forEach((columnInfo, index) => {

  let columnType = columnInfo.ColumnType

  let tds = Array.from(document.querySelectorAll(`td:nth-of-type(${index+1})`))

  if(columnType === 'NUMBER' || columnType === 'TIMESTAMP') {
    tds.forEach(td => td.style.textAlign = 'right')
  }else if(columnType === 'STRING'){
    tds.forEach(td => td.style.textAlign = 'right')
  }
})


about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!