display the text from the template after selecting it (including both the text-field and value-field)

I'm currently utilizing BootstrapVue.

In my b-form-select component, I display the name (as the text field) in the selection within my child.vue and emit the age (as the value field) to my parent.vue. This functionality is functioning as intended.

However, I am now looking to also display the name - the text-field - in my child.vue template. How can this be accomplished?

At present, I am using a watch method to detect changes when an item is selected, and emitting this value. Nevertheless, I also wish to validate my text-field and visually showcase it beneath my b-form-select component.

The template in child.vue:

<b-form-select v-model="selected_Person" :options="persons" text-field="name" value-field="age"></b-form-select>
<div> {{ Here I want to see the name of my Person }} </div>

The script in child.vue:

data() {
  return {
    persons: [
      {"name": "Hagrid", "age": "81"},
      {"name": "Harry", "age": "18"},
      {"name": "Ron", "age": "19"},
      {"name": "Snape", "age": "48"}
    ],
    selected_Person: null,
  }
},

watch: {
  selected_Person() {
    this.$emit('selected_Person', this.selected_Person) //This emits the age as the value
}
      

Answer №1

Technique-1: Exchanging values between text-field and value-field

<b-form-select v-model="selected_Person" :options="persons" text-field="age" value-field="name"></b-form-select>
<div> {{ selected_Person }} </div>

Technique-2:

Utilizing a computed property for the same purpose

<b-form-select v-model="selected_Person" :options="persons" text-field="name" value-field="age"></b-form-select>
<div> {{ getPersonName }} </div>

and

data() {
  return {
    persons: [
      {"name": "Hagrid", "age": "81"},
      {"name": "Harry", "age": "18"},
      {"name": "Ron", "age": "19"},
      {"name": "Snape", "age": "48"}
    ]
  }
},
computed: {
 getPersonName() {
  return this.persons.filter(person => person.age == this.selected_person)[0].name; // logic is applicable only when you set value-field to `age`
 // In case if there are multiple persons with the same age then use the below code
 // return this.persons.filter(person => person.age == this.selected_Person).map(per => per.name).join(',');
 }
},
watch: {
  selected_Person() {
    this.$emit('selected_Person', this.selected_Person) //Here I emit my age, because it's my value
}

Answer №2

Is there a way to bind v-model to a watcher in Vue.js? Watchers are typically used to monitor changes in specific values within your data() object. To achieve this, you can create a 'selected_Person' property in your data() and watch it for changes. Subsequently, you can display the selected value in the template using the following approach: -

data() {
  return {
    persons: [
      {"name": "Hagrid", "age": "81"},
      {"name": "Harry", "age": "18"},
      {"name": "Ron", "age": "19"},
      {"name": "Snape", "age": "48"}
    ],
    selected_Person: null
  }
},

watch: {
  selected_Person() {
    this.$emit('selected_Person', this.selected_Person) //Emitting the selected age value
}

Within the template: -

<b-form-select v-model="selected_Person" :options="persons" text-field="name" value-field="age"></b-form-select> 
<div> {{ selected_Person }} </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Redirect Form that Directs Users to a Specific Page Depending on Input Data

Apologies if this is a basic issue, but I'm struggling to figure it out. I am trying to create a form field on a webpage that will redirect users to a specific page based on the data they input. For example, if someone types in "dave" and submits the ...

Angular.js, require.js, and jQuery Plugin all combined to create a uniquely customized directive

As a newcomer to Angular and Require, I am seeking assistance with the following code snippet. Despite successfully integrating Angular and Require, I am facing an issue where the bootstrap-switch element fails to initialize: define(['myapp.ui/module ...

Magnify novice mistakes: Unhandled promise rejection and Ensure every child has a distinct "key" property

Currently, I am working through Amazon's Getting Started with AWS tutorial found here: https://aws.amazon.com/getting-started/hands-on/build-react-app-amplify-graphql/module-four/ After successfully building and hosting the app on git, I noticed that ...

Alert displayed on console during transition from MaterialUI lab

Whenever I try to run my MUI application, an error pops up in the console It seems you may have forgotten to include the ref parameter in your forwardRef render function. import { LoadingButton } from "@mui/lab"; const LoadData = ({loading,sig ...

Are there any find all functions available in JavaScript that are built-in?

I frequently work with arrays in JavaScript, and I am facing an issue with the function .find() as it only returns the first occurrence. I need a way to get all occurrences if there are multiple. Below is my code: const condition = [ { info_p ...

What could be the root cause behind this Selenium mistake?

My goal is to verify the correct scroll position in the browser using NightwatchJS and Selenium. Below is the command I am using in Nightwatch: assertScrollPosition(testValue) { this.api.execute(() => { const offsetValue = w ...

Retrieve checkbox selected value using pug and node.js

How can I retrieve the value of a checked checkbox in order to redirect to (href="/player/edit/:id") or (href="/player/delete/: id"), and obtain the player id for editing or deleting? I have omitted other code details such as app.js which includes the ser ...

What configuration changes do I need to make in my Rails application.rb file to enable CORS requests?

Utilizing Rails as a backend API to support a VueJS frontend, I aim to trigger a POST request from VueJS upon pressing a button on the website using axios. methods: { signup() { if (this.password === this.password_confirmation) { t ...

Is it possible to exclude specific URLs from CSRF protection in sails.js?

I am currently integrating Stripe with my sails.js server and need to disable CSRF for specific URLs in order to utilize Stripe's webhooks effectively. Is there a way to exempt certain URLs from CSRF POST requirements within sails.js? I have searched ...

What is the reason for TS expressing dissatisfaction about the use of a type instead of a type entry being provided

Below is a snippet of code for a Vue3 component that takes in an Array of Objects as a prop: <script lang="ts"> interface CorveesI { What: string, Who: string, Debit: number } export default { name: 'Corvees', props: { ...

reduce the size of the image as the browser width decreases

Struggling with a webpage layout that features an image on the left and content area on the right, both with fixed widths. When reducing browser width, the image should not shrink but be cropped from the left side instead. Any solutions for this issue? ...

Retrieve all populated fields on the second tier using Node.js and Mongoose

Do you have any insights to share on Node.js and mongoose? I've defined a mongoose schema and when I use findOne(), it returns a document with multiple elements under the "resource" key. Below is an example of how the document looks like: { "met ...

Instructions on uploading a PDF file from a Wordpress page and ensuring the file is stored in the wp-content upload directory folder

What is the process for uploading a PDF file on a WordPress page? <form action="" method="POST"> <input type="file" name="file-upload" id="file-upload" /> <?php $attachment_id = media_handle_upload('file-upload', $post->I ...

What is the best way to create floating text that appears when clicked, similar to the mechanics

https://i.stack.imgur.com/nRKG1.jpg Upon clicking the "big cookie" in my game, a popup appears displaying the number of cookies earned (like +276.341 septillion in this image). The popup slowly moves upward and then fades out. I successfully incorporated ...

Guide on extracting the text from the <script> tag using python

I'm attempting to extract the script element content from a generic website using Selenium. <script>...</script> . url = 'https://unminify.com/' browser.get(url) elements = browser.find_elements_by_xpath('/html/body/script[ ...

ERROR UnhandledTypeError: Unable to access attributes of null (attempting to retrieve 'pipe')

When I include "{ observe: 'response' }" in my request, why do I encounter an error (ERROR TypeError: Cannot read properties of undefined (reading 'pipe'))? This is to retrieve all headers. let answer = this.http.post<ResponseLog ...

Error encountered while mounting the 'vue-table-2' component in a Vue.js application: "Component mounting failed."

Just starting out with Vue.js and feeling a bit lost... I really want to give 'vue-table-2' a shot after seeing it in action here: (https://jsfiddle.net/matfish2/jfa5t4sm/) but I'm having trouble getting it to work properly. "dependencies" ...

Pressing a button triggers an Ajax response, but in CasperJS it results in the entire page being refreshed

Recently, I've been experimenting with CasperJS in an attempt to identify Free-Email Alias on My focus is on the input field labeled "E-Mail-Wunschname:" where I intend to input a name, click the "Prüfen" button, and then extract the suggested accou ...

Are third-party scripts and HTML widgets able to replicate your website's data, including cookies, HTML, and other elements?

Currently, I am in the process of constructing a website that incorporates a third-party weather HTML widget. The widget is sourced from a trusted and reliable source on the web. It consists of a link and small JavaScript tags that are executed once loaded ...

Display a text field upon clicking on a specific link

I am trying to create a text field that appears when a link is clicked, but I haven't been able to get it right yet. Here is what I have attempted: <span id="location_field_index"> <a href="javascript:void(0)" onclick="innerHTML=\"< ...