index.js 3.6 KB

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