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.
|
|
|
|
Page({
|
|
|
|
|
data:{
|
|
|
|
|
className: "",
|
|
|
|
|
students:[
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
onLoad: function(options) {
|
|
|
|
|
console.log(options);
|
|
|
|
|
// options 参数包含了从上一个页面传递过来的数据
|
|
|
|
|
const receivedClassName = options.className; // 获取传递过来的 className,默认为空字符串
|
|
|
|
|
// 更新页面的数据
|
|
|
|
|
this.setData({
|
|
|
|
|
className: receivedClassName // 将接收到的 className 赋值给页面的 data 属性
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// const url = `https://your-backend-server.com/api/students?className=${this.data.className}`
|
|
|
|
|
//根据className向后端请求数据
|
|
|
|
|
wx.request({
|
|
|
|
|
url: `http://192.168.152.1:8090/student/${this.data.className}`,
|
|
|
|
|
method: 'GET',
|
|
|
|
|
success: function(res) {
|
|
|
|
|
console.log(res.data)
|
|
|
|
|
// 后端返回的数据格式如下:
|
|
|
|
|
// {
|
|
|
|
|
// students: [
|
|
|
|
|
// { id: "102201", name: "Jhon" },
|
|
|
|
|
// { id: "102202", name: "Kun" },
|
|
|
|
|
// { id: "102203", name: "Tom" }
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const formattedData = {
|
|
|
|
|
// students: res.data.map(data => ({
|
|
|
|
|
// id: data.id,
|
|
|
|
|
// name: data.name,
|
|
|
|
|
// point: data.point,
|
|
|
|
|
// clazz: data.clazz
|
|
|
|
|
// }))
|
|
|
|
|
// };
|
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
|
// 使用setData方法更新data内的students数组
|
|
|
|
|
|
|
|
|
|
// this.setData({
|
|
|
|
|
// students: formattedData
|
|
|
|
|
// });
|
|
|
|
|
this.setData({
|
|
|
|
|
students: res.data,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
console.error('请求成功但数据格式不正确或数据不是数组');
|
|
|
|
|
}
|
|
|
|
|
}.bind(this), // 注意这里使用.bind(this)来确保this指向当前页面实例
|
|
|
|
|
fail: function(err) {
|
|
|
|
|
console.error('请求失败', err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(this.data.className);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|