【问题标题】:Do not update the array value bind with an input field inside vue component不要使用 vue 组件内的输入字段更新数组值绑定
【发布时间】:2020-04-20 05:03:42
【问题描述】:

在这里,我在组件内正确呈现数据。但是每当我更改需要绑定的数组中未更新的任何输入值时。我无法使用rows 数组对值进行双向绑定。请帮忙。提前致谢。

Vue.component('refund-table', {
  template: `
      <div> 
          <div class="table-responsive">
                  <table class="table table-bordered table-hover table-striped">
                  <thead>
                      <tr>
                          <th v-for="column in columns"> {{column}}</th>
                      </tr>
                  </thead>
                  <tbody>
                      <tr v-for="(row,index) in rows">
                          <td >#{{index+1}}</td>
                          <td v-for="(item,ind) in row">
                              <input v-if="ind>0" :value="item">
                              <div v-else>{{item}}</div>
                          </td>
                      </tr>
                  </tbody>
                  <tfoot>
                      <tr>
                          <td></td><td></td><td style="text-align: right">Total</td><td>{{totalRefund}}</td>
                      </tr>
                  </tfoot>
              </table>
              </div>
      </div>
      `,
  props: {
    columns: Array,
    rows: Array,
    caption: String,
    edit: Boolean,
  },
  data() {
    return {}
  },

  mounted() {},
  methods: {},
  computed: {
    totalRefund: function() {
      return 100;
    }
  }
})

new Vue({
  el: "#app",
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<refund-table id="app" :columns='["#Serial","a","b","c"]' :rows="[[1,2,3],[1,2,3],[1,2,3],[1,2,3]]" :footer='["a","b","c"]'></refund-table>

【问题讨论】:

  • 你是如何测试它没有更新rows数组的?您应该在帖子中提及所有详细信息。
  • 假设您将任何输入值 3 更改为 5,然后在 vue devtools 中您需要在该数组中看到 5 而不是 3。我将该行中的输入绑定&lt;input v-if="ind&gt;0" :value="item"&gt;

标签: vue.js vue-component 2-way-object-databinding


【解决方案1】:

在下面使用 v-model 来绑定新输入的数据并写入 计算中的函数 updateArrayData 所以只要有任何 更改,它将返回数组中新添加的值。

Vue.component('refund-table', {
  template: `
      <div> 
          <div class="table-responsive">
                  <table class="table table-bordered table-hover table-striped">
                  <thead>
                      <tr>
                          <th v-for="column in columns"> {{column}}</th>
                      </tr>
                  </thead>
                  <tbody>
                      <tr v-for="(row,index) in rows">
                          <td >#{{index+1}}</td>
                          <td v-for="(item,ind) in row">
                 <input @input="updateArrayData(row, index)" v-model="input_data" v-if="ind>0" :value="item">
                              <div v-else>{{item}}</div>
                          </td>
                      </tr>
                  </tbody>
                  <tfoot>
                      <tr>
                          <td></td><td></td><td style="text-align: right">Total</td><td>{{totalRefund}}</td>
                      </tr>
                  </tfoot>
              </table>
              </div>
      </div>
      `,
  props: {
    columns: Array,
    rows: Array,
    caption: String,
    edit: Boolean,
  },
  data() {
    return {
        input_data:''          // Add the input_data variable in your data
    }
  },

  mounted() {},
  methods: {},
  computed: {
    totalRefund: function() {
      return 100;
    },
    // Add the updateArrayData function in your computed here.
    updateArrayData: function(row, index){  
        if(input_data){
            return row[index] = this.input_data;
        }
    }
  }
})

new Vue({
  el: "#app",
})

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<refund-table id="app" :columns='["#Serial","a","b","c"]' :rows="[[1,2,3],[1,2,3],[1,2,3],[1,2,3]]" :footer='["a","b","c"]'></refund-table>

【讨论】:

  • 还有一个简单的方法我也发现v-model应该用作v-model=rows[index][ind]
猜你喜欢
  • 1970-01-01
  • 2021-02-09
  • 2020-09-24
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多