0

I'm trying to find a solution with for a table that is populated with $.ajax(), but i can't figure out how i can do this with Vue.js. How can I do that? Do i need a Vue component for that?

HTML:

<div class="row">
<div class="col-md-12 overflow-table">
        <table class="table" id="table">
        <thead class="head-color thead-inverse">
            <tr>
                <th style="border-top-left-radius: 10px; border-left:1px solid transparent;">NAME</th>
                <th>CLIENT-ID</th>
                <th>URL</th>
                <th style="border-top-right-radius: 10px; border-right:1px solid transparent;">ACTIONS</th>
            </tr>
        </thead>

        <tbody id='table-redirect'>
            <tr class='lightgrey'>
            </tr>
            <tr class='lightgrey'>
            </tr>
            <tr class='lightgrey'>
            </tr>
            <tr class='lightgrey'>
            </tr>
        </tbody>
    </table>
</div>

VUE.JS SCRIPT:

    //VUE.JS REDIRECT PAGE

//VARIABLES
var url = "http://mobisteinlp.com/redirect/redirect";
agr = 0;

//VUE.JS REDIRECT VIEW MODEL
var app = new Vue({
  el: '#redirect',
  delimiters:['{', '}'],

  data: {
    agr1:[]
  },

  methods: {

  //FUNCTION TO DISPLAY TABLE ON PAGE (RE)LOAD
      getAll: function() {
        console.log('teste');
        $.ajax({
            url: url + "/getAll",
            type: "POST",
            dataType: "json",
            success:function(response){
              console.log(response);//
              this.agr1=response;
              console.log(this.agr1);
              console.log('success!');
            },
            error:function(){
                console.log('error');
            }//end error function
        });//end $.ajax() request
      },//end getAll function
  }//end methods
})//end vue.js instance

1 Answer 1

2

Use the <tr> like a list. Add a v-for="agr in agr1" then you can iterate over the properties you want. when agr1 gets updated it'll render a new list of rows. You can also use v-bind:key="agr.property" to make it so that Vue efficiently renders the elements that get reused.

    <tbody id='table-redirect'>
        <tr 
          v-for="agr in agr1"
          v-bind:key="agr.id"
          class='lightgrey'
        >
            <td>{{ agr.name }}</td>
            <td>{{ agr.client_id }}</td>
            <td>{{ agr.url }}</td>
            <td>{{ agr.actions }}</td>
        </tr>
    </tbody>
Sign up to request clarification or add additional context in comments.

9 Comments

Btw, the actions section has 2 buttons with <img>. I know how could I use it with jQuery, but to put a working button with vue.js? the buttons are also dynamic like the data. And when I click the edit button, it edits the content of the selected row.
It wouldn't be hard so long as each object has the dynamic data needed. <td><img src="action1.png" v-on:click="doFunction1(agr)"><img src="action2.png" v-on:click="doFunction2(agr)"></td>
Ah! I understand now! :) another question, after some search, shouldn't the ajax function be inside mounted() instead of methods: {}?
Keep it in methods and call it in mounted that way if you want to have the ability to update the information you can just call the getAll function
mounted: function() { getAll(); }, ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.