Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda