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

230
Visualizações
Add style according to the value of x-text/html value

I want to center my td element when the text is equals to "-". If the text is equals to something else, I want it align at the left.

How can I do that?

<td x-bind:style="$el.textContent == '-' ? { 'text-align': 'left' } : { 'text-align': 'center' }" x-text="format(variable)"></td>

Yes, I could simple replace the $el.textContent by format(variable), but I would like to not call format(variable) twice.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the variable itself at the style binding:

<td x-bind:style="variable !== '' ? { 'text-align': 'left' } : { 'text-align': 'center' }" x-text="format(variable)"></td>

This way the style binding is reactive as well.

Edit:

If you really want to avoid using variable at the style binding, you can combine x-init with the $nextTick magic to wait until Alpine.js finishes updating the DOM, so you can grab the actual content of the cell and update the cell's style if it's just a dash. However this method is not reactive.

<td x-init="$nextTick(() => {if ($el.textContent == '-') {$el.style['text-align'] = 'center'}})"
    x-text="format(variable)">
</td>
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