index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. Component({
  2. options: {
  3. addGlobalClass: true
  4. },
  5. properties: {
  6. search: Boolean, //是否开启搜索
  7. list: Array, //功能列表
  8. condition: String, //搜索内容
  9. onClick: Function,
  10. startUsing: Boolean, //启用搜索
  11. onSearch: Function, //搜索回调
  12. record: { //记录历史
  13. type: Boolean,
  14. value: true
  15. }
  16. },
  17. lifetimes: {
  18. attached() {
  19. if (wx.getStorageSync('SearchHistory')) this.setData({
  20. history: wx.getStorageSync('SearchHistory')
  21. })
  22. }
  23. },
  24. data: {
  25. condition: "",
  26. history: [], //搜索历史
  27. showHistory: false
  28. },
  29. methods: {
  30. onClick(e) {
  31. const {
  32. item
  33. } = e.currentTarget.dataset;
  34. this.triggerEvent("onClick", item)
  35. },
  36. /* 开启关闭搜索 */
  37. clickSearch() {
  38. this.setData({
  39. startUsing: !this.data.startUsing
  40. });
  41. setTimeout(this.setListHeight, 400)
  42. },
  43. /* 开始搜索 */
  44. startSearch({
  45. detail
  46. }) {
  47. if (this.data.condition == detail) return;
  48. this.setData({
  49. condition: detail
  50. })
  51. this.triggerEvent("onSearch", detail)
  52. if (this.data.record || detail == '') {
  53. let list = this.data.history;
  54. if (list.findIndex(v => v == detail) == -1) {
  55. list.push(detail)
  56. this.setData({
  57. history: list
  58. });
  59. wx.setStorageSync("SearchHistory", list)
  60. }
  61. }
  62. },
  63. /* 取消搜索 */
  64. endSearch() {
  65. this.setData({
  66. condition: ""
  67. })
  68. this.triggerEvent("onSearch", "")
  69. },
  70. /* 删除搜索历史 */
  71. deleteHistory(e) {
  72. let that = this;
  73. wx.showModal({
  74. title: '提示',
  75. content: '是否删除所有搜索历史',
  76. complete: ({
  77. confirm
  78. }) => {
  79. if (confirm) {
  80. wx.setStorageSync("SearchHistory", [])
  81. that.setData({
  82. history: []
  83. });
  84. this.setListHeight();
  85. }
  86. }
  87. })
  88. },
  89. /* 快速搜索 */
  90. clickTag(e) {
  91. const {
  92. name
  93. } = e.currentTarget.dataset;
  94. this.setData({
  95. condition: name
  96. });
  97. this.triggerEvent("onSearch", name)
  98. },
  99. /* 单独删除 */
  100. delteTag(e) {
  101. const {
  102. name
  103. } = e.currentTarget.dataset;
  104. this.setData({
  105. history: this.data.history.filter(v => v != name)
  106. });
  107. wx.setStorageSync('SearchHistory', this.data.history);
  108. this.setListHeight();
  109. },
  110. /* 设置列表高度 */
  111. setListHeight() {
  112. let pages = getCurrentPages();
  113. if (pages[pages.length - 1].setListHeight) pages[pages.length - 1].setListHeight();
  114. },
  115. /* 搜索框焦点 */
  116. onFocus() {
  117. this.setData({
  118. showHistory: true
  119. });
  120. setTimeout(this.setListHeight, 50);
  121. },
  122. /* 搜索框失焦 */
  123. onBlur() {
  124. this.setData({
  125. showHistory: false
  126. })
  127. setTimeout(this.setListHeight, 50);
  128. }
  129. }
  130. })