top-search.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <el-autocomplete class="top-search"
  3. popper-class="my-autocomplete"
  4. v-model="value"
  5. :fetch-suggestions="querySearch"
  6. :placeholder="$t('search')"
  7. @select="handleSelect">
  8. <template slot-scope="{ item }">
  9. <i :class="[item[iconKey],'icon']"></i>
  10. <div class="name">{{ item[labelKey] }}</div>
  11. <div class="addr">{{ item[pathKey] }}</div>
  12. </template>
  13. </el-autocomplete>
  14. </template>
  15. <script>
  16. import config from "../sidebar/config.js";
  17. import { mapGetters } from "vuex";
  18. export default {
  19. data() {
  20. return {
  21. config: config,
  22. value: "",
  23. menuList: []
  24. };
  25. },
  26. created() {
  27. this.getMenuList();
  28. },
  29. watch: {
  30. menu() {
  31. this.getMenuList();
  32. }
  33. },
  34. computed: {
  35. labelKey() {
  36. return this.website.menu.props.label || this.config.propsDefault.label;
  37. },
  38. pathKey() {
  39. return this.website.menu.props.path || this.config.propsDefault.path;
  40. },
  41. iconKey() {
  42. return this.website.menu.props.icon || this.config.propsDefault.icon;
  43. },
  44. childrenKey() {
  45. return (
  46. this.website.menu.props.children || this.config.propsDefault.children
  47. );
  48. },
  49. ...mapGetters(["menu", "website"])
  50. },
  51. methods: {
  52. getMenuList() {
  53. const findMenu = list => {
  54. for (let i = 0; i < list.length; i++) {
  55. const ele = Object.assign({}, list[i]);
  56. if (ele[this.childrenKey]) findMenu(ele[this.childrenKey]);
  57. delete ele[this.childrenKey];
  58. this.menuList.push(ele);
  59. }
  60. };
  61. this.menuList = [];
  62. findMenu(this.menu);
  63. },
  64. querySearch(queryString, cb) {
  65. var restaurants = this.menuList;
  66. var results = queryString
  67. ? restaurants.filter(this.createFilter(queryString))
  68. : restaurants;
  69. // 调用 callback 返回建议列表的数据
  70. cb(results);
  71. },
  72. createFilter(queryString) {
  73. return restaurant => {
  74. return (
  75. restaurant.label.toLowerCase().indexOf(queryString.toLowerCase()) ===
  76. 0
  77. );
  78. };
  79. },
  80. handleSelect(item) {
  81. this.value = "";
  82. this.$router.push({
  83. path: this.$router.$avueRouter.getPath({
  84. name: item[this.labelKey],
  85. src: item[this.pathKey],
  86. i18n: item.meta.i18n
  87. }),
  88. query: item.query
  89. });
  90. }
  91. }
  92. };
  93. </script>
  94. <style lang="scss">
  95. .my-autocomplete {
  96. li {
  97. line-height: normal;
  98. padding: 7px;
  99. .icon {
  100. margin-right: 5px;
  101. display: inline-block;
  102. vertical-align: middle;
  103. }
  104. .name {
  105. display: inline-block;
  106. text-overflow: ellipsis;
  107. overflow: hidden;
  108. vertical-align: middle;
  109. }
  110. .addr {
  111. padding-top: 5px;
  112. width: 100%;
  113. font-size: 12px;
  114. color: #b4b4b4;
  115. }
  116. .highlighted .addr {
  117. color: #ddd;
  118. }
  119. }
  120. }
  121. </style>