Visualization.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. Page({
  2. data: {
  3. shoopingtext: "", // 搜索框内容
  4. filteredRows: [], // 表格过滤后的数据
  5. rows: [], // 所有表格数据
  6. showModal: false, // 控制底部编辑删除弹窗的显示与否
  7. showAddModal: false, // 控制新增数据弹窗的显示与否
  8. currentRow: null, // 当前选中的表格行
  9. // 新增数据弹窗的输入框内容
  10. newData: {
  11. OM: "",
  12. CL: "",
  13. CEC: "",
  14. Hplus: "",
  15. HN: "",
  16. Al3plus: "",
  17. Alumina: "",
  18. IronOxides: "",
  19. DeltaPH: "",
  20. Day0PH: ""
  21. }
  22. },
  23. // 页面加载时获取表格数据
  24. onLoad: function() {
  25. wx.request({
  26. url: 'http://localhost:5000/tables',
  27. method: 'POST',
  28. header: {
  29. 'Content-Type': 'application/json'
  30. },
  31. data: {
  32. table: 'Soil_samples'
  33. },
  34. success: (res) => {
  35. console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式
  36. if (res.data && Array.isArray(res.data.rows)) {
  37. const rows = res.data.rows.map(row => {
  38. return {
  39. '0 day pH': row[0],
  40. 'OM g/kg': row[1],
  41. 'CL g/kg': row[2],
  42. 'CEC cmol/kg': row[3],
  43. 'H+ cmol/kg': row[4],
  44. 'HN mg/kg': row[5],
  45. 'Al3plus': row[6],
  46. 'Free alumina g/kg': row[7],
  47. 'Free iron oxides g/kg': row[8],
  48. '105 day pH': row[9],
  49. 'Collection location': row[10],
  50. 'Collection Date': row[11]
  51. };
  52. });
  53. this.setData({
  54. rows: rows,
  55. filteredRows: rows
  56. });
  57. } else {
  58. wx.showToast({
  59. title: '获取数据失败',
  60. icon: 'none'
  61. });
  62. }
  63. },
  64. fail: (err) => {
  65. wx.showToast({
  66. title: '请求失败,请重试',
  67. icon: 'none'
  68. });
  69. console.error('请求失败:', err);
  70. }
  71. });
  72. },
  73. // 搜索框绑定
  74. shoppinginput: function(e) {
  75. this.setData({
  76. shoopingtext: e.detail.value
  77. });
  78. },
  79. // 搜索功能:根据输入内容过滤表格数据
  80. search: function() {
  81. const searchText = this.data.shoopingtext.toLowerCase();
  82. const filteredRows = this.data.rows.filter(item => {
  83. return Object.values(item).some(value =>
  84. String(value).toLowerCase().includes(searchText)
  85. );
  86. });
  87. this.setData({
  88. filteredRows: filteredRows
  89. });
  90. },
  91. // 新增按钮点击,显示新增弹窗
  92. onAdd: function() {
  93. console.log("新增按钮被点击了"); // 调试日志
  94. this.setData({
  95. showAddModal: true,
  96. newData: { // 重置新增数据
  97. OM: "",
  98. CL: "",
  99. CEC: "",
  100. Hplus: "",
  101. HN: "",
  102. Al3plus: "",
  103. Alumina: "",
  104. IronOxides: "",
  105. DeltaPH: "",
  106. Day0PH: ""
  107. }
  108. });
  109. console.log("showAddModal: ", this.data.showAddModal); // 确认数据更新
  110. },
  111. // 输入框绑定:更新新增数据的对应字段
  112. onInputOM: function(e) {
  113. this.setData({
  114. 'newData.OM': e.detail.value
  115. });
  116. },
  117. onInputCL: function(e) {
  118. this.setData({
  119. 'newData.CL': e.detail.value
  120. });
  121. },
  122. onInputCEC: function(e) {
  123. this.setData({
  124. 'newData.CEC': e.detail.value
  125. });
  126. },
  127. onInputHplus: function(e) {
  128. this.setData({
  129. 'newData.Hplus': e.detail.value
  130. });
  131. },
  132. onInputHN: function(e) {
  133. this.setData({
  134. 'newData.HN': e.detail.value
  135. });
  136. },
  137. onInputAl3plus: function(e) {
  138. this.setData({
  139. 'newData.Al3plus': e.detail.value
  140. });
  141. },
  142. onInputAlumina: function(e) {
  143. this.setData({
  144. 'newData.Alumina': e.detail.value
  145. });
  146. },
  147. onInputIronOxides: function(e) {
  148. this.setData({
  149. 'newData.IronOxides': e.detail.value
  150. });
  151. },
  152. onInputDeltaPH: function(e) {
  153. this.setData({
  154. 'newData.DeltaPH': e.detail.value
  155. });
  156. },
  157. onInputDay0PH: function(e) {
  158. this.setData({
  159. 'newData.Day0PH': e.detail.value
  160. });
  161. },
  162. // 提交新增数据
  163. onSubmitAdd: function() {
  164. const newRow = this.data.newData;
  165. if (Object.values(newRow).some(value => !value)) {
  166. wx.showToast({
  167. title: '所有字段都必须填写!',
  168. icon: 'none'
  169. });
  170. return;
  171. }
  172. wx.request({
  173. url: 'http://localhost:5000/add_item',
  174. method: 'POST',
  175. header: {
  176. 'Content-Type': 'application/json'
  177. },
  178. data: {
  179. table: 'your_table_name',
  180. item: newRow
  181. },
  182. success: (res) => {
  183. if (res.data.success) {
  184. this.setData({
  185. rows: [...this.data.rows, newRow],
  186. showAddModal: false
  187. });
  188. wx.showToast({
  189. title: '新增成功',
  190. icon: 'success'
  191. });
  192. } else {
  193. wx.showToast({
  194. title: '新增失败',
  195. icon: 'none'
  196. });
  197. }
  198. },
  199. fail: (err) => {
  200. wx.showToast({
  201. title: '请求失败,请重试',
  202. icon: 'none'
  203. });
  204. console.error('请求失败:', err);
  205. }
  206. });
  207. },
  208. // 取消新增数据
  209. onCancelAdd: function() {
  210. this.setData({
  211. showAddModal: false
  212. });
  213. },
  214. // 编辑按钮点击
  215. onEdit: function() {
  216. const updatedRow = this.data.currentRow.row; // 获取当前编辑的行数据
  217. wx.request({
  218. url: 'http://localhost:5000/update_item',
  219. method: 'PUT',
  220. header: {
  221. 'Content-Type': 'application/json'
  222. },
  223. data: {
  224. table: 'your_table_name',
  225. item: updatedRow
  226. },
  227. success: (res) => {
  228. if (res.data.success) {
  229. let rows = [...this.data.rows];
  230. rows[this.data.currentRow.index] = updatedRow; // 更新数据
  231. this.setData({
  232. rows: rows,
  233. showModal: false
  234. });
  235. wx.showToast({
  236. title: '更新成功',
  237. icon: 'success'
  238. });
  239. } else {
  240. wx.showToast({
  241. title: '更新失败',
  242. icon: 'none'
  243. });
  244. }
  245. },
  246. fail: (err) => {
  247. wx.showToast({
  248. title: '请求失败,请重试',
  249. icon: 'none'
  250. });
  251. console.error('请求失败:', err);
  252. }
  253. });
  254. },
  255. // 删除按钮点击
  256. onDelete: function() {
  257. const { index, row } = this.data.currentRow;
  258. const condition = `id=${row.id}`;
  259. wx.request({
  260. url: 'http://localhost:5000/delete_item',
  261. method: 'DELETE',
  262. header: {
  263. 'Content-Type': 'application/json'
  264. },
  265. data: {
  266. table: 'your_table_name',
  267. condition: condition
  268. },
  269. success: (res) => {
  270. if (res.data.success) {
  271. let rows = [...this.data.rows];
  272. rows.splice(index, 1); // 删除选中的行
  273. this.setData({
  274. rows: rows,
  275. showModal: false
  276. });
  277. wx.showToast({
  278. title: '删除成功',
  279. icon: 'success'
  280. });
  281. } else {
  282. wx.showToast({
  283. title: '删除失败',
  284. icon: 'none'
  285. });
  286. }
  287. },
  288. fail: (err) => {
  289. wx.showToast({
  290. title: '请求失败,请重试',
  291. icon: 'none'
  292. });
  293. console.error('请求失败:', err);
  294. }
  295. });
  296. },
  297. // 取消编辑删除弹窗
  298. onCancel: function() {
  299. this.setData({
  300. showModal: false
  301. });
  302. },
  303. // 行点击事件:显示编辑和删除弹窗
  304. onRowClick: function(e) {
  305. const index = e.currentTarget.dataset.index;
  306. const row = e.currentTarget.dataset.row;
  307. this.setData({
  308. currentRow: { index: index, row: row },
  309. showModal: true // 显示编辑删除弹窗
  310. });
  311. }
  312. });