【问题标题】:BootstrapVue form tags input v-modelBootstrapVue 表单标签输入 v-model
【发布时间】:2020-11-19 02:06:51
【问题描述】:

我有一个这样的b-form-tag

<b-form-group label="Search" label-for="search">
    <b-form-tags
        id="search"
        v-model="search"
        separator=","
        remove-on-delete
        tag-variant="primary"
        tag-pills
        placeholder="Search here..."
    ></b-form-tags>
</b-form-group>

data 部分:

data() {
    return {
        search: []
    }
}

search 变量中只会存储标签,我还需要访问输入的当前输入文本并将其绑定到data 中的一个变量。我知道必须使用inputAttrsinputHandlers 来完成,但我不知道怎么做?

【问题讨论】:

    标签: javascript vue.js bootstrap-4 vuejs2 bootstrap-vue


    【解决方案1】:

    您可以使用自定义输入。这将迫使您重新创建一些用于清除输入和添加标签的功能。这是一个演示,我在其中简化了文档的advanced example。它初始化标签值,使用 Enter 重新实现添加标签,并以编程方式显示设置 v-model

    new Vue({
      el: "#app",
      data() {
        return {
          newTag: 'starting text',
          value: []
        }
      },
      methods: {
        resetInputValue() {
          this.newTag = ''
        },
        setTag(text) {
          this.newTag = text;
        }
      }
    });
    <div id="app">
        <b-form-tags
          v-model="value"
          @input="resetInputValue()"
          tag-variant="success"
          class="mb-2 mt-2"
          placeholder="Enter a new tag value and click Add"
        >
          <template v-slot="{tags, inputId, placeholder, addTag, removeTag }">
            <b-input-group>
              <!-- Always bind the id to the input so that it can be focused when needed -->
              <b-form-input
                v-model="newTag"
                :id="inputId"
                :placeholder="placeholder"
                @keydown.enter="addTag(newTag)"            
              ></b-form-input>
              <b-input-group-append>
                <b-button @click="addTag(newTag)" variant="primary">Add</b-button>
              </b-input-group-append>
            </b-input-group>
            <div v-if="tags.length" class="d-inline-block" style="font-size: 1.5rem;">
              <b-form-tag
                v-for="tag in tags"
                @remove="removeTag(tag)"
                :key="tag"
                :title="tag"
                class="mr-1"
              >{{ tag }}</b-form-tag>
            </div>
            <b-form-text v-else>
              There are no tags specified. Add a new tag above.
            </b-form-text>
          </template>
        </b-form-tags>
        <div>Text from `v-model`: {{ newTag }}</div>
        <div><button @click="setTag('programatically')">Set v-model programatically</button></div>
    </div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
    <script src="https://unpkg.com/vue@2.6.12/dist/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.19.0/bootstrap-vue.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.19.0/bootstrap-vue.min.css" rel="stylesheet" />

    【讨论】:

    • 我真的不想乱用模板,所以当当前标签发生变化时,我最终使用tag-validator 作为一种事件! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2018-06-29
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多