Visualization.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. Page({
  2. data: {
  3. isEditing: false,
  4. shoopingtext: "", // 搜索框内容
  5. filteredRows: [], // 表格过滤后的数据
  6. rows: [], // 所有表格数据
  7. showModal: false, // 控制底部编辑删除弹窗的显示与否
  8. showAddModal: false, // 控制新增数据弹窗的显示与否
  9. currentRow: null, // 当前选中的表格行
  10. // 新增数据弹窗的输入框内容
  11. newData: {
  12. Sample_ID: "",
  13. OM: "",
  14. CL: "",
  15. CEC: "",
  16. H: "",
  17. HN: "",
  18. Al: "",
  19. free_alumina: "",
  20. free_iron_oxides: "",
  21. pH: "",
  22. final_pH: "",
  23. Collection_location: "",
  24. Collection_date: ""
  25. },
  26. // 中文表头内容,动态生成
  27. tableHeaders: [
  28. "序号", "0天 pH", "有机质含量 (g/kg)", "土壤粘粒重量 (g/kg)", "阳离子交换量 (cmol/kg)",
  29. "氢离子含量 (cmol/kg)", "铵离子含量 (mg/kg)", "铝离子含量 (cmol/kg)", "游离氧化铝 (g/kg)",
  30. "游离氧化铁 (g/kg)", "105天 pH", "采集位置", "采集日期"
  31. ]
  32. },
  33. // 页面加载时获取表格数据
  34. onLoad: function() {
  35. this.LoadData();
  36. },
  37. LoadData: function() {
  38. wx.request({
  39. url: 'http://localhost:5000/table',
  40. method: 'POST',
  41. header: {
  42. 'Content-Type': 'application/json'
  43. },
  44. data: {
  45. table: 'current_reduce'
  46. },
  47. success: (res) => {
  48. console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式
  49. if (res.data && Array.isArray(res.data.rows)) {
  50. const rows = res.data.rows.map(row => {
  51. return {
  52. 'Sample_ID': row[0],
  53. 'pH': row[1],
  54. 'OM': row[2],
  55. 'CL': row[3],
  56. 'CEC': row[4],
  57. 'H': row[5],
  58. 'HN': row[6],
  59. 'Al': row[7],
  60. 'free_alumina': row[8],
  61. 'free_iron_oxides': row[9],
  62. 'final_pH': row[10],
  63. 'Collection_location': row[11],
  64. 'Collection_date': row[12]
  65. };
  66. });
  67. this.setData({
  68. rows: rows,
  69. filteredRows: rows
  70. });
  71. } else {
  72. wx.showToast({
  73. title: '获取数据失败',
  74. icon: 'none'
  75. });
  76. }
  77. },
  78. fail: (err) => {
  79. wx.showToast({
  80. title: '请求失败,请重试',
  81. icon: 'none'
  82. });
  83. console.error('请求失败:', err);
  84. }
  85. });
  86. },
  87. // 搜索框绑定
  88. shoppinginput: function(e) {
  89. this.setData({
  90. shoopingtext: e.detail.value
  91. });
  92. },
  93. // 搜索功能:根据输入内容过滤表格数据
  94. search: function() {
  95. const searchText = this.data.shoopingtext.toLowerCase();
  96. const filteredRows = this.data.rows.filter(item => {
  97. return Object.values(item).some(value =>
  98. String(value).toLowerCase().includes(searchText)
  99. );
  100. });
  101. this.setData({
  102. filteredRows: filteredRows
  103. });
  104. },
  105. // 新增按钮点击,显示新增弹窗
  106. onAdd: function() {
  107. console.log("新增按钮被点击了"); // 调试日志
  108. this.setData({
  109. showAddModal: true,
  110. newData: { // 重置新增数据
  111. OM: "",
  112. CL: "",
  113. CEC: "",
  114. H: "",
  115. HN: "",
  116. Al: "",
  117. free_alumina: "",
  118. free_iron_oxides: "",
  119. pH: "",
  120. final_pH: "",
  121. Collection_location: "",
  122. Collection_date: ""
  123. }
  124. });
  125. console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
  126. },
  127. // 输入框绑定:更新新增数据的对应字段
  128. onInputOM: function(e) {
  129. this.setData({
  130. 'newData.OM': e.detail.value
  131. });
  132. },
  133. onInputCL: function(e) {
  134. this.setData({
  135. 'newData.CL': e.detail.value
  136. });
  137. },
  138. onInputCEC: function(e) {
  139. this.setData({
  140. 'newData.CEC': e.detail.value
  141. });
  142. },
  143. onInputHplus: function(e) {
  144. this.setData({
  145. 'newData.H': e.detail.value
  146. });
  147. },
  148. onInputHN: function(e) {
  149. this.setData({
  150. 'newData.HN': e.detail.value
  151. });
  152. },
  153. onInputAl3plus: function(e) {
  154. this.setData({
  155. 'newData.Al': e.detail.value
  156. });
  157. },
  158. onInputAlumina: function(e) {
  159. this.setData({
  160. 'newData.free_alumina': e.detail.value
  161. });
  162. },
  163. onInputIronOxides: function(e) {
  164. this.setData({
  165. 'newData.free_iron_oxides': e.detail.value
  166. });
  167. },
  168. onInputinitPH: function(e) {
  169. this.setData({
  170. 'newData.pH': e.detail.value
  171. });
  172. },
  173. onInputfinalPH: function(e) {
  174. this.setData({
  175. 'newData.final_pH': e.detail.value
  176. });
  177. },
  178. onInputlocation: function(e) {
  179. this.setData({
  180. 'newData.Collection_location': e.detail.value
  181. });
  182. },
  183. onBindDateChange: function(e) {
  184. this.setData({
  185. 'newData.Collection_date': e.detail.value
  186. });
  187. },
  188. // 提交新增数据
  189. onSubmitAdd: function() {
  190. const newRow = this.data.newData;
  191. if (Object.values(newRow).some(value => !value)) {
  192. wx.showToast({
  193. title: '所有字段都必须填写!',
  194. icon: 'none'
  195. });
  196. return;
  197. }
  198. wx.request({
  199. url: 'http://localhost:5000/add_item',
  200. method: 'POST',
  201. header: {
  202. 'Content-Type': 'application/json'
  203. },
  204. data: {
  205. table: 'Soil_samples',
  206. item: newRow
  207. },
  208. success: (res) => {
  209. if (res.data.success) {
  210. this.setData({
  211. rows: [...this.data.rows, newRow],
  212. showAddModal: false
  213. });
  214. wx.showToast({
  215. title: '新增成功',
  216. icon: 'success'
  217. });
  218. this.LoadData();
  219. } else {
  220. wx.showToast({
  221. title: '新增失败',
  222. icon: 'none'
  223. });
  224. }
  225. },
  226. fail: (err) => {
  227. wx.showToast({
  228. title: '请求失败,请重试',
  229. icon: 'none'
  230. });
  231. console.error('请求失败:', err);
  232. }
  233. });
  234. },
  235. // 取消新增数据
  236. onCancel: function() {
  237. this.setData({
  238. showAddModal: false
  239. });
  240. },
  241. // 编辑按钮点击
  242. onEdit: function(e) {
  243. const { index, row } = this.data.currentRow;
  244. console.log(row);
  245. // 检查 currentRow 是否有定义并且包含数据
  246. if (row) {
  247. this.setData({
  248. isEditing: true,
  249. showModal: false,
  250. showAddModal: true, // 显示编辑弹窗
  251. newData: row, // 将当前行的数据复制到 newData 中,以便在表单中显示
  252. currentRow: { index: index, row: row } // 保存当前行的信息,以便在提交时使用
  253. });
  254. } else {
  255. wx.showToast({
  256. title: '数据加载失败,请重试',
  257. icon: 'none'
  258. });
  259. }
  260. },
  261. // 提交编辑数据
  262. onSubmitEdit: function() {
  263. const updatedRow = this.data.newData;
  264. const { index } = this.data.currentRow;
  265. if (Object.values(updatedRow).some(value => !value)) {
  266. wx.showToast({
  267. title: '所有字段都必须填写!',
  268. icon: 'none'
  269. });
  270. return;
  271. }
  272. wx.request({
  273. url: 'http://localhost:5000/update_item',
  274. method: 'PUT',
  275. header: {
  276. 'Content-Type': 'application/json'
  277. },
  278. data: {
  279. table: 'Soil_samples',
  280. item: updatedRow
  281. },
  282. success: (res) => {
  283. if (res.data.success) {
  284. // 更新成功后,更新前端数据
  285. let rows = this.data.rows;
  286. rows[index] = updatedRow; // 用更新后的数据替换旧数据
  287. this.setData({
  288. rows: rows,
  289. showAddModal: false // 关闭编辑弹窗
  290. });
  291. wx.showToast({
  292. title: '编辑成功',
  293. icon: 'success'
  294. });
  295. this.LoadData();
  296. } else {
  297. wx.showToast({
  298. title: '编辑失败',
  299. icon: 'none'
  300. });
  301. }
  302. },
  303. fail: (err) => {
  304. wx.showToast({
  305. title: '请求失败,请重试',
  306. icon: 'none'
  307. });
  308. console.error('请求失败:', err);
  309. }
  310. });
  311. },
  312. // 删除按钮点击
  313. onDelete: function() {
  314. const { index, row } = this.data.currentRow;
  315. console.log("当前选中行的数据:", this.data.currentRow);
  316. const condition = `Sample_ID=${row.Sample_ID}`; // 使用条件进行删除
  317. // 发送 DELETE 请求
  318. wx.request({
  319. url: 'http://127.0.0.1:5000/delete_item', // 确保使用正确的URL
  320. method: 'DELETE', // 使用 DELETE 请求方法
  321. header: {
  322. 'Content-Type': 'application/json' // 请求类型是 JSON
  323. },
  324. data: {
  325. table: 'Soil_samples', // 目标表
  326. condition: condition // 删除条件
  327. },
  328. success: (res) => {
  329. if (res.data.success) {
  330. // 删除成功后,更新前端数据
  331. let rows = this.data.rows;
  332. rows.splice(index, 1); // 从数据中删除选中的行
  333. this.setData({
  334. rows: rows,
  335. showModal: false // 隐藏编辑删除弹窗
  336. });
  337. wx.showToast({
  338. title: '删除成功',
  339. icon: 'success'
  340. });
  341. // 重新获取数据
  342. this.LoadData(); // 重新加载数据
  343. } else {
  344. wx.showToast({
  345. title: '删除失败',
  346. icon: 'none'
  347. });
  348. }
  349. },
  350. fail: (err) => {
  351. wx.showToast({
  352. title: '请求失败,请重试',
  353. icon: 'none'
  354. });
  355. console.error('请求失败:', err);
  356. }
  357. });
  358. },
  359. onSubmit: function() {
  360. if (this.data.isEditing) {
  361. this.onSubmitEdit();
  362. } else {
  363. this.onSubmitAdd();
  364. }
  365. },
  366. // 取消编辑删除弹窗
  367. onCancel: function() {
  368. if (this.data.showModal) {
  369. this.setData({
  370. showModal: false
  371. });
  372. } else {
  373. this.setData({
  374. showAddModal: false
  375. });
  376. }
  377. },
  378. // 行点击事件:显示编辑和删除弹窗
  379. onRowClick: function(e) {
  380. const index = e.currentTarget.dataset.index;
  381. const row = e.currentTarget.dataset.row;
  382. this.setData({
  383. currentRow: { index: index, row: row },
  384. showModal: true // 显示编辑删除弹窗
  385. });
  386. }
  387. });