index.js 7.8 KB

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