Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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?

share|improve this question
    
Any errors in console? Is there a <table> tag wrapping your <tr> group? – Roy J Aug 25 at 16:45
    
Nothing in the console and of course it is wrappen in a <table> and also a <tbody>. – Kenneth_H Aug 25 at 16:53

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>

share|improve this answer
    
Normally it does not make a difference if the scripts are in the <head> or if they are in the very bottom of the <body>. In your example they are part above the <table> but it should not make a difference? – Kenneth_H Aug 25 at 17:36
    
See the below answer as I was not allowed to post the code as a comment – Kenneth_H Aug 25 at 17:38
    
@Kenneth_H I have updated my snippet to use most of the markup you posted, and I must say it looks fabulous. :) However, it still all works. – Roy J Aug 25 at 17:46
    
I have now tried to implement my code by using the CDN-based scripts as you do and now it can show the hardcoded data inside the ready() function. However as soon as I add in app.js, which is a webpack compiled version of the below code we have the issue: window._ = require('lodash'); require('material-design-lite'); window.Vue = require('vue'); require('vue-resource'); require('vue-validator'); var VueMdl = require('vue-mdl'); Vue.use(VueMdl.default); Vue.http.options.emulateJSON = true Vue.debug = true – Kenneth_H Aug 25 at 18:16
    
@Kenneth_H Edit your original post to explain what you've found. – Roy J Aug 25 at 18:35

Here is complete output of my <head> with the <table> inside the <body> and with all scripts in the bottom as I have in the live code. Customers

Customers

                        <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>
                    </div>
                </div>
            </div>
        </main>
        <script type="text/javascript" src="/static/js/customer_list.js"></script>
    </body>
</html> 
share|improve this answer

It seems now to be working. Inside app.js I for reason 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.