Tagged: bootstrap-vue autocomplete

Vuejs autocomplete search with typeahead js 0

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 :

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.

Autocomplete with Search box in vuejs Demo 0

Autocomplete with Search box in vuejs Demo

Autocomplete with Search box in vuejs Demo

In this Post We Will Explain About is Autocomplete with Search box in vuejs Demo With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Vuejs Typeahead Auto Complete json Example

In this post we will show you Best way to implement vuejs select search, hear for Vuejs autocomplete ajax example with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Autocomplete field with search box in Vue.js

In this Example,First of all Add or Include External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priority set.After that Include your relevant CSS Class.

Include External Libs

https://unpkg.com/[email protected]/dist/vue.js
https://unpkg.com/[email protected]/dist/css/select2.min.css
https://unpkg.com/[email protected]/dist/js/select2.js
https://unpkg.com/[email protected]/dist/jquery.js

index.html

<div id="el"></div>
<!-- Devloped by https://pakainfo.com free download examples -->
<script type="text/x-template" id="live-template">
  <div>
    <p>Selected: {{ selected }}</p>
    <select2 :options="options" v-model="selected">
      <option disabled value="0">Select one</option>
    </select2>
  </div>
</script>
<!-- Devloped by https://pakainfo.com free download examples -->
<script type="text/x-template" id="select2-template">
  <select>
    <slot></slot>
  </select>
</script>

index.js

Vue.component('select2', {
  props: ['options', 'value'],
  template: '#select2-template',
  mounted: function () {
    var vm = this
    $(this.$el)
      .val(this.value)
      //simple vuejs init select2
      .select2({ data: this.options })
      // emit event on change simple vuejs .
      .on('change', function () {
        vm.$emit('input', this.value)
      })
  },
  watch: {
    value: function (value) {
      // update value vuejs
      $(this.$el).select2('val', value)
    },
    options: function (options) {
      // update options using vue
      $(this.$el).select2({ data: options })
    }
  },
  destroyed: function () {
    $(this.$el).off().select2('destroy')
  }
})

var vm = new Vue({
  el: '#el',
  template: '#live-template',
  data: {
    selected: 0,
    options: [
      { id: 1, text: 'Good Morning' },
      { id: 2, text: 'Welcome' }
    ]
  }
})

style.css

html, body {
  font: 14px/19px sans-serif;
}
select {
  min-width: 400px;
}

vuejs autocomplete with search box

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is Create autocomplete search with Vuejs and PHP And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.