index.js 7.9 KB

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