3

Having some issues printing array data to a table using Vue. Can someone help me parse the values using vue and put them in a table. See code below image. Without the array of 2 it would work but I'm not sure how to with the response being multiple.

enter image description here

This is my function in due

//HTML CODE

     <tbody>
        <tr v-for="(input, index) in inputs">
          <th>((input.id))</th>
          <th>((input.tracking_number))</th>
          <td>((input.first_name))</td>
          <td>((input.last_name))</td>
          <td>((input.weight))</td>
          <td>((input.description))</td>
          <td>((input.courier))</td>
        </tr>
    </tbody>

//end HTML

//Vue Code

      var app = new Vue({
        el: '#app',
       data: {
       inputs: [],
       form: {
        scanval: null
       }
       },
       methods: {
        updatetable() {
        this.$http.get('someroute', {params:  {page: this.form}})
        .then(response => {
          if (response.body != "null") {
            console.log(response);
            this.inputs.push({
              id: response.body.id,
              tracking_number: response.body.tracking_number,
              first_name: response.body.first_name,
              last_name: response.body.last_name,
              weight: response.body.weight,
              description: response.body.description,
              courier: response.body.courier
            })
            this.form.scanval = ""
          } else {
            this.form.scanval = "",
            alert("No items found")
          }
        }, response => {
          alert("no item found");
        });
    },

1 Answer 1

2

Just set inputs equal to the response body.

this.inputs = response.body

This will replace the current value of inputs with the response. If you would like to add the response to inputs then you can concatenate the response to inputs:

this.inputs = this.inputs.concat(response.body)
Sign up to request clarification or add additional context in comments.

2 Comments

On the off-chance that inputs already has some data (since OP is using Array.prototype.push in their code), maybe try this.inputs = this.inputs.concat(response.body)
@Phil Sure :) Added.

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.