For a project, I need to loop through a list of objects given in javascript, and display them horizontally in a html table. Example here: https://jsfiddle.net/50wL7mdz/83227/
html:
<div id="app">
<table>
<thead><tr><td colspan='5'>{{body.title}}</td></tr></thead>
<tbody>
<tr>
<template v-for='car in body.cars'>
<td>{{car.make}}</td>
<td>{{car.model}}</td>
<td>{{car.year}}</td>
</template>
</tr>
</tbody>
</table>
</div>
javascript:
new Vue({
el: '#app',
data: {
body: {title : 'test title',
cars: [{make: 'Honda', model: 'Civic', year: 2010},
{make: 'Toyota', model: 'Camry', year: 2012},
{make: 'Nissan', model: 'Versa', year: 2014}]}
}
})
In the actual project, the length of "cars" in unknown so looping is unavoidable. You can see the example works fine in Chrome and Firefox, but not working in IE.
After contacting Vue dev team, they informed me that template tag simply isn't accepted in "tr" of IE, and I need to use string based templates instead. However after experimenting with Vue components, turns out Vue also doesn't allow multiple root elements in a template. Link to Vue ticket here (closed): https://github.com/vuejs/vue/issues/7243
What would be a good way to do this and make it work on IE as well?
Evan gives the answer in the issue. Use a string template.
Ugly as that looks it does work in IE. You could also write a render function.
Evan picked up that you were declaring the component as html and then mounting Vue to it. That is the problem with IE11: the browser first processes the html before knowing anything about Vue and reaches a critical error when it reaches the
template
tag, before going on to process the js. In order to make IE process the template tag you have to give it to the browser from Vue, so Vue can do the interpreting. This is why a string-based template is recommended: Vue takes the template as a string and then gives the browser HTML to display.Then as you've picked up, Vue can only have one root element for a template. The solution is to keep backing out of the DOM tree until you have one root element. In this case I propose just making the entire table the template. Then you would have:
javascript:
and html:
I've updated the jsfiddle to reflect this.
EDIT: don't read this one, it is only accurate for Vue 1!
I don't think you're going to be able to do this currently. IE simply does not allow template tags, so the only way to do this is using
<tr is="component-name">
and have a separate component (with a single root element) which can be applied to thetr
ortd
. Looping through each element and adding 3td
s per cannot be done.Again, using the
is
attribute on atr
ortd
is the current solution, but that does not allow for multi-root components as you have requested. Perhaps you can create a component for each car and do<td is="car-component">
and then style the TD to look like 3 columns.