简体   繁体   English

Vue.js - 表格得到更新,但选中的复选框数据没有

[英]Vue.js - Table gets updated but selected checkbox data does not

I've ran into a bug that I can't seem to solve.我遇到了一个似乎无法解决的错误。 I have a table with selectable rows.我有一个可选择行的表。 When a checkbox is checked, the amount column is summed up.当一个复选框被选中时,金额列被汇总。

But, when the data in the table changes using a datepicker, the merchant total on the right and the amount in the selected checkboxes object do not update and reflect what is in the table.但是,当使用日期选择器更改表中的数据时,右侧的商户总数和所选复选框对象中的金额不会更新并反映表中的内容。

Below are two images illustrating what's happening.下面是两张图片,说明了正在发生的事情。

Here is a codepen: https://codepen.io/anon/pen/NmKBjm这是一个代码笔: https ://codepen.io/anon/pen/NmKBjm

Solved the issue but I believe there's a more optimal method using v-model but I just can't get it to work.解决了这个问题,但我相信使用 v-model 有一种更优化的方法,但我无法让它工作。 I'm basically fetching the data and then re-assigning the selected object with the data.我基本上是在获取数据,然后用数据重新分配选定的对象。

 this.volume = response.data; this.total = this.volume.reduce(function(p, n) { return p + parseFloat(n.amount); }, 0); let merchants = this.selected.map(m => m.merchantName); let filterMerchants = this.volume.filter(e => e ? merchants.includes(e.merchantName) : null); this.selected = filterMerchants; this.onCheckboxChange();

在此处输入图像描述

And here is my code.这是我的代码。

 <v-data-table v-model="selected" id="transactions-volume-table" :headers="tableHeaders" :items="volume" item-key="merchantName" :loading="loading" :search="searchTable" hide-actions class="elevation-1"> <v-progress-linear slot="progress" color="blue" indeterminate></v-progress-linear> <template v-slot:items="props"> <td><v-checkbox v-model="props.selected" @change="onCheckboxChange" primary hide-details></v-checkbox></td> <td class="text-xs-left">{{ props.item.divisionName }}</td> <td class="text-xs-left">{{ props.item.merchantName }}</td> <td class="text-xs-left">£{{ props.item.amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") }}</td> </template> </v-data-table> data() { return { search: { fromDate: new Date().toISOString().substr(0, 10), toDate: new Date().toISOString().substr(0, 10) }, fromDateModal: false, toDateModal: false, searchTable: '', loading: true, volume: [], total: null, merchantTotal: 0, tableHeaders: [{ text: 'Select', sortable: false }, { text: 'Division', value: 'divisionName', sortable: true }, { text: 'Merchant', value: 'merchantName', sortable: true }, { text: 'Amount (£)', value: 'amount', sortable: true }], selected: [] } } onCheckboxChange() { console.log(this.selected); this.merchantTotal = this.selected.reduce(function(p, n) { return p + parseFloat(n.amount); }, 0); }

You update the merchant total only if a checkbox changes.仅当复选框更改时,您才更新商户总数。 You should update the merchant total in case your dates change as well.如果您的日期也发生变化,您应该更新商家总数。 Because the date change it does not trigger a checkbox to change event.因为日期更改它不会触发复选框来更改事件。

EDIT编辑
No, because the checkbox change event only gets triggered when it changes(click on it or manually fired).不,因为复选框更改事件仅在更改时触发(单击它或手动触发)。 Your merchantTotal isn't a computed property so it isn't acting on anything.您的 MercerTotal 不是计算属性,因此它不作用于任何东西。 You can try and make it computed, actually it's a computed action you are trying to mimic.你可以试着让它计算,实际上它是你试图模仿的计算动作。

EDIT编辑
Check this fiddle I created.检查我创建的这个小提琴 Like your example everything is working around the selected rows.就像您的示例一样,一切都围绕selected的行进行。 In cases the selected variable changes (after applied filters or toggle a checkbox on or of) it will re-computed the total.如果所选变量发生变化(在应用过滤器或打开或关闭复选框后),它将重新计算总数。 In my example I use products in stead of merchants, but in function it's doing the same.在我的示例中,我使用产品而不是商家,但在功能上也是如此。

EDIT编辑
Are you able to add a unique identifier foreach merchant in the dataset?您能否为数据集中的每个商家添加唯一标识符? Or you can create a ID based on 2 rows (division and merchantname).或者,您可以基于 2 行(部门和商家名称)创建一个 ID。 Its the only way around this is calculate the data via the volume (based on the selected merchant id or unique identifier, like I illustrated).解决此问题的唯一方法是通过数量计算数据(基于所选的商家 ID 或唯一标识符,如我所示)。 Another way to do it is when data changes you unselect the merchants, but this is not really user friendly.另一种方法是在数据更改时取消选择商家,但这并不是真正的用户友好。

I thought it was a Vue implementation problem, but I think the problem lies within the working of the datatable from Vuetify (in your situation).我认为这是一个 Vue 实现问题,但我认为问题在于 Vuetify 的数据表的工作(在您的情况下)。

Sorry, my bad.对不起这是我的错。 Reformulation.重新制定。

Vue has a limitation in reactivity to detect changes in objects. Vue 在检测对象变化的反应性方面存在限制。 That is the problem, you can not know when your volume array change because datepicker changes.这就是问题所在,您无法知道卷阵列何时更改,因为 datepicker 更改了。 You should use this.$set to detect the changes in objects (an array is an object).您应该使用 this.$set 来检测对象的变化(数组就是对象)。

Link: https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats链接: https ://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

This is a sample code:这是一个示例代码:

<div id="app">
    {{ message[0] }}
    <button v-on:click="change">Change</button>
</div>


<script>
    var app = new Vue({
        el: '#app',
        data: {
            message: [45,35,50]
        },
        methods: {
            change: function () {
                //this.$set(this.message, 0, 35);                    
                message[0] = 35;
                console.log(this.message);
            }
        }
    });
</script>

uncomment the line commented and try, you will see the difference.取消注释注释的行并尝试,您会看到不同之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM