【问题标题】:Selecting a single row is Selecting all other rows in Vue.js选择单行就是选择 Vue.js 中的所有其他行
【发布时间】:2017-10-17 11:04:15
【问题描述】:

我正在计算估计成本。选择产品获取有关产品的详细信息并在输入框中显示其描述和价格。然后单击添加按钮后,将出现一个新行以供另一个选择。但问题是新行与旧行数据一起出现。更改单行会影响所有其他行。到目前为止,这是我的代码:

<tbody v-for="row in rows" :key="index">
    <tr>
        <td>
            <select @change="selected" v-model="product_id" class="form-control" name="product_id" id="product_id">
                <option value="">Select Product</option>
                <option v-for="item in items" :value="item.id" v-text="item.product_name"></option>
            </select>                                           
        </td>
        <td>
            <textarea type="text" v-model="product.product_details" name="product_details" id="product_details" class="form-control" rows="1" placeholder="Product Details"></textarea>
        </td>
        <td>
            <input v-model.number="product.product_sellPrice" type="number" step="any" class="form-control" name="rate" id="rate" placeholder="Rate">
        </td>
        <td>
            <input v-model.number="product.product_tax" type="number" step="any" class="form-control" name="tax" id="tax" placeholder="Tax">
        </td>
        <td>
            <input v-model="quantity" type="number" class="form-control" name="quantity" id="quantity" placeholder="Quantity">
        </td>
            
        <td> 
            <input :value="total" type="number" step="any" class="form-control" name="total" id="total" placeholder="Total Price">
        </td>
        <td>
            <button class="btn btn-secondary" v-on:click="addrow" >
                <i class="fa fa-plus" aria-hidden="true"></i>
            </button>
            <button v-show="length > 1" class="btn btn-secondary" @click.prevent="removerow(index)">
                <i class="fa fa-minus" aria-hidden="true"></i>
            </button>
        </td>
    </tr>   
</tbody>

Vue部分

<script>
    export default{
        props: ['products'],

        data() {

            return {
                rows: [{

                }],
            
                items: this.products,
                product: '',
                product_id: '',
                quantity: '',
                index: '',
                total_price: '',
            }
        },
            

        methods: {
            addrow: function (event) {
                event.preventDefault();
                this.rows.push({
                });
            },

            removerow: function (index) {
                this.rows.splice(index, 1);
            },

            selected(e){
                var id = this.product_id;
                console.log(id);
                axios.get('/estimate/product/' + id)
                  .then((response)=>{
                        this.product = '';
                        this.product = response.data;
                  })
                  .catch((error) => {
                        console.log(error);
                  }); 
            },  
        }
    }
</script>  

我哪里做错了?

【问题讨论】:

  • 这是我项目的一部分。但我认为如果不从项目中的数据库中获取数据,它就没有其他连接。这是我这部分的完整代码,我认为我在选择行时做错了
  • 获取运行在 jsfiddle.net 中的代码并在此处发布,它会更容易帮助您。如何创建最小、完整和可验证的示例:stackoverflow.com/help/mcve
  • 它在 JSFiddle 中不起作用。但我把代码缩短了。
  • 通过创建一个最小的示例可以帮助故障排除,代码不必反映您的实际服务。我确实认为一个问题是,当您添加一个完全为空的新行时,添加已定义但为空的参数的新行,如下所示: this.rows.push({'Param1':''});

标签: vue.js vuejs2


【解决方案1】:

key 应该是每一行的唯一标识符。您在data 对象中定义了index,并将其用作所有行的键,这会导致问题。

此外,不建议将行索引用作键,因为它会在您添加/删除行时发生变化。

所以你需要为每个行项添加一些标识符并将其用作键,这里有一个简单的例子来说明如何做到这一点:

<div id="vue-instance">
  <ul>
    <li v-for="(index,item) in inventory" :key="item.name">
      {{ item.name }} - ${{ item.price }}
      <button @click="add">Add</button>
      <button @click="remove(index)">Remove</button>
    </li>
  </ul>


</div>

var vm = new Vue({
  el: '#vue-instance',
  data: {
    counter: 1,
    inventory: [{
      name: 'MacBook Air',
      price: 1000
    }, {
      name: 'MacBook Pro',
      price: 1800
    }, {
      name: 'Lenovo W530',
      price: 1400
    }, {
      name: 'Acer Aspire One',
      price: 300
    }]
  },
  methods: {
    add: function() {
      this.inventory.push({
        name: 'item' + this.counter++,
        price: this.counter * 1000
      })
    },
    remove: function(index) {
      this.inventory.splice(index, 1);
    }
  }
});

https://jsfiddle.net/1rancd5g/

【讨论】:

  • 感谢您的回答。我知道你的做法。但问题是我有选择选项,在选择一个项目后,我发送一个 axios 请求以获取项目的属性。我
  • 根据您的建议,新代码是这样的:jsfiddle.net/5phq111c/2 但问题仍然存在。代码哪里出了问题?
  • 您发布的新代码也有同样的问题,您使用的键为row.id,但行没有id。将 id 属性添加到行。您可以在这里查看如何生成 id:stackoverflow.com/questions/105034/…
  • 我可以使用row.product_id作为key吗?我已经将 product_id 与 row 绑定,并通过产品 id 发送 axios 请求以获取值。但这不起作用。我收到错误 404。如何捕获 id 并发送请求?小提琴是jsfiddle.net/5phq111c/4
  • 你可以绑定任何唯一的东西,关于404,这意味着服务器不知道你正在调用的url
猜你喜欢
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多