[英]For data in a table from a function in Vue.js and Vuetify.js
I'm trying to output a table, but it's printing just one row. 我正在尝试输出一张表,但它只打印一行。 How can i fix it? 我该如何解决? I've tried different things (Vanilla JS worked). 我尝试了不同的方法(Vanilla JS工作了)。 It's for study purposes and i'm kinda new in JS and Vue.js in general. 这是出于学习目的,我一般来说在JS和Vue.js中都是新手。 This is the table: 这是表:
<template>
<v-data-table
:headers="headers"
:items="houses"
class="elevation-1"
>
<template v-slot:items="data">
<td class="text-xs-left">{{ data.item.name }}</td>
<td class="text-xs-left">{{ data.item.party }}</td>
<td class="text-xs-left">{{ data.item.state }}</td>
<td class="text-xs-left">{{ data.item.seniority }}</td>
<td class="text-xs-left">{{ data.item.votes }}</td>
</template>
</v-data-table>
</template>
This is the header: 这是标题:
headers: [
{ text: 'Full Name', value: 'name' },
{ text: 'Party', value: 'party' },
{ text: 'State', value: 'state' },
{ text: 'Seniority', value: 'seniority' },
{ text: '% Votes', value: 'votes' }
],
houses: [
{
name: getNameHouse(),
party: getPartyHouse(),
state: getStateHouse(),
seniority: getSeniorityHouse(),
votes: getVoteHouse()
},
]
This is the header: 这是标题:
function getPartyHouse (){
var JSONhouse = data_house,
jHouseLen = JSONhouse.results[0].num_results;
for (var i = 0; i < jHouseLen; i ++ ){
return JSONhouse.results[0].members[i].party;
}
} This is the sample of the function that is called, and every other function is similar. }这是被调用的函数的示例,其他所有函数都是相似的。
From my comment: 根据我的评论:
From what I can tell, the object containing your data, houses, is an array of objects with a length of 1. This means that your table will only end up printing 1 row containing the data that you provided. 据我所知,包含您的数据的对象(房屋)是长度为1的对象数组。这意味着您的表最终只会打印出包含所提供数据的1行。 I'd recommend building your full set of data prior to creating the table. 建议您在创建表格之前先构建完整的数据集。
Here's a an example that may help clarify what I was attempting to convey in my comment. 这是一个示例,可能有助于阐明我在评论中试图传达的内容。 Note that it's not perfect and may need to be modified depending on the structure of your input. 请注意,它并不完美,可能需要根据输入的结构进行修改。
data: () => { return { headers: [{ text: 'Full Name', value: 'name' }, { text: 'Party', value: 'party' }, { text: 'State', value: 'state' }, { text: 'Seniority', value: 'seniority' }, { text: '% Votes', value: 'votes' }], // Only initialize the container for our data, // it will be populated once the component is created houses: [] } }, created: () => { // Get the data that we want to pull our houses from let input = data_house.results[0].members // Loop through our data input.forEach((member) => { // At each iteration, pull the relevent data and append it to our houses array houses.push({ name: member.name, party: member.party, state: member.state, seniority: member.seniority, votes: member.votes, }) }) }
<v-data-table :headers="headers" :items="houses" class="elevation-1"> <template v-slot:items="data"> <td class="text-xs-left">{{ data.item.name }}</td> <td class="text-xs-left">{{ data.item.party }}</td> <td class="text-xs-left">{{ data.item.state }}</td> <td class="text-xs-left">{{ data.item.seniority }}</td> <td class="text-xs-left">{{ data.item.votes }}</td> </template> </v-data-table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.