【问题标题】:Vue 2 select2 custom template for labelVue 2 select2 自定义标签模板
【发布时间】:2019-01-29 17:37:30
【问题描述】:

我知道我可以更改option slot 的模板,但我们可以为标签槽做同样的事情吗?喜欢option

  <v-select inputId="originsId" :options="origins" label="city" placeholder="Search...">
    <template slot="option" slot-scope="origin">
      <div class="flex">
        <div class="col">
          <span>{{ origin.city }}</span>
        </div>
        <div class="col">
          <span>{{ origin.country }}</span>
        </div>
      </div>
    </template>
  </v-select>

是否有某种方式我可以在选择选项时样式标签?现在它只显示label="city" 值。我需要类似的东西:

  <v-select inputId="originsId" :options="origins" label="city" placeholder="Search...">
    <template slot="label" slot-scope="origin">
      <div class="flex">
        <div class="col">
          <span>Selected city: {{ origin.city }}</span>
        </div>
        <div class="col">
          <span>Selected country: {{ origin.country }}</span>
        </div>
      </div>
    </template>

    <template slot="option" slot-scope="origin">
      <div class="flex">
        <div class="col">
          <span>{{ origin.city }}</span>
        </div>
        <div class="col">
          <span>{{ origin.country }}</span>
        </div>
      </div>
    </template>
  </v-select>

基本上我需要一些自定义样式和附加信息,然后选择选项时label="city"

【问题讨论】:

    标签: vue.js vuejs2 jquery-select2


    【解决方案1】:

    作为Vue-select Github: L324Vue-select Github: L539,使用&lt;slot name="selected-option"&gt; 将是一种解决方案。

    更新:Vue-select Github 你会看到有一个父槽 = selected-option-container,但我发现它还没有部署到 dist .将来,您应该可以使用此插槽自定义整个容器和选定的选项。

    如下演示:

    Vue.component('v-select', VueSelect.VueSelect)
    
    new Vue({
      el: '#app',
      data: {
        options: [
          {
              title: 'Read the Docs',
              icon: 'fa-book',
              url: 'https://codeclimate.com/github/sagalbot/vue-select'
            },
            {
              title: 'View on GitHub',
              icon: 'fa-github',
              url: 'https://codeclimate.com/github/sagalbot/vue-select'
            },
            {
              title: 'View on NPM',
              icon: 'fa-database',
              url: 'https://codeclimate.com/github/sagalbot/vue-select'
            },
            {
              title: 'View Codepen Examples',
              icon: 'fa-pencil',
              url: 'https://codeclimate.com/github/sagalbot/vue-select'
            }
        ]
      }
    })
    body {
      font-family: "Source Sans Pro", "Helvetica Neue", Arial, sans-serif;
    }
    
    h1,.muted {
      color: #2c3e5099;
    }
    
    h1 {
      font-size: 26px;
      font-weight: 600;
      text-rendering: optimizelegibility;
      -moz-osx-font-smoothing: grayscale;
      -moz-text-size-adjust: none;
    }
    
    #app {
      max-width: 30em;
      margin: 1em auto;
    }
    
    #app .dropdown li {
      border-bottom: 1px solid rgba(112, 128, 144, 0.1)
    }
    
    #app .dropdown li:last-child {
      border-bottom: none;
    }
    
    #app .dropdown li a {
      padding: 10px 20px;
      display: flex;
      width: 100%;
      align-items: center;
      font-size: 1.25em;
    }
    
    #app .dropdown li a .fa {
      padding-right: 0.5em;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
    <script src="https://unpkg.com/vue-select@latest"></script>
    <div id="app">
      <h1>Vue Select - Custom Option Templating</h1>
      <v-select :options="options" label="title">
        <template slot="selected-option" slot-scope="option">
          <div class="flex">
            <div class="col">
              <span class="fa" :class="option.icon"></span>
              <span>Selected item: {{ option.title }}</span>
            </div>
          </div>
        </template>
        <template slot="option" slot-scope="option">
            <span class="fa" :class="option.icon"></span>
            {{ option.title }}
        </template>
      </v-select>
    </div>

    【讨论】:

    • 很好,正是我想要的。谢谢!
    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 2015-01-20
    • 2020-08-24
    • 2014-03-04
    • 1970-01-01
    • 2021-07-13
    • 2023-04-01
    • 2020-02-08
    相关资源
    最近更新 更多