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

384
Views
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;
                });
            }
        }))
    })
over 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

over 4 years ago · Santiago Trujillo 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!