a tutorial on linking component data to a prop value

Is there a way to connect the searchString value in my Vue component to the item value in the html template it uses? I need to pass this value to the method called in my Ajax request.

Vue:

Vue.component('user-container-component', {
    props: {
        prop: null
    },
    template: '#user-container-template',
    data: function () {
        return {
            open: false,
            searchString: ''
        }
    },
    methods: {
        toggle: function () {
            this.open = !this.open;
        },
        dbSearch_method: function () {
            var self = this;
            $.ajax({
                url: 'Home/LocalSearch',
                type: 'GET',
                data: {id: self.searchString},
                success: function (response) {
                    self.$emit('search-results-fetched', response);
                },
                error: function () {
                    alert('error');
                }
            });
        }
    }
})

html:

<ul class="no-bullets" v-show="open" v-for="item in prop">
    <li><button class="btn btn-link bold" v-on:click="dbSearch_method">{{item}}</button></li>
</ul>

Answer №1

If you are using vue, you have the ability to pass a parameter in the on-click events to your dbSearch_method like this:

<ul class="no-bullets" v-show="open" v-for="item in prop">
    <li><button class="btn btn-link bold" v-on:click="dbSearch_method(item)">{{item}}</button></li>
</ul>

In the javascript code, you can define the dbSearch_method function as follows:

dbSearch_method: function (item) {

This allows you to have access to the {{item}} object within your search function and interact with its properties.

To learn more about this feature, check out the documentation here. I hope this information is helpful!

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

Disparity in React app: Misalignment between debugger and console output

Throughout the years, I've encountered this issue in various ways, and I have finally been able to articulate it. Take a look at the code snippet below: import React, {Component} from "react"; import aFunction from "./Function"; export default class ...

Child Component Unable to Access Vue Props

My root element contains logged in user data, which I am passing as props to the user profile component. However, when I attempt to access User object properties in the child component, it throws an error and shows as undefined. In my app.js: Vue.compone ...

The Upstash Redis scan operation

Attempting to utilize the @upstash/redis node client library for Node.js (available at https://www.npmjs.com/package/@upstash/redis), I am facing challenges in executing the scan command, which should be supported based on the documentation. Specifically, ...

Encountering a CORS issue specifically on the client side of a Next.js application when interacting with an API gateway

I've been struggling with this issue for a week now and can't seem to fully describe it. I have a FastAPI server running as a Lambda connected to API Gateway. https://i.stack.imgur.com/S5Zx9.png Both FastAPI and API Gateway have CORS enabled, b ...

What is the process for setting up redux in _app.tsx?

Each time I compile my application, I encounter the following error message: /!\ You are using legacy implementation. Please update your code: use createWrapper() and wrapper.useWrappedStore(). Although my application functions correctly, I am unsure ...

What is the correct way to properly insert a display none attribute

I'm experiencing some alignment issues with the images in my slideshow. I used this example as a reference to create my slide: https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_dots When clicking on the next image, it seems to mo ...

Issues with the HTML required attribute not functioning properly are encountered within the form when it is

I am encountering an issue with my modal form. When I click the button that has onclick="regpatient()", the required field validation works, but in the console, it shows that the data was submitted via POST due to my onclick function. How can I resolve thi ...

An issue was encountered with initializing digital envelope routines on a Vue.Js Project, error code 03000086

I'm encountering an issue with my Vue Project when I try to execute "npm run serve/build" syntax in the terminal. My npm node version is currently v18.12.0 (with npm 8.19.2) The error message being displayed is: opensslErrorStack: [ 'error:03 ...

Error: The variable "Tankvalue" has not been declared

Is there a way to fix the error message I am receiving when trying to display response data in charts? The Tankvalue variable seems to be out of scope, resulting in an "undefined" error. The error message states that Tankvalue is not defined. I need to ...

Merge the capabilities of server-side exporting and client-side exporting within Highcharts for Vue

While working with Highcharts' export server to download charts as images, I encountered a challenge. I needed to implement the client-side (offline) exporting feature in a single chart due to the large number of data points. However, after enabling c ...

Adjust the size and orientation of an image according to the dimensions of the window and the image

As I delve into HTML and Javascript, I am facing a challenge with resizing an image based on the window size. The goal is for the image to occupy the entire window while maintaining its aspect ratio during resizing. Additionally, if the window size exceeds ...

issue with AJAX POST request not functioning correctly upon form submission

Having an issue with Ajax post while trying to submit a form event. I am working with a table that is generated by opentable.php. Here is the code for opentable.php: <?php session_start(); require("dbc.php"); $memberid = $_SESSION['memberid' ...

AngularJS Vimeo API Request Error: "401 Authorization Required"

I've been experimenting with making external API calls to Vimeo from my AngularJS code using $http.jsonp. However, I keep receiving a 401 Authorization required error even though I have included my authorization key in the header. I encountered a simi ...

Every day, I challenge myself to build my skills in react by completing various tasks. Currently, I am facing a particular task that has me stumped. Is there anyone out there who could offer

Objective:- Input: Ask user to enter a number On change: Calculate the square of the number entered by the user Display each calculation as a list in the Document Object Model (DOM) in real-time If Backspace is pressed: Delete the last calculated resul ...

Is there a way to efficiently display more than 10 data items at a time using the FlatList component in react-native?

Here is the data I am working with: singlePost?.Comments = [ 0: {id: 82, content: "Parent1", responseTo: null} 1: {id: 83, content: "Child1", responseTo: 82} 2: {id: 84, content: "Parent2", response ...

Pattern for Ajax callback in Javascript

I'm facing an issue with the code snippet below. var bar = { ajaxcall : function(){ var _obj = {}; $.ajax({ headers: { 'Content-Type': "application/json; charset=utf-8", 'da ...

How should I proceed if I encounter an npm error stating that cb() was never called?

Hey everyone. I keep encountering an issue with npm whenever I attempt to install packages. I am receiving the following error message: npm ERR! cb() never called! npm ERR! This is an error with npm itself. Please report this error at: npm ERR! <h ...

What is the best way to add multiple Vue components to my Vue page?

Is there a more efficient way to handle multiple imports without having to write them all out individually? import button1 from './components/button1' import button2 from './componnets/button2' import table1 from './componnets/tabl ...

Angular and Firefox are flagging the response from my OAuth call as incorrect, however, Fiddler is showing a different result

Currently, I am in the process of developing a Cordova application and require OAuth authentication with my Drupal backend. My main issue lies in obtaining a request token for this purpose. Despite receiving a 200 response indicating success, when inspecti ...

The CORS problem arises only in production when using NextJS/ReactJS with Vercel, where the request is being blocked due to the absence of the 'Access-Control-Allow-Origin' header

I've encountered a CORS error while trying to call an API endpoint from a function. Strangely, the error only occurs in production on Vercel; everything works fine on localhost. The CORS error message: Access to fetch at 'https://myurl.com/api/p ...