index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const getHeight = require("../../utils/getHeight");
  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. getHeight.getHeight('.Yl_head', this).then(res => this.setData({
  44. listHeight: res - 80
  45. }));
  46. }
  47. },
  48. methods: {
  49. /* 排序 */
  50. sortClick(e) {
  51. const {
  52. name
  53. } = e.target.dataset;
  54. if (name == 'confirm') {
  55. let sort = this.data.sort.find(v => v.sorted == 1),
  56. i = this.data.list.findIndex(v => v.id == 'sort');
  57. this.setData({
  58. [`list[${i}].label`]: sort.sortname,
  59. [`list[${i}].icon`]: sort.reversed == 0 ? 'icon-jiangxu1' : 'icon-shengxu',
  60. })
  61. let pages = getCurrentPages(),
  62. page = pages[pages.length - 1];
  63. page.setData({
  64. "content.sort": this.data.sort
  65. });
  66. page.getList(true);
  67. };
  68. this.setData({
  69. sortShow: false
  70. })
  71. },
  72. sortClose() {
  73. this.setData({
  74. sortShow: false
  75. })
  76. },
  77. /* 设置排序规则 */
  78. handleSort(e) {
  79. const {
  80. id
  81. } = e.currentTarget.dataset;
  82. this.setData({
  83. sort: this.data.sort.map(v => {
  84. v.sorted = v.sortid == id ? 1 : 0
  85. return v;
  86. })
  87. });
  88. },
  89. /* 设置升序和倒叙 */
  90. handleReversed(e) {
  91. let {
  92. id
  93. } = e.currentTarget.dataset;
  94. if (this.data.reversed == id) return;
  95. this.setData({
  96. reversed: id,
  97. sort: this.data.sort.map(v => {
  98. v.reversed = id;
  99. return v;
  100. })
  101. });
  102. },
  103. onClick(e) {
  104. const {
  105. item
  106. } = e.currentTarget.dataset;
  107. if (item.id == "sort") {
  108. //排序
  109. this.setData({
  110. sortShow: true
  111. })
  112. } else {
  113. this.triggerEvent("onClick", item)
  114. }
  115. },
  116. /* 开启关闭搜索 */
  117. clickSearch() {
  118. this.setData({
  119. startUsing: !this.data.startUsing
  120. });
  121. setTimeout(this.setListHeight, 400)
  122. },
  123. /* 开始搜索 */
  124. startSearch({
  125. detail
  126. }) {
  127. if (this.data.condition == detail) return;
  128. this.setData({
  129. condition: detail
  130. })
  131. this.triggerEvent("onSearch", detail)
  132. if (this.data.record || detail == '') {
  133. let list = this.data.history;
  134. if (list.findIndex(v => v == detail) == -1) {
  135. list.push(detail)
  136. this.setData({
  137. history: list
  138. });
  139. wx.setStorageSync("SearchHistory", list)
  140. }
  141. }
  142. },
  143. /* 取消搜索 */
  144. endSearch() {
  145. this.setData({
  146. condition: ""
  147. })
  148. this.triggerEvent("onSearch", "")
  149. },
  150. /* 删除搜索历史 */
  151. deleteHistory(e) {
  152. let that = this;
  153. wx.showModal({
  154. title: '提示',
  155. content: '是否删除所有搜索历史',
  156. complete: ({
  157. confirm
  158. }) => {
  159. if (confirm) {
  160. wx.setStorageSync("SearchHistory", [])
  161. that.setData({
  162. history: []
  163. });
  164. this.setListHeight();
  165. }
  166. }
  167. })
  168. },
  169. /* 快速搜索 */
  170. clickTag(e) {
  171. const {
  172. name
  173. } = e.currentTarget.dataset;
  174. this.setData({
  175. condition: name
  176. });
  177. this.triggerEvent("onSearch", name)
  178. },
  179. /* 单独删除 */
  180. delteTag(e) {
  181. const {
  182. name
  183. } = e.currentTarget.dataset;
  184. this.setData({
  185. history: this.data.history.filter(v => v != name)
  186. });
  187. wx.setStorageSync('SearchHistory', this.data.history);
  188. this.setListHeight();
  189. },
  190. /* 设置列表高度 */
  191. setListHeight() {
  192. let pages = getCurrentPages();
  193. if (pages[pages.length - 1].setListHeight) pages[pages.length - 1].setListHeight();
  194. },
  195. /* 搜索框焦点 */
  196. onFocus() {
  197. this.setData({
  198. showHistory: true
  199. });
  200. setTimeout(this.setListHeight, 50);
  201. },
  202. /* 搜索框失焦 */
  203. onBlur() {
  204. this.setData({
  205. showHistory: false
  206. })
  207. setTimeout(this.setListHeight, 50);
  208. }
  209. }
  210. })