I'm trying to add new data to old one's without refreshing page, the issue is when I add new one not only new data but old data return with it as well.
Example
Here I have 2
bid (old data)
When I add new bid it supposed to become 3
instead it becomes 5
which means my
2
old data stays as they are and again they return with new one3
total becomes5
Code 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));
}
Question
- How can I only return new data without old ones?
this part in sendbid()
:
this.$nextTick(function () {
this.getBidders();
});
this.biders.push(item);
. Do you mean to dothis.bidders.splice(0, this.bidders.length, res.data)
? – Brian LeegetBidders()
) but the issue is i don't know how to return only new one? – mafortisError in nextTick: "TypeError: this.bidders is undefined"
with splice method. – mafortisbiders:[], //return old bids info
, I used two d's in my spelling. – Brian Lee