navbarSearch1.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!--
  2. * props
  3. * list /op/geiIntegratedDataMenus/getMenu 接口回传参数
  4. * $emit
  5. * selectedIteml 选中项的source属性值 查询使用
  6. -->
  7. <template>
  8. <view>
  9. <u-navbar
  10. :back-text="i18n('return')"
  11. :back-text-style="backStyle"
  12. back-icon-color="#fff" title-color="#fff"
  13. :back-icon-size="28"
  14. :background="popupShow?whiteBackground:background"
  15. style="z-index: 12000;position: relative;"
  16. :class="[popupShow?'isSearch':'isNotSearch']"
  17. >
  18. <view class="search-wrap">
  19. <u-search v-model="searchInput"
  20. :show-action="false"
  21. height="56"
  22. :action-style="{color: '#fff'}"
  23. :placeholder="$i18n.locale == 'zh'? '请输入关键字': 'Please input keywords'"
  24. @change="searchChange"
  25. @focus="searchFocus"
  26. ></u-search>
  27. </view>
  28. <view class="navbar-right" slot="right" v-if="popupShow">
  29. <view class="dot-box right-item">
  30. <text style="margin-right: 20upx;" @click="searchBlue">{{i18n('Cancel')}}</text>
  31. </view>
  32. </view>
  33. </u-navbar>
  34. <u-popup v-model="popupShow"
  35. mode="top">
  36. <!-- #ifdef H5 -->
  37. <view style="height: 88upx;"></view>
  38. <!-- #endif -->
  39. <!-- #ifdef APP-PLUS -->
  40. <view style="height: 150upx;"></view>
  41. <!-- #endif -->
  42. <view style="max-height: 800upx;overflow: auto;">
  43. <view v-for="(item,index) in searchResultListFilter"
  44. @click="selectedItem(item,index)"
  45. class="listText">
  46. <text v-if="$i18n.locale == 'zh'" style="line-height: 60upx;"><text v-html="item.indexName"></text></text>
  47. <text v-if="$i18n.locale == 'en'" style="line-height: 60upx;"><text v-html="item.indexEnName"></text></text>
  48. </view>
  49. <view v-if="searchResultListFilter.length ==0"
  50. style="text-align: center;">
  51. <text v-if="$i18n.locale == 'zh'" style="line-height: 60upx;">无相关搜索</text>
  52. <text v-if="$i18n.locale == 'en'" style="line-height: 60upx;">Unrelated search</text>
  53. </view>
  54. </view>
  55. </u-popup>
  56. </view>
  57. </template>
  58. <script>
  59. export default {
  60. name: "navbarSearch",
  61. props: {
  62. backStyle: {
  63. type: Object,
  64. default: ()=>{
  65. return {
  66. color: '#fff',
  67. fontSize: '28upx'
  68. }
  69. }
  70. },
  71. background: {
  72. type: Object,
  73. default: ()=>{
  74. return {
  75. backgroundImage: 'linear-gradient(270deg, #4BC0E2 0%, #538BE7 100%)'
  76. }
  77. }
  78. },
  79. list: {
  80. type: Array,
  81. default: () => []
  82. }
  83. },
  84. computed:{
  85. searchResultList() {
  86. let arr=[]
  87. this.setIcon(this.list,'child',arr,0)
  88. return arr;
  89. },
  90. searchResultListMap() {
  91. let replaceReg = new RegExp(this.searchInput, 'g')
  92. let replaceString = '<span style="color:red">' + this.searchInput + '</span>'
  93. let arr=this.searchResultList.map(item=>{
  94. return {
  95. indexEnName:item.indexEnName.replace(replaceReg,replaceString),
  96. indexEnNameText:item.indexEnName,
  97. id:item.id,
  98. source:item.source,
  99. nodeId:item.nodeId,
  100. indexName:item.indexName.replace(replaceReg,replaceString),
  101. indexNameText:item.indexName
  102. }
  103. })
  104. return arr;
  105. },
  106. searchResultListFilter() {
  107. let arr=[]
  108. if(this.$i18n.locale=='zh'){
  109. arr = this.searchResultListMap.filter(item=>item.indexName.indexOf(this.searchInput)!=-1)
  110. }
  111. if(this.$i18n.locale=='en'){
  112. arr = this.searchResultListMap.filter(item=>item.indexEnName.indexOf(this.searchInput)!=-1)
  113. }
  114. return arr;
  115. }
  116. },
  117. data() {
  118. return {
  119. popupShow: false,
  120. searchInput: '',
  121. whiteBackground:{
  122. background: '#ffffff'
  123. }
  124. }
  125. },
  126. methods: {
  127. setIcon(data,key,arr,index){
  128. let ind = index
  129. data.forEach(item=>{
  130. if(index < 3){
  131. if(item[key] && item[key].length > 0){
  132. this.setIcon(item[key],key,arr,ind+1)
  133. }else{
  134. arr.push(item)
  135. }
  136. }else{
  137. }
  138. })
  139. },
  140. i18n(data) {
  141. return this.$t('common.' + data);
  142. },
  143. selectedItem(item,index) {
  144. if(this.$i18n.locale=='zh'){
  145. this.searchInput = item.indexNameText
  146. }
  147. if(this.$i18n.locale=='en'){
  148. this.searchInput = item.indexEnNameText
  149. }
  150. this.searchBlue()
  151. this.$emit('selectedItem',{item})
  152. },
  153. searchChange(value) {
  154. if(value===''){
  155. this.$emit('selectedItem',{item:{}})
  156. }
  157. console.log('**',this.searchResultList)
  158. console.log('**',this.searchResultListMap)
  159. console.log('**',this.searchResultListFilter)
  160. },
  161. searchFocus(value) {
  162. this.popupShow = true
  163. },
  164. searchBlue(value) {
  165. this.popupShow = false
  166. },
  167. clearInput(){
  168. this.searchInput = '';
  169. this.$emit('selectedItem',{item:{}})
  170. }
  171. }
  172. }
  173. </script>
  174. <style scoped lang="scss">
  175. .search-wrap {
  176. margin: 0 20rpx;
  177. flex: 1;
  178. }
  179. .listText{
  180. width: 100%;
  181. border-bottom: 1px solid #efefef;
  182. padding: 0 30upx;
  183. box-sizing: border-box;
  184. }
  185. .isSearch /deep/ .u-back-wrap{
  186. display: none;
  187. }
  188. </style>