0

I've got basic select that I want bind to array. I use vuetify, but this problem is universal.

<v-select
v-bind:items="physicianListSpeciality"
>
</v-select>

now I want to use the same select to many arrays, depend on offer.person value that could be physician, nurse etc.

data: {
offer.person: "physician" //or offer.person: "nurse"
}

For example for physician I want to bind physicianListSpeciality For nurse I want to bind nurseListSpeciality, etc.

I tried to make something like:

<v-select
v-bind:items="`${offer.person}`ListSpeciality"
>
</v-select>

or

v-bind:items="[offer.person +'ListSpeciality']"

none of them seems to work for me. How should correct my code to make this work? Should I use some kind of computed or there is another way to do it?

2
  • v-bind:items="offer.person +'ListSpeciality'" Commented Apr 8, 2018 at 8:56
  • @samayo this gives [Vue warn]: Invalid prop: type check failed for prop "items". Expected Array, got String. Commented Apr 8, 2018 at 8:58

1 Answer 1

1

Well, it depends what scope you're. If you're working in the window scope, you would just call the var using window[offer.person + 'ListSpeciality'].

If we're in the component scope, and the getters / data 'nurseListSpeciality' exists then you could call it from the this context like so: this[offer.person + 'ListSpeciality'] where offer.person would be 'nurse'.

Another alternative would be to do something like that: Taking your example with your item's data

data() {
  return {
    jobs: {
      nurse: [
        'nurse item A',
        'nurse item B',
        'nurse item C',
      ],
      physician: [
        'physician item A',
        'physician item B',
        'physician item C',
      ],
    },
  };
}

Then you could just get the items like so:

// offer.person = 'nurse';
<v-select
  :items="jobs[offer.person]"
>
</v-select>
Sign up to request clarification or add additional context in comments.

3 Comments

I've doing spa. This should work, but I don't want to make changes of my array, beacuse they are arrays of objects and are used many times in my app already. My intention was to make simply change into :items prop
solution with this.[offer.person + 'ListSpeciality'] works for me! Thank you for help
@gileneusz Alright, then as in my edited answer, you could simply call this[offer.person + 'ListSpeciality']since just passing the string concatenation wont work. You need to call the variable to bind the array in the :items property.

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.