【发布时间】:2018-08-22 05:33:16
【问题描述】:
我正在尝试在不刷新页面的情况下将新数据添加到旧数据,问题是当我添加新数据时,不仅新数据而且旧数据也随之返回。
Example
这里我有2 出价(旧数据)
当我添加新出价时,它应该变成3,而不是变成5
这意味着我的
2旧数据保持原样并再次返回 有了新的3,总数变为5
代码commented
data() {
return {
bid: { //send bid to back-end (form)
attachment: '',
message: '',
bid: '',
project_id: '',
user_id: '',
},
biders: [], //return old bids info
new_biders: '',
}
},
mounted: function () {
this.getBidders(); // get old bids info
},
sendbid() { //send bid form data to back-end
let project_id = this.project.id;
let message = this.bid.message;
let user_id = this.user.id;
let attachment = this.bid.attachment;
let bid = this.bid.bid;
axios.post('/api/sendbid', {project_id, user_id, message, attachment, bid})
.then(response => {
$(".bidmsg").append('<div class="alert alert-success" role="alert">Thank you dear <b>' + this.user.name + '</b> your bid placed successfully.</div>');
//get new bid data and add it to previous ones
this.$nextTick(function () {
this.getBidders();
});
});
},
getBidders: function () { // get previous (old data) bids info
let url = `/api/projects/${this.$route.params.slug}`
axios.get(url).then(function (response) {
this.project = response.data;
axios.post('/api/projectbids/' + this.project.id)
.then(function (res) {
_.forEach(res.data, function (item) {
this.biders.push(item);
}.bind(this)
)
}.bind(this))
.catch(error => {
console.log(error.response)
});
Echo.private('newbidplaced.' + this.project.id)
.listen('NewBider', function (e) {
this.biders.push(e.bider);
}.bind(this));
Vue.nextTick(function () {
$('[data-toggle="tooltip"]').tooltip()
});
}.bind(this));
}
问题
- 如何只返回新数据而不返回旧数据?
这部分在sendbid():
this.$nextTick(function () {
this.getBidders();
});
【问题讨论】:
-
当您添加新出价时,作为响应,您将获得所有出价而不是添加的出价,然后您将其附加到现有出价,因此其重复数据。所以我认为一种方法是只返回最后添加的那些。
-
你只是推送到投标人的数组,从不修剪:
this.biders.push(item);。你的意思是this.bidders.splice(0, this.bidders.length, res.data)? -
@MayuriPansuriya 正是我的问题(我知道这一点,我称之为
getBidders())但问题是我不知道如何只返回新的? -
@DigitalDrifter 我用拼接方法得到
Error in nextTick: "TypeError: this.bidders is undefined"。 -
数据属性:
biders:[], //return old bids info,我在拼写中使用了两个 d。