简体   繁体   English

从Vue.js和Vuetify.js中的函数获取表中的数据

[英]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. }这是被调用的函数的示例,其他所有函数都是相似的。

What i see 我所看到的

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.

相关问题 无法从自定义自动完成搜索栏 (Vue.js/Vuetify.js) 中 select 项目 - Can't select item from custom autocomplete search bar (Vue.js/Vuetify.js) 使用编译封装的 Vue.js + Vuetify.js 组件构建微前端 - Building micro frontends with compiled encapsulated Vue.js + Vuetify.js Components vuetify.js 在 Vue 3 中不起作用(主题定制) - vuetify.js doesn't working in Vue 3 (theme customzing) 如何在Vue应用程序(使用Vuetify.js)中实现验证的简单表单? - How to implement a simple form with validation in a Vue app (with Vuetify.js)? 如何将数据表值调用到console.log Vue.js(vuetify) - How to call data table value to console.log Vue.js (vuetify) Vuetify.js 数据表固定标题通过 CSS 滚动 - Vuetify.js data-table fixed-header scrolling via CSS 如何访问 vuetify.js 2 中 v-data-table 之外的总项目? - How to access total items outside v-data-table in vuetify.js 2? 如何在 v-data-table Vue.js + Vuetify 中显示和隐藏行 - How to show and hide rows in v-data-table Vue.js + Vuetify Vue.js Vuetify.js-未知的自定义元素: <v-list-item> , <v-list-item-title> 您是否正确注册了组件? - Vue.js Vuetify.js - Unknown custom element: <v-list-item>, <v-list-item-title> did you register the component correctly? 通过vue.js将模式形式的数据添加到表中 - Adding data from modal form to the table via vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM