weixinxiaochengxu/pages/items/items.js

59 lines
1.5 KiB
JavaScript

var app = getApp()
Page({
data: {
persons: [],
currentPersonName: "",
items: [],
newItemName: ""
},
onShow: function () {
var person = app.getCurrentPerson()
this.setData({
persons: app.globalData.persons,
currentPersonName: person ? person.name : "",
items: app.getCurrentData().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
}
var data = app.getCurrentData()
if (data.items.indexOf(name) > -1) {
wx.showToast({ title: "该项目已存在", icon: "none" })
return
}
app.addItem(name)
this.setData({
items: app.getCurrentData().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.getCurrentData().items })
wx.showToast({ title: "已删除", icon: "success", duration: 1000 })
}
})
}
})