index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. const getHeight = require("../../utils/getRheRemainingHeight");
  2. Component({
  3. options: {
  4. addGlobalClass: true
  5. },
  6. properties: {
  7. sort: Array, //排序规则列表
  8. search: Boolean, //是否开启搜索
  9. list: Array, //功能列表
  10. condition: String, //搜索内容
  11. onClick: Function,
  12. startUsing: Boolean, //启用搜索
  13. onSearch: Function, //搜索回调
  14. record: { //记录历史
  15. type: Boolean,
  16. value: true
  17. }
  18. },
  19. data: {
  20. sortShow: false,
  21. reversed: 0, //是否翻转
  22. condition: "",
  23. history: [], //搜索历史
  24. showHistory: false,
  25. listHeight: null,
  26. },
  27. lifetimes: {
  28. attached() {
  29. if (wx.getStorageSync('SearchHistory')) this.setData({
  30. history: wx.getStorageSync('SearchHistory')
  31. })
  32. },
  33. ready() {
  34. let windowHeight = 0,
  35. that = this;
  36. wx.getSystemInfo({
  37. success: (res => windowHeight = res.windowHeight)
  38. });
  39. let query = wx.createSelectorQuery().in(that).select('.Yl_head').boundingClientRect();
  40. query.exec(res => that.setData({
  41. listHeight: (windowHeight - res[0].bottom - 80) * 2
  42. }))
  43. }
  44. },
  45. methods: {
  46. /* 排序 */
  47. sortClick(e) {
  48. const {
  49. name
  50. } = e.target.dataset;
  51. if (name == 'confirm') {
  52. let sort = this.data.sort.find(v => v.sorted == 1),
  53. i = this.data.list.findIndex(v => v.id == 'sort');
  54. this.setData({
  55. [`list[${i}].label`]: sort.sortname,
  56. [`list[${i}].icon`]: sort.reversed == 1 ? 'icon-jiangxu1' : 'icon-shengxu',
  57. })
  58. let pages = getCurrentPages(),
  59. page = pages[pages.length - 1];
  60. page.setData({
  61. "content.sort": this.data.sort
  62. });
  63. page.getList(true);
  64. };
  65. this.setData({
  66. sortShow: false
  67. })
  68. },
  69. sortClose() {
  70. this.setData({
  71. sortShow: false
  72. })
  73. },
  74. /* 设置排序规则 */
  75. handleSort(e) {
  76. const {
  77. id
  78. } = e.currentTarget.dataset;
  79. this.setData({
  80. sort: this.data.sort.map(v => {
  81. v.sorted = v.sortid == id ? 1 : 0
  82. return v;
  83. })
  84. });
  85. },
  86. /* 设置升序和倒叙 */
  87. handleReversed(e) {
  88. let {
  89. id
  90. } = e.currentTarget.dataset;
  91. if (this.data.reversed == id) return;
  92. this.setData({
  93. reversed: id,
  94. sort: this.data.sort.map(v => {
  95. v.reversed = id;
  96. return v;
  97. })
  98. });
  99. },
  100. onClick(e) {
  101. const {
  102. item
  103. } = e.currentTarget.dataset;
  104. if (item.id == "sort") {
  105. //排序
  106. this.setData({
  107. sortShow: true
  108. })
  109. } else {
  110. this.triggerEvent("onClick", item)
  111. }
  112. },
  113. /* 开启关闭搜索 */
  114. clickSearch() {
  115. this.setData({
  116. startUsing: !this.data.startUsing
  117. });
  118. setTimeout(this.setListHeight, 400)
  119. },
  120. /* 开始搜索 */
  121. startSearch({
  122. detail
  123. }) {
  124. if (this.data.condition == detail) return;
  125. this.setData({
  126. condition: detail
  127. })
  128. this.triggerEvent("onSearch", detail)
  129. if (this.data.record || detail == '') {
  130. let list = this.data.history;
  131. if (list.findIndex(v => v == detail) == -1) {
  132. list.push(detail)
  133. this.setData({
  134. history: list
  135. });
  136. wx.setStorageSync("SearchHistory", list)
  137. }
  138. }
  139. },
  140. /* 取消搜索 */
  141. endSearch() {
  142. this.setData({
  143. condition: ""
  144. })
  145. this.triggerEvent("onSearch", "")
  146. },
  147. /* 删除搜索历史 */
  148. deleteHistory(e) {
  149. let that = this;
  150. wx.showModal({
  151. title: '提示',
  152. content: '是否删除所有搜索历史',
  153. complete: ({
  154. confirm
  155. }) => {
  156. if (confirm) {
  157. wx.setStorageSync("SearchHistory", [])
  158. that.setData({
  159. history: []
  160. });
  161. this.setListHeight();
  162. }
  163. }
  164. })
  165. },
  166. /* 快速搜索 */
  167. clickTag(e) {
  168. const {
  169. name
  170. } = e.currentTarget.dataset;
  171. this.setData({
  172. condition: name
  173. });
  174. this.triggerEvent("onSearch", name)
  175. },
  176. /* 单独删除 */
  177. delteTag(e) {
  178. const {
  179. name
  180. } = e.currentTarget.dataset;
  181. this.setData({
  182. history: this.data.history.filter(v => v != name)
  183. });
  184. wx.setStorageSync('SearchHistory', this.data.history);
  185. this.setListHeight();
  186. },
  187. /* 设置列表高度 */
  188. setListHeight() {
  189. let pages = getCurrentPages();
  190. if (pages[pages.length - 1].setListHeight) pages[pages.length - 1].setListHeight();
  191. },
  192. /* 搜索框焦点 */
  193. onFocus() {
  194. this.setData({
  195. showHistory: true
  196. });
  197. setTimeout(this.setListHeight, 50);
  198. },
  199. /* 搜索框失焦 */
  200. onBlur() {
  201. this.setData({
  202. showHistory: false
  203. })
  204. setTimeout(this.setListHeight, 50);
  205. }
  206. }
  207. })