wx-index-list.js 3.5 KB

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