Friday, September 25, 2020

Vue Js/ Vue Autocomplete textbox , own control and use multi places, pass parameter

 

VUE/VUEJS AutoCompete textbox as separate componenet and pass parameters for it. It is own separate control that we can add in any project.

1.     Create a file: AutoComplete.vue and paste below code

<template>
  <div class="autocomplete">
    <input
      type="text"
      v-on:change="getValue"
      v-model="mdlAutoText"
      v-on:input="getSearchValues"
      @keydown.down="onArrowDown"
      @keydown.up="onArrowUp"
      @keydown.enter="onEnter"
    />
    <ul
      id="autocomplete-results"
      v-show="isOpen"
      class="autocomplete-results"
      v-bind:style="{ width: aWidth }"
    >
      <li class="loading" v-if="isLoading">Loading results...</li>
      <li
        v-else
        v-for="(resultiin results"
        v-bind:key="result.T"
        @click="setResult(result.T)"
        class="autocomplete-result"
        :class="{ 'is-active': i === arrowCounter }"
      >
        {{ result.T }}
      </li>
    </ul>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "AutoComplete",
  components: {},
  props: ["items""apiUrl""sKey""aWidth""charLen"],
  data: function () {
    return {
      mdlAutoText: this.items,

      isOpen: false,
      results: [],
      search: "",
      isLoading: false,
      arrowCounter: 0,
    };
  },
  methods: {
    getValue: function () {
      //if (this.isOpen == true) this.$refs.txtAutoComp.focus();
      if (this.results.length <= 1) {
        this.isOpen = false;
        this.$emit("resBasePart"this.mdlAutoText);
      }
    },
    getSearchValues: function () {
      if (this.mdlAutoText.length >= this.charLen * 1) {
        axios({
          method: "get",
          url: this.apiUrl + "?p1=" + this.mdlAutoText + "&p2=" + this.sKey,
        })
          .then((response=> {
            let res = JSON.parse(response.data);
            this.results = res.Table;
          })
          .catch(function (err) {
            console.log(err);
          });
        this.isOpen = true;
      } else this.isOpen = false;
    },
    setResult: function (p1) {
      this.mdlAutoText = p1;
      this.isOpen = false;
      this.$emit("resBasePart"this.mdlAutoText);
    },
    onArrowDown() {
      if (this.arrowCounter < this.results.length) {
        this.arrowCounter = this.arrowCounter + 1;
      }
    },
    onArrowUp() {
      if (this.arrowCounter > 0) {
        this.arrowCounter = this.arrowCounter - 1;
      }
    },
    onEnter() {
      this.mdlAutoText = this.results[this.arrowCounter].T;
      this.isOpen = false;
      this.arrowCounter = -1;
    },
  },

  watch: {
    items: function (p1) {
      //alert(p1 + " " + p2);
      this.mdlAutoText = p1;
    },
  },
};
</script>
<style>
.autocomplete {
  positionrelative;
}

.autocomplete-results {
  padding0;
  margin0;
  border1px solid #eeeeee;
  height120px;
  overflowauto;
  width100%;
  positionabsolute;
  backgroundwhite;
  z-index9999;
}

.autocomplete-result {
  list-stylenone;
  text-alignleft;
  padding0px 2px;
  cursorpointer;
}

.autocomplete-result.is-active,
.autocomplete-result:hover {
  background-color#4aae9b;
  colorwhite;
}
</style>

 

2.     On parent page add below code with parameter.

<AutoComplete :items="mdlBasePartv-on:resBasePart="getBasePartsKey="parts" apiUrl="http://localhost:5000/Part/GetAutoComp" aWidth="26%" charLen="4" />           

3.     Parent page get omitted value and update model

getBasePart: function (p1) {

      this.mdlBasePart = p1;

      this.partLoad("part_load");

    }

4.     Here we can update key press Enter, Up arrow and Down arrow


Thursday, September 3, 2020

Vue working experience

Deploy Vue App in sub dirrectory

 

1. Create a file  if not has Vue.config.js near package.json file in Main folder.

2. Add below code:

 

module.exports = {

    publicPath: '/aimsservice'

}

 Enable history mode

1.     Add below code in router file:

 

Vue.use(VueRouter)

 

const routes1 = [

  {

    path: '/',

    name: 'Home',

    component: AccessDenied

  },

  {

    path: '/materialcomment',

    name: 'MaterialComment',

    component: MaterialComment

  },

  {

    path: '/materialcommentlist',

    name: 'MaterialCommentList',

    component: MaterialCommentList

  }

]

 

const router = new VueRouter({

  routes: routes1

  ,mode:'history'

  ,base: process.env.BASE_URL

})

 

Enable history