searchBox.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <script>
  2. import handle from "../../until/handle";
  3. import {rCode} from "../../map/rcodeMap_esm";
  4. import Loading from "../public/loading.vue";
  5. import HideScroll from "../public/hideScroll.vue";
  6. export default {
  7. name: "searchBox",
  8. components: {Loading, HideScroll},
  9. props: {
  10. searchPlaceholder: {
  11. type: String,
  12. default: '输入搜索关键字'
  13. },
  14. loadTip: {
  15. type: String,
  16. default: '加载中'
  17. },
  18. loadMsg: {
  19. type: String,
  20. default: '加载失败'
  21. },
  22. limit: {
  23. type: Number,
  24. default: 20
  25. },
  26. loadDataApi: {
  27. type: Function,
  28. default: () => {}
  29. },
  30. },
  31. data(){
  32. return {
  33. searchLoading:false,// 搜索加载框
  34. searchTimer: null,// 防抖搜索框
  35. searchKey: '',// 关键字
  36. page: 1,// 页数
  37. count: 20,// 每页数量
  38. total: 0,// 当前数据总量
  39. loadState: 0,// 加载状态
  40. loadLock: false,// 加载锁
  41. domPlaceholder: [],//当前的数据列表
  42. }
  43. },
  44. methods: {
  45. async loadData(page,isPush){
  46. if(isPush){
  47. page = 1;
  48. this.loadState = 0;
  49. }
  50. this.searchLoading = true;
  51. // console.log('---------')
  52. // console.log(page);
  53. let searchParam = {
  54. }
  55. // 1.搜索相关设备
  56. let [err,res] = await this.loadDataApi({
  57. key: this.searchKey,//
  58. page: page,// 当前页数
  59. });
  60. this.loadLock = false;
  61. this.searchLoading = false;
  62. // 2.检查返回值是否正常
  63. if (err){
  64. console.log(err);
  65. this.msg = err.message;
  66. isPush?this.loadState=2:this.$message.error(this.msg);
  67. return false;
  68. }
  69. this.loadState = 1;
  70. // 只有在第一页时会需要加载总数
  71. if(res.page <= 1){
  72. this.total = res.total;
  73. }
  74. // 3.根据是否为新设备创建新数据集合
  75. if(isPush || page===1){
  76. this.domPlaceholder = new Array(res.total)
  77. .fill({
  78. loaded: false,
  79. val:{}
  80. });
  81. }
  82. console.log('total' + this.domPlaceholder.length)
  83. // 4.将当前的分块请求结果填充至指定位置
  84. let startIndex = (res.page - 1) * res.limit;
  85. let key = res.key;
  86. console.log(`startIndex:${startIndex} , endIndex:${res.count+startIndex}`);
  87. for (let i = startIndex,n=0;n<res.count;n++,i++){
  88. let item = res.data[n];
  89. if(item){
  90. // 时间字段转换
  91. // item.subTitle = time.dateFormat(time.timeStamp_to_Date(item.loginTime),'YY-MM-DD H:m:s');
  92. // 展示字段转换
  93. item['innerText'] = item.showText.replace(key,`<span style="color:red;">${key}</span>`);
  94. }
  95. this.$set(this.domPlaceholder,i,{
  96. loaded:!!item,
  97. val:item?item:{}
  98. });
  99. }
  100. // 判断当前位置
  101. },
  102. onSearch(v){
  103. console.log('search');
  104. console.log(v);
  105. if(this.loadLock){
  106. this.$message.info('加载设备列表中')
  107. return
  108. }
  109. this.loadLock = true;
  110. this.loadData(1);
  111. },
  112. async onScrollChange(data){
  113. let scrollTop = data.scrollTop;
  114. let index = data.index;
  115. console.log('onScrollChange'+scrollTop + 'index:' + index);
  116. //获取
  117. let page = Math.floor(scrollTop/50/this.limit);
  118. console.log(page);
  119. // 加载数据
  120. if(page < this.page){
  121. return console.log(`不加载数据`)
  122. }
  123. await this.loadData(page + 1);
  124. if(index + this.limit > this.total){
  125. console.log(`同时获取下一页数据`);
  126. await this.loadData(page + 2);
  127. }
  128. },
  129. onSelectedItem(item){
  130. this.$emit('onSelectedItem',item);
  131. }
  132. }
  133. }
  134. </script>
  135. <template>
  136. <div class="w-full h-full rounded bg-yellow-50 flex flex-col">
  137. <div class="w-full h-auto">
  138. <!-- 其他搜索项 -->
  139. <slot name="otherSearchItem"></slot>
  140. </div>
  141. <div class="serarch mt-1 box-border px-1">
  142. <a-input-search
  143. :placeholder="searchPlaceholder"
  144. enter-button
  145. :loading="searchLoading"
  146. v-model="searchKey"
  147. @search="onSearch"
  148. allow-clear
  149. style="border-radius: 0;"
  150. />
  151. </div>
  152. <div class="devices w-full h-48 md:h-full bg-white mt-2 relative">
  153. <loading
  154. :loading-state="loadState"
  155. :tip="loadTip"
  156. >
  157. <hide-scroll
  158. @onScroll="onScrollChange"
  159. :itemHeight="50"
  160. >
  161. <div
  162. v-for="(item,i) in domPlaceholder"
  163. :key="'search-key:'+i"
  164. class="item cursor-default" >
  165. <div
  166. v-if="item.loaded"
  167. class="text text-base">
  168. <span v-html="item.val.innerText"></span>
  169. <div class="subText text-gray-300 text-xs">{{ item.val.subTitle }}</div>
  170. </div>
  171. <div
  172. v-if="item.loaded"
  173. class="action">
  174. <a-button class="mx-1" @click="onSelectedItem(item.val)">选中</a-button>
  175. </div>
  176. <div class="w-full h-full bg-black text-red-500 opacity-30" v-else-if="!item.loaded">
  177. {{i}}.....loading
  178. </div>
  179. </div>
  180. </hide-scroll>
  181. <template v-slot:loadFail>
  182. <div class="w-full h-full flex justify-center items-center">
  183. <h2 class="text-2xl text-red-700">{{loadMsg}}</h2>
  184. <a-button primary @click="loadData(1)">重新加载</a-button>
  185. </div>
  186. </template>
  187. </loading>
  188. </div>
  189. </div>
  190. </template>
  191. <style scoped>
  192. .item{
  193. width: 100%;
  194. height: 50px;
  195. display: flex;
  196. align-items: center;
  197. border-bottom: 1px solid #66ccff;
  198. padding: 0 0.25em;
  199. box-sizing: border-box;
  200. }
  201. .text{
  202. width: 100%;
  203. height: 100%;
  204. }
  205. .subText{
  206. width: 100%;
  207. color: #afa7a7;
  208. }
  209. .action{
  210. display: flex;
  211. }
  212. </style>