index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // pages/filter/index.js
  2. import http from '../../utils/http'
  3. import util from '../../utils/util'
  4. import api from '../../utils/api'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. active: 0,
  11. projects: [],
  12. project_ids: [''],
  13. work_points: [],
  14. work_point_ids: [''],
  15. device_types: [],
  16. device_id: '',
  17. names: [],
  18. device_name_id: '',
  19. specs: [],
  20. spec_id: ''
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. api.getByName(this, 'projects/getAll', 'projects', {type: 'drop_menu'});
  27. api.getByName(this, 'work-points/get', 'work_points', {type: 'drop_menu'});
  28. var that = this
  29. api.getByName(this, 'devices/getThreeLevel', 'device_types', {type: 'drop_menu'}, function() {
  30. that.updateNameSpec()
  31. });
  32. },
  33. onChange: function(e) {
  34. var name = e.currentTarget.dataset.name
  35. this.setData({
  36. [name]: e.detail
  37. })
  38. if(['device_id', 'device_name_id'].indexOf(name) != -1) {
  39. if(name == 'device_id') {
  40. this.setData({
  41. device_name_id: '',
  42. spec_id: ''
  43. })
  44. }
  45. if(name == 'device_name_id') {
  46. this.setData({
  47. spec_id: ''
  48. })
  49. }
  50. this.updateNameSpec()
  51. }
  52. },
  53. confirm: function() {
  54. this.saveFilter()
  55. wx.navigateBack()
  56. },
  57. updateNameSpec: function() {
  58. var device_types = this.data.device_types
  59. var typeItem = this.getItemById(device_types, this.data.device_id)
  60. var names = typeItem.names;
  61. var nameItem = this.getItemById(names, this.data.device_name_id)
  62. // var specs = this.getItemById(nameItem.specs, this.data.spec)
  63. this.setData({
  64. names,
  65. specs: nameItem.specs
  66. })
  67. },
  68. getItemById(items, id) {
  69. for(var i = 0; i < items.length; ++i) {
  70. if(items[i].value == id) return items[i]
  71. }
  72. return ''
  73. },
  74. /**
  75. * 生命周期函数--监听页面初次渲染完成
  76. */
  77. onReady: function () {
  78. },
  79. delete: function(e) {
  80. var name = e.currentTarget.dataset.name
  81. var index = e.currentTarget.dataset.index
  82. var items = this.data[name]
  83. items.splice(index, 1)
  84. this.setData({
  85. [name]: items
  86. })
  87. },
  88. add: function(e) {
  89. var name = e.currentTarget.dataset.name
  90. var items = this.data[name]
  91. if(!items[items.length - 1]) {
  92. util.error('请先选择');
  93. return false
  94. }
  95. items.push('')
  96. this.setData({
  97. [name]: items
  98. })
  99. },
  100. onDropChange: function(e) {
  101. var index = e.currentTarget.dataset.index
  102. var name = e.currentTarget.dataset.name
  103. var val = e.detail
  104. var items = this.data[name]
  105. items[index] = val
  106. this.setData({
  107. [name]: items
  108. })
  109. },
  110. /**
  111. * 生命周期函数--监听页面显示
  112. */
  113. onShow: function () {
  114. var data = wx.getStorageSync('sg-device-filters')
  115. this.setData(data)
  116. },
  117. /**
  118. * 生命周期函数--监听页面隐藏
  119. */
  120. onHide: function () {
  121. this.saveFilter()
  122. },
  123. saveFilter() {
  124. var data = {
  125. project_ids: this.data.project_ids,
  126. work_point_ids: this.data.work_point_ids,
  127. device_id: this.data.device_id,
  128. device_name_id: this.data.device_name_id,
  129. spec_id: this.data.spec_id
  130. }
  131. wx.setStorageSync('sg-device-filters', data)
  132. },
  133. /**
  134. * 生命周期函数--监听页面卸载
  135. */
  136. onUnload: function () {
  137. },
  138. /**
  139. * 页面相关事件处理函数--监听用户下拉动作
  140. */
  141. onPullDownRefresh: function () {
  142. },
  143. /**
  144. * 页面上拉触底事件的处理函数
  145. */
  146. onReachBottom: function () {
  147. },
  148. /**
  149. * 用户点击右上角分享
  150. */
  151. onShareAppMessage: function () {
  152. }
  153. })