Topic: V-FOR doesn\\\'t work in MDBSELECT

I'm trying to do a v-for in my MDBSELECT but my list doesn't render, i've check the structure of my v-for and it is right. After this i make a default select and the V-FOR worked. Check the code below.  
<mdb-selectv-if="next==2"@getValue="getDevice">
<optiondisabledselected>Escolha seu dispostivo</option>
<optionv-for="deviceofdevices" :value="device.label"v-bind:key="device.label">{{device.label}}</option>
</mdb-select>
   

Mikołaj Smoleński staff answered 5 years ago


Hi there,

Vue Select was completely rebuild and now You can use loops but You have to pass options as a Object, not as a tag like it was before. Please read more here: https://mdbootstrap.com/docs/vue/forms/select/

Best regards


jaccordeiro free answered 5 years ago


Is this already solved? I still can't use v-for in the select.


Jakub Strebeyko staff answered 5 years ago


Dear @ruudboon@Pisarev, @shilu911  and @Soluti Alexandre Henrique de Souza Torres 

The update time is now, baby! MDB Vue 4.9.0 release is here, and the answer for the prayers in the thread with it - it brings a total <mdb-select> component makeover. Now it is fully dynamic, takes care of itself and its children, minds own business and has a rather stylish conduct!

note: mind the syntax change!

Cheers,
Kuba


ruudboon pro answered 5 years ago


Any updates regarding this issue? Can you give an estimate when to expect a fix? 


Jakub Strebeyko staff answered 5 years ago


Yes. The 4.8.2 brought an improvement in a downloaded package usability and other treats, including minor bug fixes and styling updates. For anyone interested in following newest advancements in the package, here is our changelog.


ruudboon pro answered 5 years ago


For everybody following this topic. 4.8.2 is still having this issue


Jakub Strebeyko staff answered 5 years ago


Yes, I will make sure of it.


ruudboon pro answered 5 years ago


@Jakub Strebeyko Will this thread be updated when the fix is in?

Jakub Strebeyko staff answered 5 years ago


The problem which this thread regards should be dealt with within a month tops. @Pisarev The problem you are facing has been addressed in a dedicated thread.

Pisarev pro answered 5 years ago


When a problem will be resolved? We can't use select!!!

Jakub Strebeyko staff answered 5 years ago


It probably has to do with the way select evaluates its children, doing it on initial render only, perhaps? There is a team on the way, should fix it soon. Thanks!

ruudboon pro answered 5 years ago


Updates are not being processed.
<mdb-select search @getValue="selectClient">
  <option disabled selected>Kies een client</option>
  <option v-for="client in clients" v-bind:key="client.client_id">{{client.firstname}} {{client.lastname}}</option>
</mdb-select>
<span v-for="client in clients" v-bind:key="client.client_id">{{client.firstname}} {{client.lastname}}</span>
In the example above I see the initial value of the clients array. When fetching new clients from our backend and storing it in clients the span is updated with the new clients the select still has it's initial value.

Jakub Strebeyko staff answered 5 years ago


Yes, it was. Say you have a data object with "options" array.
<mdb-select @getValue="getSelectValue">
  <option v-for="option in options" :key="option" :value="option">{{option}}</option>
</mdb-select>
As Slavoj Žižek once famously put it: "It just works" (on my end). Please describe the issue in detail if it persists. Best, Kuba

ruudboon pro answered 5 years ago


@jakub Was this fixed in the latest release? In the release notes of 4.8.1 there is a message "Added a possibility of using v-for loop in Select component" But still seeing the same issue here.

Jakub Strebeyko staff answered 5 years ago


We will definitely give it a spin this week. Thanks!

shilu911 pro answered 5 years ago


I had the same issue, so I made a work around, hope this issue can be fixed officially soon.
<template>
  <component :is="tag" :class="wrapperClasses" :style="wrapperStyles" :data-multiple="this.multiple" v-on-clickaway="away">
    <span :class="caretClasses">▼</span>
    <mdb-select-input @click.native="toggle = !toggle" :text="inputText" :value="inputValue" :disabled="disabled" :valid="valid" :invalid="invalid" :errorMsg="errorMsg" successMsg="successMsg" />
    <transition @enter="enter" @after-enter="afterEnter" @before-leave="beforeLeave">
      <mdb-select-options v-if="toggle" :search="search" class="collapse-item">
        <mdb-select-option v-for="(option, index) in options" :active="isActive(option.value)" :disabled="!!option.disabled" :value="option.value" :key="index" :icon="option.icon" @click.native="onChange(option)">{{option.text}}</mdb-select-option>
        <mdb-btn v-if="multiple" save color="primary" size="sm" @click.native="toggle = !toggle">Save</mdb-btn>
      </mdb-select-options>
    </transition>
  </component>
</template>

<script>
  import classNames from 'classnames';
  import {
    mdbSelectInput,
    mdbSelectOption,
    mdbBtn,
    mdbSelectDropdown as mdbSelectOptions
  } from 'mdbvue';
  import { mixin as clickaway } from 'vue-clickaway';

  const Select = {
    name: 'Select',
    components: {
      mdbSelectInput,
      mdbSelectOptions,
      mdbSelectOption,
      mdbBtn
    },
    model: {
      prop: 'value',
      event: 'change',
    },
    props: {
      tag: {
        type: String,
        default: "div"
      },
      color: {
        type: String
      },
      multiple: {
        type: Boolean,
        default: false
      },
      disabled: {
        type: Boolean,
        default: false
      },
      search: {
        type: Boolean
      },
      wrapperClass: {
        type: String
      },
      wrapperStyle: {
        type: String
      },
      caretClass: {
        type: String
      },
      caretStyle: {
        type: String
      },
      getValue: {
        type: Function
      },
      value: {
        type: [String, Array],
      },
      options: {
        type: Array,
        default: [],
      },
      successMsg: {
        type: String
      },
      errorMsg: {
        type: String
      },
      valid: {
        type: Boolean
      },
      invalid: {
        type: Boolean
      },
    },
    data() {
      return {
        toggle: false,
        // inputValue: '',
        // inputText: 'Choose Your option',
        optionValues: [],
        optionTexts: [],
        disabledOptions: [],
        activeOptions: [],
        multiValues: [],
        icons: [],
      };
    },
    mixins: [clickaway],
    methods: {
      onChange(option) {
        if (this.multiple) {
          let values = [...this.value];
          values.push(option.value);
          this.$emit('change', values);
        } else {
          this.$emit('change', option.value);
        }
      },
      emitValue(value) {
        this.$emit('getValue', value);
      },
      away() {
        this.toggle = false;
      },
      enter(el) {
        el.style.opacity = 0;
      },
      afterEnter(el) {
        el.style.opacity = 1;
      },
      beforeLeave(el) {
        el.style.opacity = 0;
      },
      isActive(val) {
        if (this.multiple) {
          return this.value.indexOf(val) >= 0;
        } else {
          return val == this.value;
        }
      },
    },
    created() {
    },
    computed: {
      wrapperClasses() {
        return classNames(
          'select-wrapper md-form',
          this.color ? 'colorful-select dropdown-' + this.color : '',
          this.wrapperClass
        );
      },
      wrapperStyles() {
        return this.wrapperStyle;
      },
      caretClasses() {
        return classNames(
          'caret',
          this.disabled ? 'disabled' : '',
          this.caretClass
        );
      },
      caretStyles() {
        return this.caretStyle;
      },
      inputText() {
        let result = [];
        if (this.multiple) {
          if (!this.value || this.value.length == 0) {
            return result;
          }
          result = [];
          this.value.forEach(v => {
            let o = this.options.filter(option => option.value == v);
            if (o.length > 0) {
              result.push(o[0].text);
            }
          });
        } else {
          let o = this.options.filter(option => option.value == this.value);
          if (o.length > 0) {
            result.push(o[0].text);
          }
        }
        return result.join(', ');
      },
      inputValue() {
        let result = [];
        if (this.multiple) {
          if (!this.value || this.value.length == 0) {
            return result.join();
          }
          result = [];
          this.value.forEach(v => {
            let o = this.options.filter(option => option.value == v);
            if (o.length > 0) {
              result.push(o[0].value);
            }
          });
        } else {
          let o = this.options.filter(option => option.value == this.value);
          if (o.length > 0) {
            result.push(o[0].value);
          }
        }
        return result.join();
      }
    },
    watch: {
      activeOptions(index) {
        this.activeOptions[index] = this.activeOptions[index];
      },
    }
  };

  export default Select;
  export { Select as mdbSelect };
</script>

<style scoped>
  .collapse-item {
    position: absolute;
    top: 0;
    z-index: 9999;
    width: 100%;
    transition: opacity .2s;
  }
</style>
Usage example: <mdb-select color="primary" :options="roleOptions" v-model="role" :invalid="!!errors.role" :errorMsg="errors.role" :disabled="mode == 'profile'"/> Note: I haven't tested it very well, just clicked a couple of times, so don't be surprised if you find bugs.... Note again: please ignore the 'invalid' and 'errorMsg' props, I made changes in the selectInput.vue to make it is able to show error message below the input, so without the changes, these 2 props won't work, if you guys want the feature, I can post the changes here as well. Have fun!

ruudboon pro answered 5 years ago


As far as I understand you can only use this in a static way where you have predefined options. Then you create your options dynamically (like you do in almost every application) you can't use it right now. Hoping that this update will land soon.

Jakub Strebeyko staff answered 5 years ago


The issue is caused by the way select scrapes off attributes of dev-imposed <option> native elements to re-instate its own inner options. Passing in an <option v-for="..."> works, but its value attribute must not be bound, as otherwise it is not getting scraped (as from an attribute it turns into a prop) and hence the rendering problems. Should be fixed in upcoming weeks, no update otherwise.

ruudboon pro answered 5 years ago


Any updates regarding this issue. Select is not usable like this.

Jakub Strebeyko staff commented 5 years ago

Hi, could you elaborate on what you mean by "not usable"? Thanks

Jakub Strebeyko staff answered 5 years ago


Hi there, mdb-select component is grasping children's attributes on created life-cycle hook and it seems to cause problem with dynamically inserted content. I am filing it as a bug. Thanks for reaching out! Should be repaired soon. Best, Kuba

Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: MDB Vue
  • MDB Version: 4.6.0
  • Device: Desktop
  • Browser: Chrome
  • OS: Ubuntu
  • Provided sample code: No
  • Provided link: No