0

I have a v-autocomplete component on my template and I would like display data via item-text. The first value ${item.name} is will always be rendered but the second value ${item.faculty.name} comes from a nested relationship and I want to make it optional( if exist then display and if it null then display nothing). How can achieve it.

        <v-autocomplete
              v-model="form.classroom"
              :items="classrooms"
              :item-text="item => `${item.name} - ${item.faculty.name}`"
              item-value="id"
              :label="$t('GENERAL.ALL.CLASS_ROOM')"
              hide-selected
              clearable
              prepend-icon="mdi-chair-school"
              :error-messages="serverValidation.getMessage('classroom')"
           ></v-autocomplete>

The first value is displaying without problem.

1
  • use a computed value, it makes it easier to deal with if you put that logic into your js code. Commented Jun 23, 2020 at 21:48

1 Answer 1

2

Try ternary operator condition?option1:option2 :

  :item-text="item => item.faculty?`${item.name} - ${item.faculty.name}`:item.name"

or

:item-text="item => `${item.name} ${item.faculty?' - '+item.faculty.name:''}`"
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a champ sir. Thanks a lot

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.