search.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div class="search-box">
  3. <input
  4. @focus="onfocus"
  5. @input="search"
  6. @keyup="choseList"
  7. v-model="searchVal"
  8. class="search"
  9. type="text"
  10. placeholder="在NutUI中搜索"
  11. >
  12. <!-- <transition name="fade"> -->
  13. <ul class="search-list" v-if="searchList.length>0">
  14. <li :class="searchCurName==item.name?'cur':''" @click="checklist(item)" v-for="(item,index) in searchList" :key="index">
  15. <router-link :to="{name:item.name}">
  16. {{item.name}}
  17. <span>{{item.chnName}}</span>
  18. </router-link>
  19. </li>
  20. </ul>
  21. <!-- </transition> -->
  22. </div>
  23. </template>
  24. <script>
  25. import Conf from "../../src/config.json";
  26. export default {
  27. data(){
  28. return {
  29. packages : Conf.packages,
  30. searchVal: "",
  31. searchList: [],
  32. searchCurName:'',
  33. searchIndex:0,
  34. }
  35. },
  36. mounted() {
  37. let list = document.querySelector('.search-list');
  38. let that = this;
  39. window.addEventListener('click',that.closelist)
  40. },
  41. methods:{
  42. closelist(){
  43. this.searchList = [];
  44. this.searchCurName='';
  45. this.searchIndex=0;
  46. },
  47. onfocus(e) {
  48. e.target.select();
  49. },
  50. clearSearch() {
  51. this.searchList = [];
  52. },
  53. search(e) {
  54. let val = e.target.value.toLowerCase();
  55. this.searchIndex=0;
  56. if (val) {
  57. let packages = this.packages;
  58. let list = [];
  59. for (let i = 0, item; (item = packages[i]); i++) {
  60. let cn = item.chnName.toLowerCase();
  61. let en = item.name.toLowerCase();
  62. if (cn.indexOf(val) > -1 || en.indexOf(val) > -1) {
  63. list.push(item);
  64. }
  65. }
  66. this.searchList = list;
  67. }else{
  68. this.checklist()
  69. }
  70. },
  71. checklist() {
  72. this.searchVal = "";
  73. this.searchList = [];
  74. this.searchCurName='';
  75. this.searchIndex=0;
  76. },
  77. choseList(e){
  78. let searchIndex = this.searchIndex;
  79. if(e.keyCode==40){
  80. searchIndex++;
  81. }
  82. if(e.keyCode==38){
  83. searchIndex--;
  84. }
  85. if(searchIndex<0){
  86. searchIndex = 0;
  87. }
  88. let searchList = this.searchList;
  89. if(searchList.length>0){
  90. let chnName = searchList[searchIndex].name;
  91. if(chnName){
  92. this.searchCurName = chnName;
  93. this.searchIndex = searchIndex;
  94. if(e.keyCode==13){
  95. this.$router.push({
  96. path:'/'+searchList[searchIndex].name
  97. })
  98. this.searchCurName='';
  99. this.searchIndex=0;
  100. this.searchList=[];
  101. this.searchVal='';
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. .search-box {
  111. height: 22px;
  112. min-width: 300px;
  113. position: relative;
  114. input {
  115. width: 100%;
  116. }
  117. }
  118. .search {
  119. height: 22px;
  120. font-size: 14px;
  121. color: #666;
  122. border: none;
  123. background: url(./asset/css/i/sreach.png) no-repeat left center;
  124. padding-left: 45px;
  125. &:focus {
  126. outline: none;
  127. }
  128. }
  129. .search-list {
  130. background: #fff;
  131. position: absolute;
  132. width: 300px;
  133. list-style: none;
  134. border: 1px solid #f2f2f2;
  135. z-index: 99999;
  136. top: 27px;
  137. padding: 0;
  138. li {
  139. height: 40px;
  140. line-height: 40px;
  141. font-size:12px;
  142. a {
  143. display: inline-block;
  144. box-sizing: border-box;
  145. width: 100%;
  146. padding-left: 40px;
  147. text-decoration: none;
  148. color: #666;
  149. }
  150. &:hover {
  151. background: #6096ff;
  152. color: #fff;
  153. a {
  154. color: #fff;
  155. }
  156. }
  157. }
  158. .cur{
  159. background: #6096ff;
  160. color: #fff;
  161. a {
  162. color: #fff;
  163. }
  164. }
  165. }
  166. </style>