searchbar.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // components/searchbar.js
  2. const app = getApp();
  3. Component({
  4. options: {
  5. addGlobalClass: true,
  6. },
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. paramAtoB: {
  12. type: String,//类型
  13. value: 'default value'//默认值
  14. },
  15. currentvalue: {
  16. type: String,//类型
  17. value: '',//默认值
  18. observer: function(newVal, oldVal) {
  19. // 属性被改变时执行的函数(可选)
  20. this.setData({
  21. inputVal:newVal
  22. })
  23. }
  24. },
  25. disabled: {
  26. type: Boolean,
  27. value: false
  28. },
  29. placeholder: {
  30. type: String,
  31. value: ''
  32. }
  33. },
  34. /**
  35. * 组件的初始数据
  36. */
  37. data: {
  38. inputShowed: false,
  39. timer: null,
  40. inputVal: ""
  41. },
  42. /**
  43. * 组件的方法列表
  44. */
  45. methods: {
  46. backfill: function (e) {
  47. var id = e.currentTarget.id;
  48. for (var i = 0; i < this.data.suggestion.length;i++){
  49. if(i == id){
  50. this.setData({
  51. backfill: this.data.suggestion[i].title
  52. });
  53. }
  54. }
  55. },
  56. //触发关键词输入提示事件
  57. getsuggest: function(e) {
  58. var _this = this;
  59. var old_timer = this.data.timer;
  60. if (old_timer) {
  61. clearTimeout(old_timer)
  62. }
  63. this.setData({
  64. timer:setTimeout(() => {
  65. if(e.detail.value){
  66. app.globalData.qqmapsdk.getSuggestion({
  67. //获取输入框值并设置keyword参数
  68. keyword: e.detail.value, //用户输入的关键词,可设置固定值,如keyword:'KFC'
  69. region:app.globalData.city, //设置城市名,限制关键词所示的地域范围,非必填参数
  70. success: function(res) {//搜索成功后的回调
  71. var sug = [];
  72. for (var i = 0; i < res.data.length; i++) {
  73. sug.push(
  74. res.data[i].title,
  75. )
  76. }
  77. _this.triggerEvent('SearchList',sug);
  78. },
  79. });
  80. }else{ _this.triggerEvent('SearchList',[]);}
  81. // 调用关键词提示接口
  82. }, 700),
  83. inputVal:e.detail.value,
  84. })
  85. },
  86. toSearch:function(e){
  87. var value = this.data.inputVal;
  88. this.triggerEvent('ChildInputValue',value);
  89. },
  90. }
  91. })