Warm tip: This article is reproduced from stackoverflow.com, please click
bootstrap-4 html input vue.js dropdown

How can I make a dropdown like Github's collaborators menu where the input text is not exactly the s

发布于 2020-03-27 10:21:08

I have an HTML5 input field with a bootstrap dropdown menu. When selecting a dropdown item in a normal bootstrap dropdown menu, this action populates the input field with the same exact text as in that dropdown item. How can I make the action of selecting a dropdown item update the text in the input field to a string that is not exactly the same as the text in the dropdown item? For example, the github collaborators dropdown displays only the username in the input field, but displays the username and name of the users in the dropdown items.

For an example, go to any github repository you own or have forked. Select Settings and then select Collaborators.

Questioner
GNG
Viewed
82
Loïc Monard 2019-07-03 23:02

Imagine you have some data like that :

users: [
  {
    'name': 'Thomas Edison',
    'email' 'thomas@edison.com',
    'selected': false
  },
  {
    'name': 'William Thomson',
    'email': 'william@thomson.com'
    'selected': false
  }
]

Then you just have to loop through your data in your v-for element :

<div v-for="user in users" class="dropdown">
  <span class="user" @click="user.selected = true">{{ user.email }} - {{ user.name }}</span>
</div>

Then in your input field, you have to loop through your users where the selected property is set to true, and only display user.name, as an example.