Page({ data: { rows: [], // 所有表格数据 currentRow: null, // 当前选中的表格行 filteredRows: [], // 过滤后的表格数据,默认为空 tableHeaders: [ "数据集名称","数据条数","更新时间" ], types: [{ name: 'reduce', display: '降酸模型' }, { name: 'reflux', display: '反酸模型' }], // 数据种类 currentType: '', // 当前选中的数据种类,默认为空字符串 }, // 页面加载时获取表格数据 onLoad: function() { this.LoadData(); }, LoadData: function() { wx.request({ url: 'https://soilgd.com:5000/table', method: 'POST', header: { 'Content-Type': 'application/json' }, data: { table: 'Datasets' }, success: (res) => { console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式 if (res.data && Array.isArray(res.data.rows)) { const rows = res.data.rows.map(row => { return { 'id': row.Dataset_ID, 'name': row.Dataset_name, 'description': row.Dataset_description, 'type': row.Dataset_type, 'count': row.Row_count, 'status': row.Status, 'uploadTime': row.Uploaded_at, }; }); console.log(rows); this.setData({ rows: rows, // 初始时不显示任何数据 }); } else { wx.showToast({ title: '获取数据失败', icon: 'none' }); } }, fail: (err) => { wx.showToast({ title: '请求失败,请重试', icon: 'none' }); console.error('请求失败:', err); } }); }, // 处理行点击事件 onRowClick: function(e) { const index = e.currentTarget.dataset.index; const selectedRow = this.data.filteredRows[index]; this.setData({ currentRow: this.data.filteredRows[index] ? {...this.data.filteredRows[index], index} : null }); console.log('选中的行信息:', selectedRow); // 打印当前选中行的信息 }, // 处理数据种类改变事件 onTypeChange: function(e) { const type = this.data.types[e.detail.value].name; this.setData({ currentType: type, filteredRows: this.data.rows.filter(row => row.type === type) }); }, // 处理训练模型按钮点击事件 trainModel: function() { const { currentRow } = this.data; if (!currentRow) { wx.showToast({ title: '请先选择一行数据', icon: 'none' }); return; } const { id, type } = currentRow; const trainData = { model_type: "RandomForest", model_name: "ForestModel1", model_description: "A random forest model trained on current data.", data_type: type, dataset_id: id }; wx.request({ url: 'https://soilgd.com:5000/train-and-save-model', method: 'POST', header: { 'Content-Type': 'application/json' }, data: trainData, success: (res) => { console.log('模型训练成功:', res); wx.showToast({ title: '模型训练完成', icon: 'success' }); }, fail: (err) => { console.error('模型训练失败:', err); wx.showToast({ title: '模型训练失败', icon: 'none' }); } }); } })