Advertisement
Gustavo_Inzunza

dr

Oct 6th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <div class="ui dropdown selection component" :id="id" v-bind:class="classes">
  3.         <input type="hidden" v-model="selected" :id="`input_${id}`" :name="name">
  4.         <i class="dropdown icon"></i>          
  5.         <div class="default text">{{ defaultText }}</div>
  6.         <div class="menu">
  7.             <div class="item" v-for="(value, index) in data" :data-value="valorItem(value, index)">{{ nombreItem(value, index) }}</div>
  8.             <!--  @click="change(valorItem(value, index))"-->
  9.         </div>
  10.     </div>
  11.    
  12. </template>
  13.  
  14. <script>
  15.     import Tools from '../../tools.js';
  16.  
  17.     export default {
  18.         /* On load */
  19.         mounted(){
  20.             this.inicializarDropdown();
  21.         },
  22.  
  23.         data(){
  24.             return {
  25.                 id: Tools.id()
  26.             }
  27.         },
  28.  
  29.         /* Parametros recibidos */
  30.         props: {
  31.             data: {
  32.                 type: [Object, Array],
  33.                 required: true
  34.             },
  35.  
  36.             dataKeyValue: {
  37.                 type: Boolean,
  38.                 default: false
  39.             },
  40.             dataInverted: {
  41.                 type: Boolean,
  42.                 default: false
  43.             },
  44.             selected: {
  45.                 type: [Number, String]              
  46.             },
  47.             text: {
  48.                 type: String
  49.             },          
  50.             value: {
  51.                 type: String
  52.             },
  53.             classes: {
  54.                 type: String
  55.             },
  56.             defaultText: {
  57.                 type: String,
  58.                 default: 'Seleccione'
  59.             },
  60.             useVModel:{
  61.                 type: Boolean,
  62.                 default: false  
  63.             },
  64.             fullTextSearch:{
  65.                 type: [Boolean, String],
  66.                 default: false
  67.             },
  68.             name: {
  69.                 type: String,
  70.                 default: ''                
  71.             }
  72.         },
  73.                
  74.         /* Metodos */
  75.         methods:{
  76.             inicializarDropdown(){
  77.                 $(`#${this.id}`).dropdown({
  78.                     fullTextSearch: this.fullTextSearch,
  79.                     forceSelection: false,
  80.                     onChange: function(value){
  81.                         this.onChange(value);
  82.                     }.bind(this)
  83.                 });
  84.             },
  85.             onChange(value){
  86.                 if(this.useVModel){
  87.                     this.$emit('input', value);
  88.                 }else{
  89.                     this.$emit('change', value);
  90.                 }
  91.             },
  92.  
  93.             nombreItem(name, index){
  94.                 return (this.dataKeyValue) ?
  95.                     (this.dataInverted ? name : index) : name[this.text];
  96.             },
  97.  
  98.             valorItem(value, index){
  99.                 return (this.dataKeyValue) ?
  100.                     (this.dataInverted ? index : value) : value[this.value];
  101.             },
  102.  
  103.  
  104.  
  105.         },
  106.         watch: {
  107.             selected: function(value) {
  108.                 $(`#${this.id}`).dropdown('set selected', value);
  109.             },
  110.             data: function(value) {
  111.                 Vue.nextTick(() => {
  112.                     this.inicializarDropdown();
  113.                     $(`#${this.id}`).dropdown('set selected', this.selected);                  
  114.                 });
  115.  
  116.             }
  117.         }
  118.  
  119.     }
  120.  
  121. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement