Visualization.js 9.1 KB

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