123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- Page({
- data: {
- shoopingtext: "", // 搜索框内容
- filteredRows: [], // 表格过滤后的数据
- rows: [], // 所有表格数据
- showModal: false, // 控制底部编辑删除弹窗的显示与否
- showAddModal: false, // 控制新增数据弹窗的显示与否
- currentRow: null, // 当前选中的表格行
- // 新增数据弹窗的输入框内容
- newData: {
- OM: "",
- CL: "",
- CEC: "",
- Hplus: "",
- HN: "",
- Al3plus: "",
- Alumina: "",
- IronOxides: "",
- DeltaPH: "",
- Day0PH: ""
- }
- },
- // 页面加载时获取表格数据
- onLoad: function() {
- wx.request({
- url: 'http://localhost:5000/tables',
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- },
- data: {
- 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) => {
- wx.showToast({
- title: '请求失败,请重试',
- icon: 'none'
- });
- console.error('请求失败:', err);
- }
- });
- },
- // 搜索框绑定
- shoppinginput: function(e) {
- this.setData({
- shoopingtext: e.detail.value
- });
- },
- // 搜索功能:根据输入内容过滤表格数据
- search: function() {
- const searchText = this.data.shoopingtext.toLowerCase();
- const filteredRows = this.data.rows.filter(item => {
- return Object.values(item).some(value =>
- String(value).toLowerCase().includes(searchText)
- );
- });
- this.setData({
- filteredRows: filteredRows
- });
- },
- // 新增按钮点击,显示新增弹窗
- onAdd: function() {
- console.log("新增按钮被点击了"); // 调试日志
- this.setData({
- showAddModal: true,
- newData: { // 重置新增数据
- OM: "",
- CL: "",
- CEC: "",
- Hplus: "",
- HN: "",
- Al3plus: "",
- Alumina: "",
- IronOxides: "",
- DeltaPH: "",
- Day0PH: ""
- }
- });
- console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
- },
- // 输入框绑定:更新新增数据的对应字段
- onInputOM: function(e) {
- this.setData({
- 'newData.OM': e.detail.value
- });
- },
- onInputCL: function(e) {
- this.setData({
- 'newData.CL': e.detail.value
- });
- },
- onInputCEC: function(e) {
- this.setData({
- 'newData.CEC': e.detail.value
- });
- },
- onInputHplus: function(e) {
- this.setData({
- 'newData.Hplus': e.detail.value
- });
- },
- onInputHN: function(e) {
- this.setData({
- 'newData.HN': e.detail.value
- });
- },
- onInputAl3plus: function(e) {
- this.setData({
- 'newData.Al3plus': e.detail.value
- });
- },
- onInputAlumina: function(e) {
- this.setData({
- 'newData.Alumina': e.detail.value
- });
- },
- onInputIronOxides: function(e) {
- this.setData({
- 'newData.IronOxides': e.detail.value
- });
- },
- onInputDeltaPH: function(e) {
- this.setData({
- 'newData.DeltaPH': e.detail.value
- });
- },
- onInputDay0PH: function(e) {
- this.setData({
- 'newData.Day0PH': e.detail.value
- });
- },
- // 提交新增数据
- onSubmitAdd: function() {
- const newRow = this.data.newData;
- if (Object.values(newRow).some(value => !value)) {
- wx.showToast({
- title: '所有字段都必须填写!',
- icon: 'none'
- });
- return;
- }
- wx.request({
- url: 'http://localhost:5000/add_item',
- method: 'POST',
- header: {
- 'Content-Type': 'application/json'
- },
- data: {
- table: 'your_table_name',
- item: newRow
- },
- success: (res) => {
- if (res.data.success) {
- this.setData({
- rows: [...this.data.rows, newRow],
- showAddModal: false
- });
- wx.showToast({
- title: '新增成功',
- icon: 'success'
- });
- } else {
- wx.showToast({
- title: '新增失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- wx.showToast({
- title: '请求失败,请重试',
- icon: 'none'
- });
- console.error('请求失败:', err);
- }
- });
- },
- // 取消新增数据
- onCancelAdd: function() {
- this.setData({
- showAddModal: false
- });
- },
- // 编辑按钮点击
- onEdit: function() {
- const updatedRow = this.data.currentRow.row; // 获取当前编辑的行数据
- wx.request({
- url: 'http://localhost:5000/update_item',
- method: 'PUT',
- header: {
- 'Content-Type': 'application/json'
- },
- data: {
- table: 'your_table_name',
- item: updatedRow
- },
- success: (res) => {
- if (res.data.success) {
- let rows = [...this.data.rows];
- rows[this.data.currentRow.index] = updatedRow; // 更新数据
- this.setData({
- rows: rows,
- showModal: false
- });
- wx.showToast({
- title: '更新成功',
- icon: 'success'
- });
- } else {
- wx.showToast({
- title: '更新失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- wx.showToast({
- title: '请求失败,请重试',
- icon: 'none'
- });
- console.error('请求失败:', err);
- }
- });
- },
- // 删除按钮点击
- onDelete: function() {
- const { index, row } = this.data.currentRow;
- const condition = `id=${row.id}`;
- wx.request({
- url: 'http://localhost:5000/delete_item',
- method: 'DELETE',
- header: {
- 'Content-Type': 'application/json'
- },
- data: {
- table: 'your_table_name',
- condition: condition
- },
- success: (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) => {
- wx.showToast({
- title: '请求失败,请重试',
- icon: 'none'
- });
- console.error('请求失败:', err);
- }
- });
- },
- // 取消编辑删除弹窗
- onCancel: function() {
- this.setData({
- showModal: false
- });
- },
- // 行点击事件:显示编辑和删除弹窗
- onRowClick: function(e) {
- const index = e.currentTarget.dataset.index;
- const row = e.currentTarget.dataset.row;
- this.setData({
- currentRow: { index: index, row: row },
- showModal: true // 显示编辑删除弹窗
- });
- }
- });
|