index.js 8.0 KB

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