6

How can I change the color and style of chips in v-select?

I have code like this:

<v-select
  v-model="value"
  :items="roles"
  :menu-props="{ top: true, offsetY: true }"
  small-chips
  label="Roles"
  multiple
  hint="Select the role"
  persistent-hint
>
</v-select>

enter image description here

How can I change chips to styled label and color blue?

Yom T.
  • 8,760
  • 2
  • 32
  • 49
Pillo
  • 347
  • 1
  • 4
  • 15
  • 1
    Did you actually check [the docs](https://vuetifyjs.com/en/components/chips/)? It should be pretty straightforward. – Yom T. May 21 '20 at 18:41
  • `chip` has `color` props: `color="success"` – Mohsen May 21 '20 at 18:43
  • Hello @YomS. The answer is yes, i know how can i change colors and styles to chips as a single component, but in this case chip is a part of v-select component, i read docs and i found some thing about slots but i am new on vuejs-vuetify and it is not clear to me – Pillo May 21 '20 at 18:49
  • 1
    @tao This `small-chips` directive doesn't appear to be Vuetify's though. Not sure what that does there. – Yom T. May 21 '20 at 19:00

4 Answers4

10

You probably want the selection slot.

<v-select
  v-model="value"
  :items="roles"
  :menu-props="{ top: true, offsetY: true }"
  small-chips
  label="Roles"
  multiple
  hint="Select the role"
  persistent-hint>
  <template #selection="{ item }">
    <v-chip color="blue">{{item.name}}</v-chip>
  </template>
</v-select>

Where item.name would depend on these individual items of roles.

Yom T.
  • 8,760
  • 2
  • 32
  • 49
  • 1
    This is closer to *"pretty straightforward"*. – tao May 21 '20 at 18:57
  • Because it *is* straighforward if you look carefully in the docs. :P There is a section that demonstrates just this, only with different customizations. – Yom T. May 21 '20 at 18:58
  • Would you agree that's the section you should have linked in your comment, though? :P. And let's agree to disagree here: by industry standards, Vuetify's docs are not so user friendly, nor straightforward. They can be quite cryptic at times and they're far from exhaustive. Whenever I develop with Vuetify I spend more time looking at their source code to see what options are available than on their website. It's simply faster. And I'm not that bad at reading docs, I might add... – tao May 21 '20 at 18:59
  • Well, you're right. I should have done that instead of linking to the `chips` component page. My bad. – Yom T. May 21 '20 at 19:03
  • I have done it this way and I was able to change the color and style, but the text disappears – Pillo May 21 '20 at 19:04
  • @Pillo Then your `roles` must be an array of string? In that case, simply do this `{{item}}`. – Yom T. May 21 '20 at 19:05
  • @Pillo I personally think Vuetify's docs are good, although no better than Quasar's -- so, maybe ;) – Yom T. May 21 '20 at 19:07
  • 1
    Thaks @YomS. with {{item}} works fine, thanks to all too for your time – Pillo May 21 '20 at 19:08
  • Thanks a lot, I can make it work, but not understand clearly why and how it works, because I'm a newbie Can you point out where on the docs said about that, so I can read more, thanks, – Thinh NV Aug 02 '20 at 15:55
  • 1
    @ThinhNV There are a few examples on the `selection` slot on [this same page (check out the "Playground" section for the first one)](https://vuetifyjs.com/en/components/selects/#playground). – Yom T. Aug 02 '20 at 16:46
3

If we use selection slot to customize the chip as we want like @YomS. show above, we cannot use chips and deletable-chips props to make that chip deletable.

For anyone also need to implement deletable-chips in selection slot, here my snippet:

<template>
    <v-select
          v-model="styleSelection" // Linking that v-select to one array
    
          clearable
          multiple
          :items="travelStyles"
          label="Travel Style"
        >
          <template #selection="{ item }">
            <v-chip
              
              color="primary"

              close
              @click:close="deleteChip(item, styleSelection)" // call a method deleteChip

              >{{ item }}</v-chip
            >
          </template>
        </v-select>
</template>

<script>
  export default {
    data: () => ({
      styleSelection: [],
      travelStyles: [
        'Discovery',
        'Family',
        'In-depth Cultural',
        'Historical',
        'Food & Culinary',
        'Adventure',
        'Beach',
        'Hiking & Trekking',
        'Bicycle',
        'Sightseeing',
        'Boat',
        'River Cruise',
        'Ocean Cruise',
      ],

    }),
    methods: {
      deleteChip(itemNeedToRemove, array) {
        for (let i = 0; i < array.length; i += 1) {
          if (array[parseInt(i, 10)] === itemNeedToRemove) {
            array.splice(i, 1);
          }
        }
      },
    },
  };
</script>

Beside Selects v-select, it also works with Autocomplete v-autocompletes, the snippet is exactly the same.

If you want to customize the chip color and make it deletable in V-autocompletes, you can take the look on the code below:

      <v-autocomplete
        v-model="citySelection"
        clearable
        multiple
        :items="city"
        label="Where do you want to go?"
        :search-input.sync="search"
        @change="search = ''"
      >
        <template #selection="{ item }">
          <v-chip
            close
            color="primary"
            @click:close="deleteChip(item, citySelection)"
            >{{ item }}</v-chip
          >
        </template>
      </v-autocomplete>
Thinh NV
  • 3,182
  • 3
  • 18
  • 17
2

I have been in your shoes and I think that, if you need deleteable chips, reimplementing the delete chip functionality just for the sake of changing a color is an overkill.

Since your goal is stylistic I would suggest that you use a simple scss solution:

<template>
 <v-select
   class="mySelect"
   v-model="value"
   :items="roles"
   :menu-props="{ top: true, offsetY: true }"
   small-chips
   label="Roles"
   multiple
   hint="Select the role"
   persistent-hint
   deletable-chips
 >
 </v-select>
</template>
<script>
export default {
 data() {
   return {
     roles: ['role1', 'role2'],
   }
 },
}
</script>
<style lang="scss" scoped>
 .mySelect::v-deep .v-chip {

   /* chip background color */
   background-color: var(--v-primary-base);
   
   /* chip text color */
   color: white;

   /* chip X color */
   button {
     color: white;
 }
}
</style>

Notice the .mySelect::v-deep selector that enables the rule to apply in the specific element, even in a scoped style block.

EDIT:

  • Included Andy's suggestion
  • Added explanatory comments in style
  • Removed unnecessary !important
  • Added dummy roles in data to make the answer copy/paste testable
George Marios
  • 144
  • 1
  • 11
  • 1
    This answer is great, and worked perfectly for me. I would add that, if you'd like to style the delete "x" as well, the selector is `.mySelect::v-deep .v-chip button`. – Andrew Jun 09 '21 at 18:41
  • 1
    That's handy, I updated the answer to include your suggestion, thank you. – George Marios Jun 10 '21 at 08:03
0

When working with the <v-select> component in Vue.js projects, it's wise to make use of the configuration options provided by vuetify.js. By doing so, you can set up the component once and avoid repeating the same configuration code throughout your project. This approach not only helps to reduce code duplication but also makes it easier to maintain and update your instances globally.

export default createVuetify({
defaults: {
VSelect:{
      VChip:{
        closeIcon:"fa-solid fa-xmark",
        class:".........",
        style:"..."
      }
    },
}
})