index.js 6.8 KB

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