javascript - 带有 Vue(2) 绑定的 HTML 表格行
问题描述
我有一个购物车/订单列表,我绑定到一个工作正常的数组。
我最近为添加到数组中的每个项目添加了一个行号,还有一个按钮行。问题是我像这样将它们绑定到当前行
<div id="table">
<table class="table table-sm table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Product</th>
<th scope="col">Type</th>
<th scope="col">Attribute</th>
<th scope="col">Height</th>
<th scope="col">Width</th>
<th scope="col">Price</th>
<th width="1%" scope="col">Remove</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in table_products">
<td>{{ index +1 }}</td>
<td>{{ item.product }}</td>
<td>{{ item.type }}</td>
<td>{{ item.attribute }}</td>
<td>{{ item.height }}</td>
<td>{{ item.width }}</td>
<td>{{ item.price }}</td>
<td><button class="btn btn-danger" @click="deleteRow(index)">X</button></td>
</tr>
</tbody>
</table>
</div>
</div>
我从来没有遇到过问题,因为绑定的项目值为空,因此从未创建该行。但是现在索引和按钮创建了表格的第一行。
有没有办法可以在页面加载时清除数组,还是我走错了路?
编辑:为上下文添加了我的 vue 组件
var table_products = new Vue ({
el : '#table',
data : {
table_products : [{
}]
},
解决方案
我初始化 table_products [{}]
而不是[]
从初始化开始,它在数组中有一个空对象,因此它有一个元素。初始化一个空数组反而解决了这个问题,因为它没有元素。
感谢评论中的菲尔
推荐阅读
- python - 结合两个熊猫数据框而不包含重复项?
- hibernate - 在 Spring Boot 应用程序中,Hibernate SQL 语句没有使用 log4j 登录到单独的日志文件中
- algorithm - 查找数组的所有可能子数组的时间复杂度
- jquery - Vuejs读取一个文件夹下的所有文件
- ios - 如何在 HereMaps 中实现多条路由?
- reactjs - 如何修复无法在“HTMLInputElement”上设置“value”属性以进行反应?
- ionic-framework - 未检测到匹配的服务人员。您可能需要重新加载页面
- java - S3 文件作为 PDFBox 的输入
- javascript - 正则表达式计算文本缩进中空格和制表符的数量
- c# - 如何在 Visual Studio 中为 Excel 创建 GUI 插件?