【发布时间】:2017-04-30 10:05:27
【问题描述】:
我想使用 Vue 来呈现一个包含行的表格,但将单元格作为一个组件。
我已经将a working example 作为我希望看到的最终结果,并有一些与我认为我可以如何去做的代码有关:
HTML
<div id="app">
<table>
<thead>
<tr>
<th>Row</th>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, rindex) in people">
<td>{{ rindex }}</td>
<cell v-for="(value, vindex, rindex) in row" :value="value" :vindex="vindex" :rindex="rindex"></cell>
</tr>
</tbody>
</table>
<template id="template-cell">
<td>{{ value }}</td>
</template>
</div>
JS
// Register component
Vue.component('cell', {
template: '#template-cell',
name: 'row-value',
props: ['value', 'vindex', 'rindex']
// In here I would like to access value, vindex and rindex
});
// Start application
new Vue({
el: '#app',
data: {
people: [
{id: 3, name: 'Bob', age: 27},
{id: 4, name: 'Frank', age: 32},
{id: 5, name: 'Joe', age: 38}
]
}
});
这是因为,在 Vue documentation on v-for 他们展示了这个例子:
And another for the index:
<div v-for="(value, key, index) in object">
{{ index }}. {{ key }} : {{ value }}
</div>
从逻辑上思考,我应该能够在没有太多问题的情况下获取单元格组件中的行索引,但是 this is not so 因为它出现了一堆错误,表明这些值是未定义的。
如果有人能在这里指出我正确的方向,那么我将不胜感激,因为我对这个问题一无所知,但真的很想了解如何正确实施。
(注意:我想在组件中渲染单元格的原因是为了注册值的变化,see the answer 到另一个问题,如果你有兴趣我在这里问)
【问题讨论】:
标签: javascript vue.js