search_box.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // pages/components/blocks/search_box/search_box.js
  2. const getReq = require('../../../../config.js').getReq;
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. },
  9. /**
  10. * 组件的初始数据
  11. */
  12. data: {
  13. inputVal:'',
  14. searchItems:[]
  15. },
  16. /**
  17. * 组件的方法列表
  18. */
  19. methods: {
  20. skip_target(e){
  21. let word = e.target.dataset.word;
  22. wx.navigateTo({
  23. url: `/pages/brand/brand?keyword=${word}&title=${word}`,
  24. })
  25. },
  26. search_btn(){
  27. let inputVal = this.data.inputVal;
  28. if (!inputVal) {
  29. return;
  30. }
  31. wx.navigateTo({
  32. url: `/pages/brand/brand?keyword=${inputVal}&title=${inputVal}`,
  33. })
  34. },
  35. input(e){
  36. let self = this;
  37. let val = e.detail.value;
  38. this.setData({
  39. inputVal: val
  40. });
  41. if (val == "") {
  42. clearTimeout(this.search);
  43. self.setData({
  44. searchItems: []
  45. })
  46. return;
  47. }
  48. clearTimeout(this.search);
  49. this.search = setTimeout(function(){
  50. getReq({
  51. act: 'search',
  52. op: 'suggest_words',
  53. keyword: val
  54. }, function (res) {
  55. if (res.code == 200) {
  56. if(res.datas.words == null) {
  57. self.setData({
  58. searchItems:[]
  59. })
  60. }
  61. else {
  62. self.setData({
  63. searchItems: res.datas.words
  64. });
  65. }
  66. }
  67. })
  68. },300);
  69. }
  70. }
  71. })