如图:想要实现的效果,当点击了弹窗的确定按钮后更新数据库字段,在页面及时显示“顶”这个字,在代码中设置0:const wehicle = that.data.wehicles[0],就能够实现效果,如果设置为id,const wehicle = that.data.wehicles[id],就实现不了。这个id,是点击置顶按钮时的id值,这是什么原因呢?
/**
* 置顶弹窗
*/
showCode(event) {
console.log(event);
const id = event.currentTarget.dataset.id
console.log(id)
this.setData({
show_input: true,
id: id
})
},
// 获取input输入框
getCodeValue(e) {
this.setData({
code: e.detail.value,
})
},
// 关闭弹窗
closeModal(e) {
console.log(e);
this.setData({
show_input: false
})
},
// 点击确定
async confirm(e) {
const that = this;
const itmelength = that.data.code;
const id = that.data.id; // 点击弹窗按钮的id,
const wehicle = that.data.wehicles[0]; // 传入0能够实现效果,当传入id时,就不能实现。
console.log(wehicle)
wx.showLoading({
title: "正在置顶中..."
})
wx.cloud.callFunction({
name: "topping",
data: {
id: id,
itmelength: itmelength,
},
success: res => {
console.log(res);
const updated = res.result.stats.updated;
if(!wehicle.topping){
wehicle.topping = [itmelength]
}else{
wehicle.topping.push(itmelength)
}
const wehicles = that.data.wehicles;
console.log(wehicles)
wehicles[id] = wehicle
console.log(wehicles[id]);
that.setData({
wehicles:wehicles
})
if (updated) {
wx.hideLoading();
wx.showToast({
title: "恭喜!置顶成功!",
})
setTimeout(function () {
that.setData({
show_input: false,
})
}, 800)
} else {
wx.showToast({
title: "置顶失败,请重新置顶!",
})
}
}
})
},
that.data.wehicles是个数组,当然得通过索引类访问,你的id是一个键值,如果that.data.wehicles是一个Json,那是可以这么访问that.data.wehicles[id]