|
@@ -1,5 +1,6 @@
|
|
Page({
|
|
Page({
|
|
data: {
|
|
data: {
|
|
|
|
+ isEditing: false,
|
|
shoopingtext: "", // 搜索框内容
|
|
shoopingtext: "", // 搜索框内容
|
|
filteredRows: [], // 表格过滤后的数据
|
|
filteredRows: [], // 表格过滤后的数据
|
|
rows: [], // 所有表格数据
|
|
rows: [], // 所有表格数据
|
|
@@ -8,21 +9,28 @@ Page({
|
|
currentRow: null, // 当前选中的表格行
|
|
currentRow: null, // 当前选中的表格行
|
|
// 新增数据弹窗的输入框内容
|
|
// 新增数据弹窗的输入框内容
|
|
newData: {
|
|
newData: {
|
|
|
|
+ Sample_ID: "",
|
|
OM: "",
|
|
OM: "",
|
|
CL: "",
|
|
CL: "",
|
|
CEC: "",
|
|
CEC: "",
|
|
- Hplus: "",
|
|
|
|
|
|
+ H: "",
|
|
HN: "",
|
|
HN: "",
|
|
- Al3plus: "",
|
|
|
|
- Alumina: "",
|
|
|
|
- IronOxides: "",
|
|
|
|
- DeltaPH: "",
|
|
|
|
- Day0PH: ""
|
|
|
|
|
|
+ Al: "",
|
|
|
|
+ free_alumina: "",
|
|
|
|
+ free_iron_oxides: "",
|
|
|
|
+ pH: "",
|
|
|
|
+ final_pH: "",
|
|
|
|
+ Collection_location: "",
|
|
|
|
+ Collection_date: ""
|
|
}
|
|
}
|
|
},
|
|
},
|
|
|
|
|
|
// 页面加载时获取表格数据
|
|
// 页面加载时获取表格数据
|
|
onLoad: function() {
|
|
onLoad: function() {
|
|
|
|
+ this.LoadData();
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ LoadData: function() {
|
|
wx.request({
|
|
wx.request({
|
|
url: 'http://localhost:5000/tables',
|
|
url: 'http://localhost:5000/tables',
|
|
method: 'POST',
|
|
method: 'POST',
|
|
@@ -31,49 +39,49 @@ Page({
|
|
},
|
|
},
|
|
data: {
|
|
data: {
|
|
table: 'Soil_samples'
|
|
table: 'Soil_samples'
|
|
- },
|
|
|
|
- success: (res) => {
|
|
|
|
- console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式
|
|
|
|
-
|
|
|
|
- if (res.data && Array.isArray(res.data.rows)) {
|
|
|
|
- const rows = res.data.rows.map(row => {
|
|
|
|
- return {
|
|
|
|
- '0 day pH': row[0],
|
|
|
|
- 'OM g/kg': row[1],
|
|
|
|
- 'CL g/kg': row[2],
|
|
|
|
- 'CEC cmol/kg': row[3],
|
|
|
|
- 'H+ cmol/kg': row[4],
|
|
|
|
- 'HN mg/kg': row[5],
|
|
|
|
- 'Al3plus': row[6],
|
|
|
|
- 'Free alumina g/kg': row[7],
|
|
|
|
- 'Free iron oxides g/kg': row[8],
|
|
|
|
- '105 day pH': row[9],
|
|
|
|
- 'Collection location': row[10],
|
|
|
|
- 'Collection Date': row[11]
|
|
|
|
- };
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- this.setData({
|
|
|
|
- rows: rows,
|
|
|
|
- filteredRows: rows
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- wx.showToast({
|
|
|
|
- title: '获取数据失败',
|
|
|
|
- icon: 'none'
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
|
|
+ },
|
|
|
|
+ success: (res) => {
|
|
|
|
+ console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式
|
|
|
|
+
|
|
|
|
+ if (res.data && Array.isArray(res.data.rows)) {
|
|
|
|
+ const rows = res.data.rows.map(row => {
|
|
|
|
+ return {
|
|
|
|
+ 'Sample_ID': row[0],
|
|
|
|
+ 'pH': row[1],
|
|
|
|
+ 'OM': row[2],
|
|
|
|
+ 'CL': row[3],
|
|
|
|
+ 'CEC': row[4],
|
|
|
|
+ 'H': row[5],
|
|
|
|
+ 'HN': row[6],
|
|
|
|
+ 'Al': row[7],
|
|
|
|
+ 'free_alumina': row[8],
|
|
|
|
+ 'free_iron_oxides': row[9],
|
|
|
|
+ 'final_pH': row[10],
|
|
|
|
+ 'Collection_location': row[11],
|
|
|
|
+ 'Collection_date': row[12]
|
|
|
|
+ };
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ this.setData({
|
|
|
|
+ rows: rows,
|
|
|
|
+ filteredRows: rows
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
wx.showToast({
|
|
wx.showToast({
|
|
- title: '请求失败,请重试',
|
|
|
|
|
|
+ title: '获取数据失败',
|
|
icon: 'none'
|
|
icon: 'none'
|
|
});
|
|
});
|
|
- console.error('请求失败:', err);
|
|
|
|
}
|
|
}
|
|
|
|
+ },
|
|
|
|
+ fail: (err) => {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '请求失败,请重试',
|
|
|
|
+ icon: 'none'
|
|
|
|
+ });
|
|
|
|
+ console.error('请求失败:', err);
|
|
|
|
+ }
|
|
});
|
|
});
|
|
- },
|
|
|
|
-
|
|
|
|
|
|
+ },
|
|
// 搜索框绑定
|
|
// 搜索框绑定
|
|
shoppinginput: function(e) {
|
|
shoppinginput: function(e) {
|
|
this.setData({
|
|
this.setData({
|
|
@@ -103,13 +111,15 @@ Page({
|
|
OM: "",
|
|
OM: "",
|
|
CL: "",
|
|
CL: "",
|
|
CEC: "",
|
|
CEC: "",
|
|
- Hplus: "",
|
|
|
|
|
|
+ H: "",
|
|
HN: "",
|
|
HN: "",
|
|
- Al3plus: "",
|
|
|
|
- Alumina: "",
|
|
|
|
- IronOxides: "",
|
|
|
|
- DeltaPH: "",
|
|
|
|
- Day0PH: ""
|
|
|
|
|
|
+ Al: "",
|
|
|
|
+ free_alumina: "",
|
|
|
|
+ free_iron_oxides: "",
|
|
|
|
+ pH: "",
|
|
|
|
+ final_pH: "",
|
|
|
|
+ Collection_location: "",
|
|
|
|
+ Collection_date: ""
|
|
}
|
|
}
|
|
});
|
|
});
|
|
console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
|
|
console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
|
|
@@ -133,7 +143,7 @@ Page({
|
|
},
|
|
},
|
|
onInputHplus: function(e) {
|
|
onInputHplus: function(e) {
|
|
this.setData({
|
|
this.setData({
|
|
- 'newData.Hplus': e.detail.value
|
|
|
|
|
|
+ 'newData.H': e.detail.value
|
|
});
|
|
});
|
|
},
|
|
},
|
|
onInputHN: function(e) {
|
|
onInputHN: function(e) {
|
|
@@ -143,27 +153,37 @@ Page({
|
|
},
|
|
},
|
|
onInputAl3plus: function(e) {
|
|
onInputAl3plus: function(e) {
|
|
this.setData({
|
|
this.setData({
|
|
- 'newData.Al3plus': e.detail.value
|
|
|
|
|
|
+ 'newData.Al': e.detail.value
|
|
});
|
|
});
|
|
},
|
|
},
|
|
onInputAlumina: function(e) {
|
|
onInputAlumina: function(e) {
|
|
this.setData({
|
|
this.setData({
|
|
- 'newData.Alumina': e.detail.value
|
|
|
|
|
|
+ 'newData.free_alumina': e.detail.value
|
|
});
|
|
});
|
|
},
|
|
},
|
|
onInputIronOxides: function(e) {
|
|
onInputIronOxides: function(e) {
|
|
this.setData({
|
|
this.setData({
|
|
- 'newData.IronOxides': e.detail.value
|
|
|
|
|
|
+ 'newData.free_iron_oxides': e.detail.value
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ onInputinitPH: function(e) {
|
|
|
|
+ this.setData({
|
|
|
|
+ 'newData.pH': e.detail.value
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ onInputfinalPH: function(e) {
|
|
|
|
+ this.setData({
|
|
|
|
+ 'newData.final_pH': e.detail.value
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- onInputDeltaPH: function(e) {
|
|
|
|
|
|
+ onInputlocation: function(e) {
|
|
this.setData({
|
|
this.setData({
|
|
- 'newData.DeltaPH': e.detail.value
|
|
|
|
|
|
+ 'newData.Collection_location': e.detail.value
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- onInputDay0PH: function(e) {
|
|
|
|
|
|
+ onBindDateChange: function(e) {
|
|
this.setData({
|
|
this.setData({
|
|
- 'newData.Day0PH': e.detail.value
|
|
|
|
|
|
+ 'newData.Collection_date': e.detail.value
|
|
});
|
|
});
|
|
},
|
|
},
|
|
|
|
|
|
@@ -198,6 +218,7 @@ Page({
|
|
title: '新增成功',
|
|
title: '新增成功',
|
|
icon: 'success'
|
|
icon: 'success'
|
|
});
|
|
});
|
|
|
|
+ this.LoadData();
|
|
} else {
|
|
} else {
|
|
wx.showToast({
|
|
wx.showToast({
|
|
title: '新增失败',
|
|
title: '新增失败',
|
|
@@ -216,59 +237,97 @@ Page({
|
|
},
|
|
},
|
|
|
|
|
|
// 取消新增数据
|
|
// 取消新增数据
|
|
- onCancelAdd: function() {
|
|
|
|
|
|
+ onCancel: function() {
|
|
this.setData({
|
|
this.setData({
|
|
showAddModal: false
|
|
showAddModal: false
|
|
});
|
|
});
|
|
},
|
|
},
|
|
|
|
|
|
// 编辑按钮点击
|
|
// 编辑按钮点击
|
|
- onEdit: function() {
|
|
|
|
- const updatedRow = this.data.currentRow.row; // 获取当前编辑的行数据
|
|
|
|
- wx.request({
|
|
|
|
- url: `http://127.0.0.1:5000/delete_item?${condition}`,
|
|
|
|
- method: 'DELETE',
|
|
|
|
- header: {
|
|
|
|
- 'Content-Type': 'application/json'
|
|
|
|
- },
|
|
|
|
- success: (res) => {
|
|
|
|
- console.log('响应数据:', res); // 输出响应数据,检查服务器返回的内容
|
|
|
|
- if (res.data.success) {
|
|
|
|
- let rows = [...this.data.rows];
|
|
|
|
- rows.splice(index, 1); // 删除选中的行
|
|
|
|
- this.setData({
|
|
|
|
- rows: rows,
|
|
|
|
- showModal: false
|
|
|
|
- });
|
|
|
|
- wx.showToast({
|
|
|
|
- title: '删除成功',
|
|
|
|
- icon: 'success'
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- wx.showToast({
|
|
|
|
- title: '删除失败',
|
|
|
|
- icon: 'none'
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- console.error('请求失败:', err);
|
|
|
|
|
|
+onEdit: function(e) {
|
|
|
|
+ const { index, row } = this.data.currentRow;
|
|
|
|
+ console.log(row);
|
|
|
|
+
|
|
|
|
+ // 检查 currentRow 是否有定义并且包含数据
|
|
|
|
+ if (row) {
|
|
|
|
+ this.setData({
|
|
|
|
+ isEditing: true,
|
|
|
|
+ showModal: false,
|
|
|
|
+ showAddModal: true, // 显示编辑弹窗
|
|
|
|
+ newData: row, // 将当前行的数据复制到 newData 中,以便在表单中显示
|
|
|
|
+ currentRow: { index: index, row: row } // 保存当前行的信息,以便在提交时使用
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '数据加载失败,请重试',
|
|
|
|
+ icon: 'none'
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+},
|
|
|
|
+
|
|
|
|
+ // 提交编辑数据
|
|
|
|
+onSubmitEdit: function() {
|
|
|
|
+ const updatedRow = this.data.newData;
|
|
|
|
+ const { index } = this.data.currentRow;
|
|
|
|
+
|
|
|
|
+ if (Object.values(updatedRow).some(value => !value)) {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '所有字段都必须填写!',
|
|
|
|
+ icon: 'none'
|
|
|
|
+ });
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ wx.request({
|
|
|
|
+ url: 'http://localhost:5000/update_item',
|
|
|
|
+ method: 'PUT',
|
|
|
|
+ header: {
|
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
|
+ },
|
|
|
|
+ data: {
|
|
|
|
+ table: 'Soil_samples',
|
|
|
|
+ item: updatedRow
|
|
|
|
+ },
|
|
|
|
+ success: (res) => {
|
|
|
|
+ if (res.data.success) {
|
|
|
|
+ // 更新成功后,更新前端数据
|
|
|
|
+ let rows = this.data.rows;
|
|
|
|
+ rows[index] = updatedRow; // 用更新后的数据替换旧数据
|
|
|
|
+ this.setData({
|
|
|
|
+ rows: rows,
|
|
|
|
+ showAddModal: false // 关闭编辑弹窗
|
|
|
|
+ });
|
|
wx.showToast({
|
|
wx.showToast({
|
|
- title: '请求失败,请重试',
|
|
|
|
|
|
+ title: '编辑成功',
|
|
|
|
+ icon: 'success'
|
|
|
|
+ });
|
|
|
|
+ this.LoadData();
|
|
|
|
+ } else {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '编辑失败',
|
|
icon: 'none'
|
|
icon: 'none'
|
|
});
|
|
});
|
|
}
|
|
}
|
|
- });
|
|
|
|
- },
|
|
|
|
|
|
+ },
|
|
|
|
+ fail: (err) => {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '请求失败,请重试',
|
|
|
|
+ icon: 'none'
|
|
|
|
+ });
|
|
|
|
+ console.error('请求失败:', err);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+},
|
|
|
|
|
|
// 删除按钮点击
|
|
// 删除按钮点击
|
|
onDelete: function() {
|
|
onDelete: function() {
|
|
const { index, row } = this.data.currentRow;
|
|
const { index, row } = this.data.currentRow;
|
|
- const condition = `id=${row.id}`;
|
|
|
|
|
|
+ console.log(this.data.currentRow);
|
|
|
|
+ const condition = `Sample_ID=${row.Sample_ID}`;
|
|
|
|
|
|
wx.request({
|
|
wx.request({
|
|
url: 'http://127.0.0.1:5000/delete_item',
|
|
url: 'http://127.0.0.1:5000/delete_item',
|
|
- method: 'DELETE',
|
|
|
|
|
|
+ method: 'POST',
|
|
header: {
|
|
header: {
|
|
'Content-Type': 'application/json'
|
|
'Content-Type': 'application/json'
|
|
},
|
|
},
|
|
@@ -278,7 +337,7 @@ Page({
|
|
},
|
|
},
|
|
success: (res) => {
|
|
success: (res) => {
|
|
if (res.data.success) {
|
|
if (res.data.success) {
|
|
- let rows = [...this.data.rows];
|
|
|
|
|
|
+ let rows = this.data.rows;
|
|
rows.splice(index, 1); // 删除选中的行
|
|
rows.splice(index, 1); // 删除选中的行
|
|
this.setData({
|
|
this.setData({
|
|
rows: rows,
|
|
rows: rows,
|
|
@@ -288,6 +347,7 @@ Page({
|
|
title: '删除成功',
|
|
title: '删除成功',
|
|
icon: 'success'
|
|
icon: 'success'
|
|
});
|
|
});
|
|
|
|
+ this.LoadData();
|
|
} else {
|
|
} else {
|
|
wx.showToast({
|
|
wx.showToast({
|
|
title: '删除失败',
|
|
title: '删除失败',
|
|
@@ -304,12 +364,24 @@ Page({
|
|
}
|
|
}
|
|
});
|
|
});
|
|
},
|
|
},
|
|
-
|
|
|
|
|
|
+ onSubmit: function() {
|
|
|
|
+ if (this.data.isEditing) {
|
|
|
|
+ this.onSubmitEdit();
|
|
|
|
+ } else {
|
|
|
|
+ this.onSubmitAdd();
|
|
|
|
+ }
|
|
|
|
+ },
|
|
// 取消编辑删除弹窗
|
|
// 取消编辑删除弹窗
|
|
onCancel: function() {
|
|
onCancel: function() {
|
|
- this.setData({
|
|
|
|
- showModal: false
|
|
|
|
- });
|
|
|
|
|
|
+ if (this.data.showModal) {
|
|
|
|
+ this.setData({
|
|
|
|
+ showModal: false
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ this.setData({
|
|
|
|
+ showAddModal: false
|
|
|
|
+ });
|
|
|
|
+ }
|
|
},
|
|
},
|
|
|
|
|
|
// 行点击事件:显示编辑和删除弹窗
|
|
// 行点击事件:显示编辑和删除弹窗
|