You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
project/pages/list/list.ts

137 lines
3.0 KiB

// pages/list/list.js
Page({
/**
*
*/
data: {
list:[]
},
/**
* --
*/
onLoad: function () {
},
/**
* --
*/
onReady: function () {
},
/**
* --
* Show
*/
onShow: function () {
//这里的this是指窗口而在request中this是指onShow方法(因为是页面调用onShow,onShow调用request),所以要先定义
var that = this;
wx.request({
//后端接口提供的url
url: 'http://localhost:8080/task/taskList',
method:'GET',
//需要传入的参数
data:{},
success:function(res :any){
var list = res.data.data;
if(list == null){
//如果获取数据失败,提示使用者
var toastText = '获取数据失败' + res.data.msg;
wx.showToast({
title: toastText,
//显示时长为2s
duration:2000
})
}else{
that.setData({
list:list
})
}
}
})
},
/**
* --
*/
onHide: function () {
},
/**
* --
*/
onUnload: function () {
},
/**
* --
*/
onPullDownRefresh: function () {
},
/**
*
*/
onReachBottom: function () {
},
/**
*
*/
onShareAppMessage: function () {
},
addTask: function(){
wx.navigateTo({
url: '../operation/operation',
})
},
//e表示响应的控件
delTask: function(e :any){
var that = this;
//相当于confirm窗口
wx.showModal({
title: '提示',
//这里的变量名需要与响应控件的data-后面的变量名相同
content: '确认要删除['+e.target.dataset.name+']吗?',
success:function(sm){
if(sm.confirm){
wx.request({
url: 'http://localhost:8080/task/delete',
data: {'id':e.target.dataset.id},
header: {
//默认是 'content-type': 'application/json'要传post的参数必须写成这样要传delete参数则为null
"content-type": "application/x-www-form-urlencoded"
},
method: 'DELETE',
success: function(res:any) {
var result = res.data.success;
var toastText='删除成功';
if(result == true){
that.data.list.splice(e.target.dataset.index,1);
that.setData({
list:that.data.list
});
}else{
toastText = '删除失败';
}
wx.showToast({
title: toastText,
duration:2000
})
},
})
}
}
})
}
})