index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. isFilter: {
  60. type: Boolean,
  61. value: false
  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) {
  173. page.setData({
  174. show: true,
  175. })
  176. } else {
  177. getCurrentPages()[getCurrentPages().length - 1].setData({
  178. modalName: 'viewModal'
  179. })
  180. }
  181. }
  182. },
  183. /* 开启关闭搜索 */
  184. clickSearch() {
  185. this.setData({
  186. startUsing: !this.data.startUsing
  187. });
  188. setTimeout(this.setListHeight, 400)
  189. },
  190. /* 搜索 */
  191. confirmSearch(e) {
  192. if (this.data.condition == e.detail.value) return;
  193. this.setData({
  194. condition: e.detail.value
  195. })
  196. this.triggerEvent("onSearch", e.detail.value)
  197. },
  198. /* 开始搜索 */
  199. startSearch({
  200. detail
  201. }) {
  202. if (this.data.condition == detail) return;
  203. this.setData({
  204. condition: detail
  205. })
  206. this.triggerEvent("onSearch", detail)
  207. if (this.data.record || detail == '') {
  208. let list = this.data.history;
  209. if (list.findIndex(v => v == detail) == -1) {
  210. list.push(detail)
  211. this.setData({
  212. history: list
  213. });
  214. wx.setStorageSync("SearchHistory", list)
  215. }
  216. }
  217. },
  218. /* 取消搜索 */
  219. endSearch() {
  220. this.setData({
  221. condition: ""
  222. })
  223. this.triggerEvent("onSearch", "")
  224. },
  225. /* 删除搜索历史 */
  226. deleteHistory(e) {
  227. let that = this;
  228. wx.showModal({
  229. title: '提示',
  230. content: '是否删除所有搜索历史',
  231. complete: ({
  232. confirm
  233. }) => {
  234. if (confirm) {
  235. wx.setStorageSync("SearchHistory", [])
  236. that.setData({
  237. history: []
  238. });
  239. this.setListHeight();
  240. }
  241. }
  242. })
  243. },
  244. /* 快速搜索 */
  245. clickTag(e) {
  246. const {
  247. name
  248. } = e.currentTarget.dataset;
  249. this.setData({
  250. condition: name
  251. });
  252. this.triggerEvent("onSearch", name)
  253. },
  254. /* 单独删除 */
  255. delteTag(e) {
  256. const {
  257. name
  258. } = e.currentTarget.dataset;
  259. this.setData({
  260. history: this.data.history.filter(v => v != name)
  261. });
  262. wx.setStorageSync('SearchHistory', this.data.history);
  263. this.setListHeight();
  264. },
  265. /* 设置列表高度 */
  266. setListHeight() {
  267. let pages = getCurrentPages();
  268. if (pages[pages.length - 1].setListHeight) pages[pages.length - 1].setListHeight();
  269. },
  270. /* 搜索框焦点 */
  271. onFocus() {
  272. this.setData({
  273. showHistory: true
  274. });
  275. setTimeout(this.setListHeight, 50);
  276. },
  277. /* 搜索框失焦 */
  278. onBlur() {
  279. this.setData({
  280. showHistory: false
  281. })
  282. setTimeout(this.setListHeight, 50);
  283. }
  284. }
  285. })