search_box.js 1.5 KB

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