index.js 5.9 KB

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