index.js 7.7 KB

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