search.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. console.log(e)
  79. let searchIndex = this.searchIndex;
  80. if(e.keyCode==40){
  81. searchIndex++;
  82. }
  83. if(e.keyCode==38){
  84. searchIndex--;
  85. }
  86. if(searchIndex<0){
  87. searchIndex = 0;
  88. }
  89. let searchList = this.searchList;
  90. if(searchList.length>0){
  91. console.log(searchList[searchIndex])
  92. let chnName = searchList[searchIndex].name;
  93. if(chnName){
  94. this.searchCurName = chnName;
  95. this.searchIndex = searchIndex;
  96. if(e.keyCode==13){
  97. this.$router.push({
  98. path:'/'+searchList[searchIndex].name
  99. })
  100. this.searchCurName='';
  101. this.searchIndex=0;
  102. this.searchList=[];
  103. this.searchVal='';
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .search-box {
  113. height: 22px;
  114. min-width: 300px;
  115. position: relative;
  116. input {
  117. width: 100%;
  118. }
  119. }
  120. .search {
  121. height: 22px;
  122. font-size: 14px;
  123. color: #666;
  124. border: none;
  125. background: url(./asset/css/i/sreach.png) no-repeat left center;
  126. padding-left: 45px;
  127. &:focus {
  128. outline: none;
  129. }
  130. }
  131. .search-list {
  132. background: #fff;
  133. position: absolute;
  134. width: 300px;
  135. list-style: none;
  136. border: 1px solid #f2f2f2;
  137. z-index: 99999;
  138. top: 27px;
  139. padding: 0;
  140. li {
  141. height: 40px;
  142. line-height: 40px;
  143. a {
  144. display: inline-block;
  145. box-sizing: border-box;
  146. width: 100%;
  147. padding-left: 40px;
  148. text-decoration: none;
  149. color: #666;
  150. }
  151. &:hover {
  152. background: #6096ff;
  153. color: #fff;
  154. a {
  155. color: #fff;
  156. }
  157. }
  158. }
  159. .cur{
  160. background: #6096ff;
  161. color: #fff;
  162. a {
  163. color: #fff;
  164. }
  165. }
  166. }
  167. </style>