[英]Javascript Vue 3 Extract data from an array and display into a table
大家好,希望你们有美好的一天,
我在 Vue Js 上设置了一个前端应用程序,该应用程序与连接到 mysql 数据库的 express.js api 链接。 我使用 axios 发送 http 请求。 我能够从我的 mysql 数据库中获取数据并将其控制台记录到前端系统中。 但是我正在努力从数组中提取数据并将其显示到表格中。
这是桌子。
<table class="table">
<thead>
<tr>
<th scope="col">#ID</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">email</th>
<th scope="col">DOB</th>
<th scope="col">Country Of Residence</th>
<th scope="col">Phone Number</th>
<th scope="col">Contact_Via Phone</th>
<th scope="col">Contact Via Email</th>
<th scope="col">Contact Via Mail</th>
<th scope="col">Sign up date</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" v-for="name in newData" :key="newData.id"></th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
这是我在创建页面时从数据库中获取数据的地方。
async created() {
try {
const getUserData = await axios.get('/api/getstudentdata', {})
.then(getUserData => getUserData.data)
{
this.newData = JSON.parse(JSON.stringify(getUserData))
for( let i = 0; i < this.newData.length; i++) {
console.log(this.newData[i].Name)
}
}
}catch(error) {
console.log(error)
}
},
在这里,我添加了我的控制台日志和 vue 开发工具所说的内容。 https://imgur.com/a/eIHDq6u控制台日志:
roxy {0: {…}, 1: {…}, 2: {…}}
[[Handler]]: Object
[[Target]]: Array(3)
0: {ID: 50, Name: 'Bob', Surname: 'Bobbob', Email: 'bob@gmail.com', DOB: '2000-07-15', …}
1: {ID: 51, Name: 'Tony', Surname: 'Scrub', Email: 'badatcoding@gmail.com', DOB: '2000-07-15', …}
2: {ID: 52, Name: 'Onemoreuser', Surname: 'te4st', Email: 'testoding@gmail.com', DOB: '2000-07-15', …}
length: 3
[[Prototype]]: Array(0)
[[IsRevoked]]: false
开发工具:
newData:Array[3]
0:Reactive
ID:50
Name:"Bob"
Surname:"Bobbob"
Email:"bob@gmail.com"
DOB:"2000-07-15"
Country_of_residence:"London"
Phonenumber:"0749432423"
contact_phone:"true"
contact_email:"true"
sign_up_date:"2022-07-19T14:06:19.000Z"
Timezone:"GMT+1"
1:Reactive
2:Reactive
更新! 我设法将所有数据显示到表中,但是我不能只提取 ID 或名称……这是结果的照片。
首先,在v-for
循环中,键必须是唯一的,并且 newData.id 不是有效的键,我猜你想使用类似的东西:
v-for="item in newData" :key="item.id"
其次,你在错误的地方使用了v-for
并且你没有使用 vue 胡子来使用动态变量。 试试这个:
<tr v-for="item in newData" :key="item.id">
<td>{{item.ID}}</td>
<td>{{item.Name}}</td>
<td>{{item.Surname}}</td>
...
</tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.