【问题标题】:To immediately change columns and rows of a table by entering numbers in the Vue通过在 Vue 中输入数字来立即更改表格的列和行
【发布时间】:2021-06-12 17:20:33
【问题描述】:

添加并输入按钮后,我实现了按按钮更改表格,但我想一输入数字就立即更改表格的列和行。例如,如果我输入 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>

【问题讨论】:

  • 只要我输入号码然后添加一个@input="change(size)"&lt;input 也不需要this.size=s

标签: vue.js


【解决方案1】:

你可以在你的尺寸状态上使用观察者

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2017-12-22
    • 1970-01-01
    相关资源
    最近更新 更多