index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. const getHeight = require("../../utils/getHeight");
  2. Component({
  3. options: {
  4. addGlobalClass: true
  5. },
  6. properties: {
  7. sort: {
  8. type: Array
  9. }, //排序规则列表
  10. search: {
  11. type: Boolean
  12. }, //是否开启搜索
  13. list: {
  14. type: Array
  15. }, //功能列表
  16. condition: {
  17. type: String
  18. }, //搜索内容
  19. onClick: {
  20. type: Function
  21. },
  22. startUsing: {
  23. type: Boolean
  24. }, //启用搜索
  25. onSearch: {
  26. type: Function
  27. }, //搜索回调
  28. record: { //记录历史
  29. type: Boolean,
  30. value: true
  31. }
  32. },
  33. data: {
  34. sortShow: false,
  35. reversed: 0, //是否翻转
  36. condition: "",
  37. history: [], //搜索历史
  38. showHistory: false,
  39. listHeight: null,
  40. },
  41. lifetimes: {
  42. attached() {
  43. if (wx.getStorageSync('SearchHistory')) this.setData({
  44. history: wx.getStorageSync('SearchHistory')
  45. })
  46. getApp().globalData.Language.getLanguagePackage(this)
  47. },
  48. ready() {
  49. getHeight.getHeight('.Yl_head', this).then(res => this.setData({
  50. listHeight: res - 80
  51. }));
  52. }
  53. },
  54. methods: {
  55. /* 排序 */
  56. sortClick(e) {
  57. const {
  58. name
  59. } = e.target.dataset;
  60. if (name == 'confirm') {
  61. let sort = this.data.sort.find(v => v.sorted == 1),
  62. i = this.data.list.findIndex(v => v.id == 'sort');
  63. this.setData({
  64. [`list[${i}].label`]: sort.sortname,
  65. [`list[${i}].icon`]: sort.reversed == 0 ? 'icon-jiangxu1' : 'icon-shengxu',
  66. })
  67. let pages = getCurrentPages(),
  68. page = pages[pages.length - 1];
  69. page.setData({
  70. "content.sort": this.data.sort
  71. });
  72. page.getList(true);
  73. };
  74. this.setData({
  75. sortShow: false
  76. })
  77. },
  78. sortClose() {
  79. this.setData({
  80. sortShow: false
  81. })
  82. },
  83. /* 设置排序规则 */
  84. handleSort(e) {
  85. const {
  86. id
  87. } = e.currentTarget.dataset;
  88. this.setData({
  89. sort: this.data.sort.map(v => {
  90. v.sorted = v.sortid == id ? 1 : 0
  91. return v;
  92. })
  93. });
  94. },
  95. /* 设置升序和倒叙 */
  96. handleReversed(e) {
  97. let {
  98. id
  99. } = e.currentTarget.dataset;
  100. if (this.data.reversed == id) return;
  101. this.setData({
  102. reversed: id,
  103. sort: this.data.sort.map(v => {
  104. v.reversed = id;
  105. return v;
  106. })
  107. });
  108. },
  109. onClick(e) {
  110. const {
  111. item
  112. } = e.currentTarget.dataset;
  113. if (item.id == "sort") {
  114. //排序
  115. this.setData({
  116. sortShow: true
  117. })
  118. } else {
  119. this.triggerEvent("onClick", item)
  120. }
  121. },
  122. /* 开启关闭搜索 */
  123. clickSearch() {
  124. this.setData({
  125. startUsing: !this.data.startUsing
  126. });
  127. setTimeout(this.setListHeight, 400)
  128. },
  129. /* 开始搜索 */
  130. startSearch({
  131. detail
  132. }) {
  133. if (this.data.condition == detail) return;
  134. this.setData({
  135. condition: detail
  136. })
  137. this.triggerEvent("onSearch", detail)
  138. if (this.data.record || detail == '') {
  139. let list = this.data.history;
  140. if (list.findIndex(v => v == detail) == -1) {
  141. list.push(detail)
  142. this.setData({
  143. history: list
  144. });
  145. wx.setStorageSync("SearchHistory", list)
  146. }
  147. }
  148. },
  149. /* 取消搜索 */
  150. endSearch() {
  151. this.setData({
  152. condition: ""
  153. })
  154. this.triggerEvent("onSearch", "")
  155. },
  156. /* 删除搜索历史 */
  157. deleteHistory(e) {
  158. let that = this;
  159. wx.showModal({
  160. title: getApp().globalData.Language.getMapText('提示'),
  161. content: getApp().globalData.Language.getMapText('是否删除所有搜索历史'),
  162. cancelText: getApp().globalData.Language.getMapText('取消'),
  163. confirmText: getApp().globalData.Language.getMapText('确定'),
  164. complete: ({
  165. confirm
  166. }) => {
  167. if (confirm) {
  168. wx.setStorageSync("SearchHistory", [])
  169. that.setData({
  170. history: []
  171. });
  172. this.setListHeight();
  173. }
  174. }
  175. })
  176. },
  177. /* 快速搜索 */
  178. clickTag(e) {
  179. const {
  180. name
  181. } = e.currentTarget.dataset;
  182. this.setData({
  183. condition: name
  184. });
  185. this.triggerEvent("onSearch", name)
  186. },
  187. /* 单独删除 */
  188. delteTag(e) {
  189. const {
  190. name
  191. } = e.currentTarget.dataset;
  192. this.setData({
  193. history: this.data.history.filter(v => v != name)
  194. });
  195. wx.setStorageSync('SearchHistory', this.data.history);
  196. this.setListHeight();
  197. },
  198. /* 设置列表高度 */
  199. setListHeight() {
  200. let pages = getCurrentPages()[getCurrentPages().length - 1];
  201. let model = pages.selectComponent("#ListBox");
  202. if (model) model.automaticSetHei();
  203. },
  204. /* 搜索框焦点 */
  205. onFocus() {
  206. this.setData({
  207. showHistory: true
  208. });
  209. setTimeout(this.setListHeight, 50);
  210. },
  211. /* 搜索框失焦 */
  212. onBlur() {
  213. this.setData({
  214. showHistory: false
  215. })
  216. setTimeout(this.setListHeight, 50);
  217. }
  218. }
  219. })