index.js 8.3 KB

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