【问题标题】:Bootstrap-vue: Auto-select first hardcoded <option> in <b-form-select>Bootstrap-vue:在 <b-form-select> 中自动选择第一个硬编码的 <option>
【发布时间】:2019-02-28 15:53:14
【问题描述】:

我正在使用带有服务器端生成选项标签的 b-form-select:

        <b-form-select :state="errors.has('type') ? false : null"
                       v-model="type"
                       v-validate="'required'"
                       name="type"
                       plain>
            <option value="note" >Note</option>
            <option value="reminder" >Reminder</option>
        </b-form-select>

当没有为此字段设置数据时,我想自动选择列表中的第一个选项。

这可能吗?我还没有找到如何从我的 Vue 实例中访问组件的选项。

【问题讨论】:

    标签: bootstrap-vue


    【解决方案1】:

    您的v-model 应该具有第一个选项的值。

    例子

    <template>
      <div>
        <b-form-select v-model="selected" :options="options" />
    
        <div class="mt-3">Selected: <strong>{{ selected }}</strong></div>
      </div>
    </template>
    
    <script>
      export default {
        data() {
          return {
            selected: 'a',
            options: [
              { value: null, text: 'Please select an option' },
              { value: 'a', text: 'This is First option' },
              { value: 'b', text: 'Selected Option' },
              { value: { C: '3PO' }, text: 'This is an option with object value' },
              { value: 'd', text: 'This one is disabled', disabled: true }
            ]
          }
        }
      }
    </script>
    

    没有设置数据时可以触发this.selected=${firstOptionValue}。

    【讨论】:

    • 如果我们不知道第一个选项是什么。列表生成了吗?
    【解决方案2】:

    如果我们不知道第一个选项是什么。列表生成了吗?

    如果你有动态数据,这样的东西会起作用。

    <template>
      <div>
        <b-form-select v-model="selected" :options="options" />
        <div class="mt-3">Selected: <strong>{{ selected }}</strong></div>
      </div>
    </template>
    <script>
    export default {
      data() {
        return {
          selected: [],
          options: [],
        };
      },
      mounted: function() {
        this.getOptions();
      },
      methods: {
        getOptions() {
          //Your logic goes here for data fetch from API
          const options = res.data;
          this.options = res.data;
          this.selected = options[0].fieldName; // Assigns first index of Options to model
          return options;
        },
      },
    };
    </script>
    

    【讨论】:

      【解决方案3】:

      如果您的选项存储在动态加载的属性中:

      • 计算属性
      • 异步计算(使用 AsyncComputed 插件)
      • 通过道具,可能会改变

      然后可以@Watch属性设置第一个选项。 这样,选择第一项的行为与数据加载分离,您的代码更易于理解。

      使用 Typescript 和 @AsyncComputed 的示例

      export default class PersonComponent extends Vue {
        selectedPersonId: string = undefined;
      
        // ...
      
        // Example method that loads persons data from API
        @AsyncComputed()
        async persons(): Promise<Person[]> {
          return await apiClient.persons.getAll();
        }
      
        // Computed property that transforms api data to option list
        get personSelectOptions() {
           const persons = this.persons as Person[];
           return persons.map((person) => ({
             text: person.name,
             value: person.id
           }));
        }
      
        // Select the first person in the options list whenever the options change
        @Watch('personSelectOptions')
        automaticallySelectFirstPerson(persons: {value: string}[]) {
           this.selectedPersonId = persons[0].value;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        • 2021-06-04
        • 1970-01-01
        • 2020-08-21
        • 2020-04-28
        相关资源
        最近更新 更多