代码之家  ›  专栏  ›  技术社区  ›  Vucko

Vue 2 select2标签自定义模板

  •  0
  • Vucko  · 技术社区  · 6 年前

    我知道我可以更改 option slot ,但我们能对标签槽做同样的操作吗?喜欢 选项 :

      <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=“城市” 当选择该选项时。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Sphinx    6 年前

    作为 Vue-select Github: L324 Vue-select Github: L539 <slot name="selected-option"> 将是一个解决方案。

    更新时间: Vue-select Github 你会看到有一个父插槽= 选定的选项容器

    如下演示:

    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>