Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
update x-for from Ajax - Alpine Js

How can I tell Alpine to update the x-for from the server. In another word, How to do force update in AlpineJs.

<select id="branches" name="branches" x-data="ajax" >
    <option value="" disabled selected="">Choose</option>
    <template x-for="branche in branches">
        <option :value="branche.ID" x-text="branche.Branch_name"></option>
    </template>
</select>
    document.addEventListener('alpine:init', () => {
        Alpine.data('ajax', () => ({
            async branches() {
                return await $.post("path-url", {
                    tb: "branches"
                }, (data) => {
                    return data;
                });
            }
        }))
    })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming $.post is from jQuery:

<select id="branches" name="branches" x-data="getBranches()">
  <option value="" disabled selected="">Choose</option>
  <template x-for="branch in branches">
    <option :value="branch.ID" x-text="branch.Branch_name"></option>
  </template>
</select>

<script>
document.addEventListener('alpine:init', () => {
  Alpine.data('getBranches', () => ({
    branches: [],

    init() {
      $.post("path-url", {
        tb: "branches"
      }, (data) => {
        this.branches = data
      })
    }
  }))
})
</script>

We used the init() method to get the data, that executes before rendering the component. You don't have to "force" the update, just mutate a reactive Alpine.js data variable and Alpine.js will update the DOM shortly after.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda