4

I am using vue-select component from Vue Select Library in my html form as shown below and I want to display three value in the label but don't know how to achieve that. i couldn't found any solution in the documentation.

I want to display three value in the label as shown below.

    <v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id" label="item_name+'::'+item_code+'::'+item_created_at" v-model="item.selected_item" @input="getSelectedItem"  style="width: 100%; height:56px;" />

HTML:


     <script src="{{ asset('assets/requiredjs/vue-select.js') }}"></script>
     <link href="{{ asset('assets/requiredcss/vue-select.css') }}" rel="stylesheet">


    <v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id" label="item_name" v-model="item.selected_item" @input="getSelectedItem"  style="width: 100%; height:56px;" />

JS:


    Vue.component('v-select', VueSelect.VueSelect);

    var app = new Vue({
        el: '#app',
        data: {

        formfieldsdata: {

                items: [],

            },

        item: {
                selected_item: 0,
            },

        }

        });

Ref to vue select library documentation: https://vue-select.org/guide/values.html#transforming-selections

1 Answer 1

8

Just use template literals, what allow embed expressions in JavaScript strings. And make the label binded :label

 <v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id" :label="`${item_name} ${item_code} ${item_created_at}" v-model="item.selected_item`" @input="getSelectedItem"  style="width: 100%; height:56px;" />

Update label can use only for one object property. But you can use scopes for options and selected values. Example on codepen

<v-select id="selected_item" name="selected_item" title="Select an item" :options="formfieldsdata.items" :reduce="item_name => item_name.id"  v-model="item.selected_item" @input="getSelectedItem"  style="width: 100%; height:56px;" >


   <template slot="option" slot-scope="option">
       <span :class="option.icon"></span>
       {{ option.item_name }} {{option.item_code}} {{option.created_at}}
   </template>
   <template slot="selected-option" slot-scope="option">
       <span :class="option.icon"></span>
       {{ option.item_name }} {{option.item_code}} {{option.created_at}}
   </template>
</v-select>

Update 2 Multi properties search vue-select

vue-component

 <div id="app">
  <h1>Vue Select - Multiple properties</h1>
  <v-select :options="options" label="item_data"
   v-model="selected">
  </v-select>
</div>

vue-code

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'
        }
    ]
  }
})
Sign up to request clarification or add additional context in comments.

6 Comments

I have tried this, it did not work. i got an empty list of options
Okey I figured out and updated, also upload the example to codepen
This is getting closer. I can now display three properties of the object But when i try to search an item, i get "no result found". also the onChange event only fires for my first selection, to select another item, i have to unselect the current selection before the onChange event would fire again. Pls i really want to be able to search items by their name and code. Thanks
This works. Thanks so much bro, you are indeed a life saver. i owe you a pizza. Thanks I used the this.options.map(function (x){ return x.item_data = x.item_name + ' ' + x.item_code + ' ' + x.created_at; }
I glad to help you. I look forward my pizza :) :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.