search.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div class="q-pa-md position-relative">
  3. <q-input v-model="query" color="white" placeholder="Search ..." />
  4. <q-list class="search-results">
  5. <q-item clickable v-for="result in results" :key="result.item.title" @click="goTo(result.item.url)">
  6. <q-item-section>
  7. <q-item-label>{{ result.item.title }}</q-item-label>
  8. </q-item-section>
  9. </q-item>
  10. </q-list>
  11. </div>
  12. </template>
  13. <style>
  14. .position-relative {
  15. position: relative;
  16. }
  17. .search-results {
  18. position: absolute;
  19. color: white;
  20. width: 100%;
  21. background: rgba(166, 222, 214, 0.519);
  22. z-index: 9999;
  23. max-height: 200px;
  24. overflow-y: auto;
  25. }
  26. </style>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. query: "",
  32. results: [],
  33. documents: [
  34. {
  35. title: "Introduction",
  36. content: "This is the introduction to our documentation...",
  37. url: "/docs/introduction",
  38. },
  39. {
  40. title: "Getting Started",
  41. content: "Here's how to get started with our project...",
  42. url: "/docs/getting_started",
  43. },
  44. // More documents...
  45. ],
  46. fuse: null,
  47. };
  48. },
  49. watch: {
  50. query() {
  51. this.search();
  52. },
  53. },
  54. created() {
  55. let options = {
  56. keys: ["title", "content"],
  57. };
  58. this.fuse = new Fuse(this.documents, options);
  59. },
  60. methods: {
  61. search() {
  62. this.results = this.fuse.search(this.query);
  63. console.log(this.results);
  64. },
  65. goTo(url) {
  66. this.$router.push(url);
  67. },
  68. },
  69. };
  70. </script>