weixinxiaochengxu/pages/items/items.js

51 lines
1.2 KiB
JavaScript

var app = getApp()
Page({
data: {
items: [],
newItemName: ""
},
onShow: function () {
this.setData({ items: app.globalData.items })
},
onNameInput: function (e) {
this.setData({ newItemName: e.detail.value })
},
onAddItem: function () {
var name = this.data.newItemName.trim()
if (!name) {
wx.showToast({ title: "请输入项目名称", icon: "none" })
return
}
if (app.globalData.items.indexOf(name) > -1) {
wx.showToast({ title: "该项目已存在", icon: "none" })
return
}
app.addItem(name)
this.setData({
items: app.globalData.items,
newItemName: ""
})
wx.showToast({ title: "已添加", icon: "success", duration: 1000 })
},
onDeleteItem: function (e) {
var item = e.currentTarget.dataset.item
var that = this
wx.showModal({
title: "确认删除",
content: "确定删除项目「" + item + "」吗?\n该项目的所有历史数据也将被删除。",
confirmColor: "#e53e3e",
success: function (res) {
if (!res.confirm) return
app.removeItem(item)
that.setData({ items: app.globalData.items })
wx.showToast({ title: "已删除", icon: "success", duration: 1000 })
}
})
}
})