0

I'm trying to bind a json array to a table. Therefor I'm using v-for on a <tr> tag.

<tr v-for="file in files">
    <td>@{{ file.name }}</td>
    <td>@{{ file.hash }}</td>
    <td>@{{ file.size }}</td>
    <td>@{{ file.download_count }}</td>
    <td>@{{ file.id }}</td>
</tr>

The file is bound through vue, which loads from a JSON.

<script>
    var url = '{{ route('getAllFiles') }}';
    new Vue({
        el: '#fileTable',

        data: {
            files: null
        },

        created: function(){
            this.fetchData();
        },
        methods: {
            fetchData: function(){
                var self = this;
                $.get(url, function(data){
                    self.files = data;
                    console.log(data);
                });
            }
        }
    });
</script>

The JSON looks like this: http://pastebin.com/tNy7A4Yw so that should be correct. The console.log(data) I've used inside of fetchData() looks exactly like the API Output, so I don't see any error.

1
  • Do you getting any console error ? Try to set property dataType: 'json' to $.get method. Edit: Sorry, I see now you said there are no console errors. Commented Feb 27, 2017 at 21:55

2 Answers 2

2

i think you should set the initial files data as an empty array not null because v-for will render a list based on an array. read here .

Sign up to request clarification or add additional context in comments.

1 Comment

exacly what you should do, can confirm.
0

So using the Vue Dev Tools I saw that my JSON Output was escaped in the files array.

I used a $.parseJSON like so:

new Vue({
        el: '#fileTable',

        data: {
            files: [],
            filesAreReady: false
        },

        created: function(){
            this.fetchData();
        },
        methods: {
            fetchData: function(){
                var self = this;
                $.get(url, function(data){
                self.files = $.parseJSON(data);
                self.filesAreReady = true;
            });
        }
    }
});

Now it works.

Comments

Your Answer

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