index.js 7.6 KB

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