标签的Vue 2 select2自定义模板 [英] Vue 2 select2 custom template for label

查看:190
本文介绍了标签的Vue 2 select2自定义模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以更改 option插槽的模板,但是我们可以对标签槽做同样的事情吗?就像option:

I know I can change the template for the option slot, but can we do the same for the label slot? Like for 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"值.我需要类似的东西:

Is there some way I can style the label when the option is selected? Now it only shows the label="city" value. I need something like:

  <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"之外,我还需要一些自定义样式和其他信息.

Basically I need some custom styling and additional info other then label="city" when the option is selected.

推荐答案

将是一种解决方案.

As Vue-select Github: L324 and Vue-select Github: L539, uses <slot name="selected-option"> will be one solution.

已更新:来自

Updated: from Vue-select Github you will see there is one parent slot = selected-option-container, but I found it hasn't been deployed to the dist. In future, you should be able to use this slot to custom the whole container and the selected options.

就像下面的演示一样:

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>

这篇关于标签的Vue 2 select2自定义模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆