0

I have a search bar and an autocomplete component. What I'd like is to be able to style the way the autocomplete renders. At the moment, when I type letters, it displays every word that contains those letters and what I'd like is for it to only show the words which begin with the letters I'm typing. Also, is there a way not to highlight the letters, but to display them in bold ?

enter image description here

Here's what my code looks like

<template>
  <div class="inspire" style="background-color:red">
    <v-app-bar style="padding: 10px 0px;"
      color="rgba(33,33,33,255)" 
      elevation="0" 
      height="64px"
    > 
      <div style="width:100%" v-if="$route.name == 'Mapping'">
        <template>
          <v-autocomplete
            v-model="valuesActor"
            :items="actorArray"
            :search-input.sync="searchActor"
            filled
            @blur="toggleSearch"
            background-color="#313131"
            append-icon=""
            color="var(--textLightGrey)"
            :menu-props="{maxWidth: 1600}"
            placeholder="Search for actors"
            active
          >
          </v-autocomplete>
        </template>
      </div>  
    </v-app-bar>
  </div>
</template>
Quentin
  • 65
  • 9

1 Answers1

1

Bold the matching text:

add an item slot to the autocomplete that uses a method to render the item text the way you want it. You can use regex to get the partial text of an item that matches the query string and surround it with a <strong> tag.

<v-autocomplete>
 <template v-slot:item="data">
    <div v-html="boldHighlight(data.item)"></div>
  </template>
</v-autocomplete>
boldHighlight(text) {
  return text.replace(new RegExp(this.searchActor, 'gi'), match => {
    return '<strong>' + match + '</strong>';
  });
}

Filter only if start of string is a match

Use the filter prop with the autocomplete to make your own custom filter function. The function will run for every item in the autocomplete list and should return true or false based on the given query.

<v-autocomplete
  :filter="customFilter"
/>
customFilter(item, queryText) {
  const itemText = item.toLowerCase();
  const searchText = queryText.toLowerCase();

  return itemText.startsWith(searchText);
}

Focus input on page load

This is done using standard Vue functionality, not specifically Vuetify. Add a ref to the element, which will give you a programmatic way of accessing the DOM element in your code, and then in the mounted hook of your component call the focus function.

<v-autocomplete
  ref="auto"
/>
mounted() {
  this.$refs.auto.focus()
}

Here's a codepen putting it all together.

yoduh
  • 7,074
  • 2
  • 11
  • 21
  • Though do you know if it's possible to type in the placeholder as soon as the search bar opens ? And not having to click inside the placeholder ? (i don't know if that makes sense) – Quentin Feb 23 '23 at 15:51
  • I think I know what you mean. I've updated my answer to include how to focus the autocomplete's input box on page load (this assumes the autocomplete is not hidden on initial page load) – yoduh Feb 23 '23 at 16:28
  • You're the best !!! Thank you sooo much !! I don't know how you manage to come up with answers this fast but thank you've been super helpful !! – Quentin Feb 23 '23 at 16:56