//keep-alive包裹需更新组件
当前位置:   article > 正文

Vue实现搜索页面_vue搜索页面

vue搜索页面

目录

一、效果

二、实现


一、效果:

实现功能如下图所示:(因为被腰斩,样式未调整)

二、实现

直接上代码:

  1. pages/Search.vue //搜索页
  2. <template>
  3. <div class="search-page">
  4. <page-header></page-header>
  5. <keep-alive> //keep-alive包裹需更新组件
  6. <search-content></search-content>
  7. </keep-alive>
  8. <page-footer></page-footer>
  9. </div>
  10. </template>
  11. <script>
  12. import PageHeader from '../components/PageHeader'
  13. import SearchContent from '../components/SearchContent'
  14. import PageFooter from '../components/PageFooter'
  15. export default {
  16. name: 'Search',
  17. components: {PageHeader, SearchContent, PageFooter},
  18. beforeRouteUpdate (to, from, next) {
  19. console.log(to, from, next)
  20. if (to.query.input !== from.query.input) {
  21. next()
  22. }
  23. }
  24. }
  25. </script>
  1. components/SearchContent.vue //搜索内容组件
  2. <template>
  3. <div class="search-content">
  4. <div class="searchContentImg"> //背景图片
  5. <img id="scimg" src="../assets/welcome.png" alt="">
  6. </div>
  7. <div class="secSearch">
  8. <div class="searchBox"> //搜索框
  9. <el-input v-model="input" placeholder="按关键词搜索" class="search" @keyup.enter.native="handleJump(input)"></el-input>
  10. <a @click="handleJump(input)"><i class="el-icon-search"></i></a>
  11. </div>
  12. //搜索内容框高度动态变化 :style
  13. <div v-if="searchData.length>0" class="search-list" :style="{height: heightCss}">
  14. <p class="total-result" :total="total" ref="pTitle">共{{ total }}条</p>
  15. //搜索内容列表
  16. <div class="search-board" ref="ulList">
  17. <ul class="search-fix">
  18. <li v-for="(item, index) in searchData" :key="index">
  19. <span>{{ item }}</span>
  20. </li>
  21. </ul>
  22. </div>
  23. //搜索内容分页
  24. <el-pagination v-if="pageshow" @size-change="handleSizeChange" @current-change="handleCurrentChange"
  25. :current-page="page" :page-sizes="[1, 2,5, 10]" :page-size="limit"
  26. layout="total, sizes, prev, pager, next, jumper" :total="total" class="page" ref="pageHeight">
  27. </el-pagination>
  28. </div>
  29. <div v-else class="search-empty">
  30. <ul>
  31. <li>
  32. <img src="../assets/welcome.png" class="empty-img">
  33. </li>
  34. </ul>
  35. </div>
  36. </div>
  37. <div class="searchImg">
  38. <img id="simg" src="../assets/welcome.png" alt="">
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. export default {
  44. name: 'SearchContent',
  45. data: function () {
  46. return {
  47. input: this.$route.query.input,
  48. keyword: this.$route.query.input,
  49. heightCss: '',
  50. list: ['1', '12', '3', '1', '12', '3', '1', '12', '3'],
  51. data: [],
  52. searchData: [],
  53. limit: 5,
  54. total: null,
  55. page: 1,
  56. pageshow: true
  57. }
  58. },
  59. activated () { //初始搜索内容(由其他页面搜索内容跳转至搜索页/更新搜索内容)
  60. this.pageList()
  61. console.log('actived!')
  62. },
  63. methods: {
  64. handleJump (input) {
  65. this.$router.push(
  66. {path: '/search', query: {input: input}},
  67. onComplete => {},
  68. onAbort => {})
  69. },
  70. pageList () {
  71. // 发请求拿到数据并暂存全部数据(此处未接接口)
  72. this.data = ['1', '12', '3', '1', '12', '3', '1', '12', '3']
  73. this.page = 1
  74. this.pageshow = false
  75. this.getList()
  76. this.$nextTick(() => {
  77. this.pageshow = true
  78. })
  79. console.log('data')
  80. console.log(this.data)
  81. },
  82. getList () {
  83. // es6过滤得到满足搜索条件的展示数据list
  84. let searchData = this.data.filter((item, index) =>
  85. item.includes(this.keyword)
  86. )
  87. console.log('keyword')
  88. console.log(this.keyword)
  89. // 过滤分页
  90. this.searchData = searchData.filter((item, index) =>
  91. index < this.page * this.limit && index >= this.limit * (this.page - 1)
  92. )
  93. // 分页的总数据
  94. this.total = searchData.length
  95. console.log('searchData')
  96. console.log(this.searchData)
  97. },
  98. handleSizeChange (val) {
  99. console.log(`每页 ${val} 条`)
  100. this.limit = val
  101. this.getList()
  102. },
  103. handleCurrentChange (val) {
  104. console.log(`当前页: ${val}`)
  105. this.page = val
  106. this.getList()
  107. },
  108. getHeight () {
  109. var heightCss1 = window.getComputedStyle(this.$refs.ullist).height // 100px
  110. var heightCss2 = window.getComputedStyle(this.$refs.pTitle).height // 100px
  111. var heightCss3 = window.getComputedStyle(this.$refs.pageHeight).height // 100px
  112. this.heightCss = heightCss1 + heightCss2 + heightCss3 + heightCss3
  113. console.log(this.heightCss)
  114. }
  115. },
  116. inject: ['reload'],
  117. watch: {
  118. '$route' (to, from) {
  119. this.input = to.query.input
  120. this.reload()
  121. }
  122. }
  123. }
  124. </script>
  125. <style scoped>
  126. .searchContentImg {
  127. width: 100%;
  128. height: 20%;
  129. top: 10%;
  130. left: 0;
  131. position: absolute;
  132. margin: 0 0 0 0;
  133. z-index: -1;
  134. }
  135. .secSearch {
  136. width: 50%;
  137. height: 600px;
  138. top: 15%;
  139. left: 10%;
  140. position: absolute;
  141. }
  142. .secSearch p {
  143. font-size: 30px;
  144. font-weight: 700;
  145. float: left;
  146. }
  147. .searchBox {
  148. width: 80%;
  149. top: 2%;
  150. position: relative;
  151. z-index: 10;
  152. }
  153. .searchBox i {
  154. float: right;
  155. left: -10px;
  156. position: relative;
  157. margin-top: -30px;
  158. color: #b5b5b5;
  159. z-index: 11;
  160. font-size: 18px;
  161. cursor: pointer;
  162. }
  163. #scimg {
  164. width: 100%;
  165. height: 100%;
  166. }
  167. .search-list {
  168. width: 100%;
  169. padding-bottom: 1.5%;
  170. top: 30%;
  171. position: absolute;
  172. border: 1px solid #9c9c9c;
  173. }
  174. .search-board {
  175. width: 90%;
  176. margin-left: 5%;
  177. margin-top: 8%;
  178. border-top: 1px solid #9c9c9c;
  179. }
  180. .search-board ul {
  181. margin-top: 1%;
  182. }
  183. .search-board ul li {
  184. list-style-type: none;
  185. text-align: left;
  186. line-height: 30px;
  187. }
  188. .searchImg {
  189. width: 20%;
  190. height: 100%;
  191. top: 40%;
  192. left: 65%;
  193. position: absolute;
  194. float: right;
  195. }
  196. #simg {
  197. width: 100%;
  198. float: left;
  199. }
  200. .page {
  201. top: 5%;
  202. right: 0;
  203. position: relative;
  204. }
  205. .empty-img {
  206. width: 100%;
  207. margin-top: 10%;
  208. left: 1%;
  209. position: relative;
  210. }
  211. .search-empty {
  212. width: 100%;
  213. height: 110%;
  214. top: 30%;
  215. position: absolute;
  216. border: 1px solid #9c9c9c;
  217. }
  218. .search-empty ul li {
  219. list-style-type: none;
  220. }
  221. .total-result {
  222. display: block;
  223. height: 24px;
  224. line-height: 24px;
  225. font-size: 24px;
  226. color: #333;
  227. width: 90%;
  228. text-align: left;
  229. }
  230. .search-list p{
  231. margin-top: 3%;
  232. margin-left: 5%;
  233. }
  234. </style>

代码可能有冗余未删除,若代码有误或可以改进,欢迎指正!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/636511
推荐阅读
相关标签