0

I am building a web application using vue.js, vue-resource, vue-mdl and google material design lite. JS compilation is performed using webpack through laravel elixir. In this app I have to render a table row for each object from an array returned from a Rest API (Django Rest Framework). I have made the following code inside the html to render content using vue.js:

<tr v-for="customer in customers">
    <td class="mdl-data-table__cell--non-numeric">{{ customer.status }}</td>
    <td class="mdl-data-table__cell--non-numeric">{{ customer.name }}</td>
    <td class="mdl-data-table__cell--non-numeric">{{ customer.company }}</td>
<tr>

This should render all objects in the array as a table row. I have also tried to wrap the above in a template tag like this:

<template v-for="customer in customers">
<tr>
    <td class="mdl-data-table__cell--non-numeric">{{ customer.status }}</td>
    <td class="mdl-data-table__cell--non-numeric">{{ customer.name }}</td>
    <td class="mdl-data-table__cell--non-numeric">{{ customer.company }}</td>
</tr>
</template>

This did not change either. I have also tried to hardcode the array inside the ready() function of the vue instance, but this did not help either.

window._ = require('lodash');
require('material-design-lite');
window.Vue = require('vue');
require('vue-resource');
var VueMdl = require('vue-mdl');
Vue.use(VueMdl.default);
const app = new Vue({
    el:'body',
    ready: function(){
        //this.getCustomerList();
        this.customers = [
            { name: "Peter", status: "true", company: "Company 1"},
            { name: "John", status: "false", company: "Company 2"}
        ]

    },
    data: {
        customers: [],
        response: null,
        messages: []
    },
    methods: {
        getCustomerList: function()
        {
            this.$http({url: '/api/customers/', method: 'GET'}).then(function(response){
                //Success
                this.customers = response.data
                console.log(response.data)
            },
            function(response){
                console.log(response)
            })
        }
    }
})

Changing the above to this does not change either:

window._ = require('lodash');
require('material-design-lite');
window.Vue = require('vue');
require('vue-resource');
var VueMdl = require('vue-mdl');
Vue.use(VueMdl.default);
const app = new Vue({
    el:'body',
    ready: function(){
        //this.getCustomerList();

    },
    data: {
        customers: [
            { name: "Peter", status: "true", company: "Company 1"},
            { name: "John", status: "false", company: "Company 2"}
        ],
        response: null,
        messages: []
    },
    methods: {
        getCustomerList: function()
        {
            this.$http({url: '/api/customers/', method: 'GET'}).then(function(response){
                //Success
                this.customers = response.data
                console.log(response.data)
            },
            function(response){
                console.log(response)
            })
        }
    }
})

I have also tried to just make a plain html table that does not have any of the Google MDL classes applied, and this does also not give any result. Logging this.customers to the console shows that it does in fact contain the data, but for reason it is not rendering. Why is that? What am I doing wrong?

Kenneth_H
  • 141
  • 2
  • 13

2 Answers2

1

Here's a snippet of your code, which works as expected. I've added in CDN references to the libraries you mentioned, but I'm not doing anything with them. I offer this as a starting point for you to see if you can find what changes will reproduce your problem here.

const app = new Vue({
  el: 'body',
  ready: function() {
    //this.getCustomerList();
    this.customers = [{
      name: "Peter",
      status: "true",
      company: "Company 1"
    }, {
      name: "John",
      status: "false",
      company: "Company 2"
    }]
  },
  data: {
    customers: [],
    response: null,
    messages: []
  }
})
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/vue-resource/0.9.3/vue-resource.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.2.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.2.0/material.min.js"></script>
<script src="https://rawgit.com/posva/vue-mdl/develop/dist/vue-mdl.min.js"></script>
<div class="mdl-grid">
  <div class="mdl-cell mdl-cell--3-col">
    <button id="create-customer" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" @click="$refs.createCustomer.open">
      Create customer
    </button>
  </div>
  <div class="mdl-cell mdl-cell--3-col">
    <button id="convert-reseller" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect">
      Convert to reseller
    </button>
  </div>
  <div class="mdl-cell mdl-cell--3-col">
    <button id="remove-customer" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect">
      Remove Customer
    </button>
  </div>
  <div class="mdl-cell mdl-cell--3-col">
    <button id="change-status" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect">
      Change status
    </button>
  </div>
</div>
<div class="mdl-grid">
  <div class="mdl-cell mdl-cell--12-col">
    <table class="mdl-data-table mdl-js-data-table mdl-data-table--selectable" style="width:100%;">
      <thead>
        <tr>
          <th class="mdl-data-table__cell--non-numeric">Status</th>
          <th class="mdl-data-table__cell--non-numeric">Customer name</th>
          <th class="mdl-data-table__cell--non-numeric">Company</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="customer in customers">
          <td class="mdl-data-table__cell--non-numeric">{{ customer.status }}</td>
          <td class="mdl-data-table__cell--non-numeric">{{ customer.name }}</td>
          <td class="mdl-data-table__cell--non-numeric">{{ customer.company }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
Roy J
  • 42,522
  • 10
  • 78
  • 102
0

It seems now to be working. Inside app.js I had:

const app = new Vue({ el: 'body' })

That, for some reason, conflicted with the one I was creating inside customer_list.js—although my methods worked fine.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Kenneth_H
  • 141
  • 2
  • 13