index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // component/wx-index-list/wx-index-list.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. data: {
  8. type: Object,
  9. value: {},
  10. },
  11. myCity: {
  12. type: String,
  13. value: "",
  14. },
  15. // 用于外部组件搜索使用
  16. search: {
  17. type: String,
  18. value: "",
  19. observer: function (newVal, oldVal) {
  20. console.log(newVal)
  21. this.value = newVal;
  22. this.searchMt();
  23. }
  24. }
  25. },
  26. data: {
  27. list: [],
  28. rightArr: [],// 右侧字母展示
  29. jumpNum: '',//跳转到那个字母
  30. myCityName: '请选择' // 默认我的城市
  31. },
  32. ready() {
  33. let data = this.data.data;
  34. this.resetRight(data);
  35. console.log('--------------',this.myCity)
  36. // if (this.data.myCity) {
  37. // this.getCity()
  38. // }
  39. },
  40. methods: {
  41. // 数据重新渲染
  42. resetRight(data) {
  43. let rightArr = []
  44. for (let i in data) {
  45. rightArr.push(data[i].title.substr(0, 1));
  46. }
  47. this.setData({
  48. list: data,
  49. rightArr
  50. })
  51. },
  52. getCity() {
  53. wx.getLocation({
  54. type: 'wgs84',
  55. success: function (res) {
  56. this.latitude = res.latitude;
  57. this.longitude = res.longitude;
  58. // console.log(res)
  59. }
  60. })
  61. },
  62. // 右侧字母点击事件
  63. jumpMt(e) {
  64. let jumpNum = e.currentTarget.dataset.id;
  65. this.setData({ jumpNum });
  66. },
  67. // 列表点击事件
  68. detailMt(e) {
  69. let detail = e.currentTarget.dataset.detail;
  70. let myEventOption = {
  71. bubbles: false,//事件是否冒泡
  72. composed: false,//事件是否可以穿越组件边界
  73. capturePhase: false //事件是否拥有捕获阶段
  74. } // 触发事件的选项
  75. this.triggerEvent('detail', detail, myEventOption)
  76. },
  77. // 获取搜索输入内容
  78. input(e) {
  79. this.value = e.detail.value;
  80. },
  81. // 基础搜索功能
  82. searchMt(e) {
  83. this.value = e.detail.value;
  84. this._search();
  85. },
  86. _search() {
  87. let data = this.data.data;
  88. let newData = [];
  89. for (let i = 0; i < data.length; i++) {
  90. let itemArr = [];
  91. for (let j = 0; j < data[i].item.length; j++) {
  92. if (data[i].item[j].name.indexOf(this.value) > -1) {
  93. let itemJson = {};
  94. for (let k in data[i].item[j]) {
  95. itemJson[k] = data[i].item[j][k];
  96. }
  97. itemArr.push(itemJson);
  98. }
  99. }
  100. if (itemArr.length === 0) {
  101. continue;
  102. }
  103. newData.push({
  104. title: data[i].title,
  105. type: data[i].type ? data[i].type : "",
  106. item: itemArr
  107. })
  108. }
  109. this.resetRight(newData);
  110. },
  111. // 城市定位
  112. locationMt() {
  113. // TODO 暂时没有实现 定位自己的城市,需要引入第三方api
  114. },
  115. // 重新定位
  116. locationRe() {
  117. let myEventOption = {
  118. bubbles: false,//事件是否冒泡
  119. composed: false,//事件是否可以穿越组件边界
  120. capturePhase: false //事件是否拥有捕获阶段
  121. }
  122. // 触发事件的选项
  123. this.triggerEvent('relocation', myEventOption)
  124. }
  125. }
  126. })