vue.js - 通过在 Vue 中输入数字来立即更改表格的列和行
问题描述
添加并输入按钮后,我通过按下按钮实现了更改表格,但是我想一输入数字就立即更改表格的列和行。例如,如果我输入 4,我想要一个 4X4 的表。我不知道。帮我。
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
div { padding: 30px; margin: 30px auto; width: 600px;
border: 1px solid #ccc; box-shadow: 3px 3px 3px #aaa; }
table { border-collapse: collapse; margin-top: 10px; }
td { width: 50px; height: 50px; border: 1px solid gray; font-size: 20pt;
text-align: center; cursor: pointer; }
.yellow { background-color: yellow; }
</style>
</head>
<body>
<div id="app">
<input type="text" v-model.number="size" >
<button type="button" @click="change(size)">Change</button>
<table :style="{backgroundColor: color}">
<tr v-for="(row, index1) in matrix" v-bind:key="index1">
<td v-for="(value, index2) in row" v-bind:key="index2">
{{ value }}
</td>
</tr>
</table>
<h1>{{size}}</h1>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
size: 3,
matrix: [],
clicked: [],
color: "",
},
created() {
for (let r = 0; r < this.size; ++r) {
this.matrix[r] = [];
for (let c = 0; c < this.size; ++c)
this.matrix[r][c] = r * this.size + c +1;
}
},
methods:{
change(s){
this.size=s
console.log(this.size)
let arr=[]
for (let r = 0; r < this.size; ++r) {
arr[r] = [];
for (let c = 0; c < this.size; ++c)
arr[r][c] = r * this.size + c +1;
}
this.matrix=arr
}
}
})
</script>
</body>
</html>
解决方案
你可以在你的尺寸状态上使用观察者
watch 语句应该是这样的:
watch: {
size(newVal){
this.change(newVal)
}
}
您可以在下面看到一个工作示例:)
var app = new Vue({
el: '#app',
data: {
size: 3,
matrix: [],
clicked: [],
color: "",
},
created() {
this.change(this.size)
},
methods:{
change(s){
console.log(this.size)
let arr=[]
for (let r = 0; r < this.size; ++r) {
arr[r] = [];
for (let c = 0; c < this.size; ++c)
arr[r][c] = r * this.size + c +1;
}
this.matrix=arr
}
},
watch: {
size(newVal){
this.change(newVal)
}
}
})
div { padding: 30px; margin: 30px auto; width: 600px;
border: 1px solid #ccc; box-shadow: 3px 3px 3px #aaa; }
table { border-collapse: collapse; margin-top: 10px; }
td { width: 50px; height: 50px; border: 1px solid gray; font-size: 20pt;
text-align: center; cursor: pointer; }
.yellow { background-color: yellow; }
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model.number="size" >
<table :style="{backgroundColor: color}">
<tr v-for="(row, index1) in matrix" v-bind:key="index1">
<td v-for="(value, index2) in row" v-bind:key="index2">
{{ value }}
</td>
</tr>
</table>
<h1>{{size}}</h1>
</div>
</body>
</html>
推荐阅读
- javascript - 如果您没有文档参考,是否可以将 StartAt 与 Firestore 查询一起使用
- java - 我们可以将 Spark 流用于基于时间的事件吗
- swift - 如何使用coredata从上到下对tableview项目进行排序
- mysql - 如何在mysql中更改日期格式
- java - 如果我们定义 WebDriver 驱动程序,则在使用 TestNG 时;全局然后不创建该类的对象,我们如何能够在方法下使用驱动程序?
- android - 单击列表视图项时从 SQLite 数据库中获取行 ID
- hadoop - SemanticException 10072: 数据库不存在 (Sqoop)
- python - 如何在python中检测具有nan值的字符串
- javascript - 如何正确运行 firebase 功能?
- dart - Flutter 滚动视图的第一个小部件最初居中对齐,但所有后续选定的小部件也居中对齐