index.js 6.3 KB

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