Vuejs autocomplete search with typeahead js

Today, We want to share with you Vuejs autocomplete search with typeahead.js.In this post we will show you Vue AutoComplete TextBox Component, hear for Popular Vue autocomplete Component we will give you demo and example for implement.In this post, we will learn about Building an Autocomplete Component with Vue.js with an example.

Vuejs autocomplete search with typeahead.js

There are the Following The simple About Vuejs autocomplete search with typeahead.js Full Information With Example and source code.

As I will cover this Post with live Working example to develop Autocomplete Field using Vue.js, so the bootstrap-vue autocomplete for this example is following below.

HTML Part

<div id="app">
  <h3>form auto complete</h3>
  <div class="d-flex">
    <div class="d-flex-lang">
      <h4>Single Select</h4>
      <form-autocomplete
        v-model="programmingLangA"
        :options="countries"
        placeholder="please select programmingLang"
      ></form-autocomplete>
      <p>
        Selected programmingLang : <br/>
        lid: {{programmingLangA.lid}}  <br/>
        title: {{programmingLangA.title}}
      </p>
    </div>
    <div class="d-flex-lang">
      <h4>Multi Select</h4>
      <form-autocomplete
        is-multiple
        v-model="multiLanguageA"
        :options="countries"
        placeholder="please select programmingLang"
      ></form-autocomplete>
      <div>
        Selected programmingLang : <br/>
        <p v-for="(v, k) in multiLanguageA" :key="k">
          lid: {{v.lid}} - title: {{v.title}}
        </p>
      </div>
    </div>
    <!-- With Scope Slot -->
    <div class="d-flex-lang">
      <h4>Single Select - with Scope Slot</h4>
      <form-autocomplete
        v-model="programmingLangB"
        :options="countries"
        placeholder="please select programmingLang"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
      </form-autocomplete>
      <p>
        Selected programmingLang : <br/>
        lid: {{programmingLangB.lid}}  <br/>
        title: {{programmingLangB.title}}
      </p>
    </div>
    <div class="d-flex-lang">
      <h4>Multi Select - with Scope Slot</h4>
      <form-autocomplete
        is-multiple
        v-model="multiLanguageB"
        :options="countries"
        placeholder="please select programmingLang"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
      </form-autocomplete>
      <div>
        Selected programmingLang : <br/>
        <p v-for="(v, k) in multiLanguageB" :key="k">
          lid: {{v.lid}} - title: {{v.title}}
        </p>
      </div>
    </div>
    <!-- Default value -->
    <div class="d-flex-lang">
      <h4>Single Select - Default value</h4>
      <form-autocomplete
        v-model="programmingLangC"
        :options="countries"
        placeholder="please select programmingLang"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
      </form-autocomplete>
      <p>
        Selected programmingLang : <br/>
        lid: {{programmingLangC.lid}}  <br/>
        title: {{programmingLangC.title}}
      </p>
    </div>
    <div class="d-flex-lang">
      <h4>Multi Select - Default Value</h4>
      <form-autocomplete
        is-multiple
        v-model="multiLanguageC"
        :options="countries"
        placeholder="please select programmingLang"
      >
        <template slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
        <template slot="selected" slot-scope="row"><span :class="`flag-icon flag-icon-${toLowerCase(row.lang.lid)}`"></span> {{row.lang.title}}</template>
      </form-autocomplete>
      <div>
        Selected programmingLang : <br/>
        <p v-for="(v, k) in multiLanguageC" :key="k">
          lid: {{v.lid}} - title: {{v.title}}
        </p>
      </div>
    </div>
  </div>
</div>

CSS Part

.d-flex {
  display: flex;
  flex-wrap: wrap;
}
.d-flex-lang {
  width: 300px;
  padding: 10px;
  border: 1px solid #eaeaea;
  margin: 0 20px 20px 0;
}
.form-autocomplete {
  padding: 10px;
  background: #eaeaea;
}

ul {
  width: 200px;
  padding-left: 0;
}
ul > li {
  cursor: pointer;
  list-style: none;
  padding: 5px;
  margin-bottom: 5px;
  border: 1px solid white;
  background: #dedede;
}
ul > li:hover {
  background: #313131;
  color: white;
}
ul > li.disabled,
ul > li.disabled:hover {
  background: #eaeaea;
  color: #a7a7a7;
  cursor: inherit;
}
ul > li.disabled .flag-icon {
  opacity: 0.5;
}

.langs-selected {
  margin-top: 20px;
}
.lang-selected {
  border-radius: 5px;
  display: inline-block;
  padding: 5px;
  background: #7b8fdc;
  color: white;
  margin: 0 5px 10px 0;
}

Js Part

const countries = [
  { lid: 'AU', title: 'Laravel' },
  { lid: 'ID', title: 'Angularjs' },
  { lid: 'KR', title: 'PHP' },
  { lid: 'NZ', title: 'Magento' },
];

Vue.component('form-autocomplete', {
  template: 
    `<div class="form-autocomplete">
      <div v-if="!isMultiple">
        <input v-if="!value.lid" v-model="textSearch" type="text" :placeholder="placeholder" @focus="showOptions"/>
        <div class="langs-selected">
          <div class="lang-selected" v-if="value.lid">
            <slot name="selected" :lang="value">{{value.lid}} - {{value.title}}</slot> <button @click="clearItem">remove</button>
          </div>
        </div>
        <ul class="options" v-show="isShow">
          <li v-for="lang in filteredOptions" @click="selectItem(lang)">
            <slot :lang="lang">{{lang.title}}</slot>
          </li>
          <li v-if="filteredOptions.length===0">Item not found</li>
        </ul>
      </div>
      <div v-if="isMultiple">
        <input v-model="textSearch" type="text" :placeholder="placeholder" @focus="showOptions"/>
        <div class="langs-selected">
          <div class="lang-selected" v-for="(v, k) in value">
            <slot name="selected" :lang="v">{{v.lid}} - {{v.title}}</slot> <button @click="removeItem(k)">remove</button>
          </div>
        </div>
        <ul class="options" v-show="isShow">
          <li v-for="lang in filteredOptions" :class="{'disabled': inSelectedItems(lang.lid)}" @click="addItem(lang)">
            <slot :lang="lang">{{lang.title}}</slot>
          </li>
          <li v-if="filteredOptions.length===0">Item not found</li>
        </ul>
      </div>
    </div>`
  ,
  props: {
    isMultiple: {
      type: Boolean,
      default: false,
    },
    placeholder: {
      type: String,
      default: '',
    },
    options: {
      type: Array,
      default() {
        return [];
      },
    },
    value: {
      type: [Object, Array],
      default() {
        return {
          lid: undefined,
          title: undefined,
        };
      },
    },
  },
  data() {
    return {
      textSearch: '',
      isShow: false,
    };
  },
  computed: {
    filteredOptions() {
      return this.options.filter(val => val.title.toLowerCase().includes(this.textSearch.toLowerCase()));
    },
    selectedItems() {
      if (this.isMultiple) {
        return this.value.map(v => v.lid);
      }
      return [];
    },
  },
  methods: {
    inSelectedItems(lid) {
      return this.selectedItems.includes(lid);
    },
    showOptions() {
      this.isShow = true;
    },
    selectItem(lang) {
      this.textSearch = '';
      this.isShow = false;
      // this.$emit('update:lang', lang);
      this.$emit('input', lang);
      this.$emit('onSelectItem', lang);
    },
    clearItem() {
      this.textSearch = '';
      // this.$emit('update:lang', {
      //   id: undefined,
      //   title: undefined,
      // });
      this.$emit('input', {
        id: undefined,
        title: undefined,
      });
      this.$emit('onClearItem');
    },
    addItem(lang) {
      if (!this.inSelectedItems(lang.lid)) {
        this.textSearch = '';
        this.isShow = false;
        const langs = JSON.parse(JSON.stringify(this.value));
        langs.push(lang);
        // this.$emit('update:lang', langs);
        this.$emit('input', langs);
        this.$emit('onAddItem', langs);
      }
    },
    removeItem(index) {
      this.textSearch = '';
      const langs = JSON.parse(JSON.stringify(this.value));
      langs.splice(index, 1);
      // this.$emit('update:lang', langs);
      this.$emit('input', langs);
      this.$emit('onRemoveItem', langs);
    },
  },
});
new Vue({
  el: '#app',
  data() {
    return {
      programmingLangA: {
        lid: undefined,
        title: undefined,
      },
      programmingLangB: {
        lid: undefined,
        title: undefined,
      },
      programmingLangC: {
        lid: 'ID',
        title: 'Angularjs',
      },
      multiLanguageA: [],
      multiLanguageB: [],
      multiLanguageC: [
        { lid: 'ID', title: 'Angularjs' },
        { lid: 'AU', title: 'Laravel' },
      ],
    };
  },
  methods: {
    toLowerCase(text) {
      return text.toLowerCase();
    },
  },
});

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Preview Image before Upload using vuejs

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Vuejs autocomplete search with typeahead.js.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.