Having difficulty choosing an item from a personalized autocomplete search bar in my Vue.js/Vuetify.js project

NOTE: I have opted not to use v-autocomplete or v-combobox due to their limitations in meeting my specific requirements.

I'm facing difficulties while setting up an autocomplete search bar. The search functionality works perfectly except for one minor issue - selecting an item. When hovering over an item, a hand cursor appears indicating its clickability. Upon clicking the item, an @click event should trigger a method, but unfortunately, nothing happens.

Identifying the root cause of the problem is clear to me, however finding a solution has been challenging. The issue arises from the following parts:

<v-text-field //here's the input
    ...
    @focus="isFocused = true" @blur="isFocused = false"
    ...
></v-text-field>

<v-list v-show="queried_cards.length && isFocused"> //this represents the list
    <v-list-item @click="setCard(card)" v-for="(card, index) in queried_cards" :item="card" :key="index">
        ...
    </v-list-item> 
</v-list>

In summary, according to the above code structure, I aim to display the list only if the array queried_cards is not empty and if the v-text-field is focused on. Concealing the list when clicking outside the text field aligns with the intended outcome. However, this behavior conflicts with the item selection functionality, as clicking on an item is equivalent to clicking out of the text field (triggering @blur). Consequently, the setCard() method fails to execute since the v-list disappears before the @click="setCard(card)" event.

This issue has been lingering as I hoped to find a resolution eventually. Unfortunately, I am currently overwhelmed by other project tasks.

To better illustrate my problem, I have created a codepen: https://codepen.io/chataolauj/pen/RwwYxBg?editors=1010

Answer №1

there seems to be an issue with the way you have set up the list display using

<v-list v-if="queried_cards.length && isFocused" class="pa-0 overflow-y-auto" max-height="300" two-line>

When you click on a list item, the isFocused property is set to false due to the @blur="isFocused=false" binding on the input field, causing it to disappear. You need to find a solution to keep isFocused true when selecting items.

A quick solution would be:

Bind a click function on your v-container (to hide the result list when the user clicks outside of the input or the list):

  <v-container fluid @click="checkHide">
    checkHide(e){
   // if clicked on input or list items, keep the list visible
      if(e.target.tagName ==="INPUT" || e.target.classList.contains("v-list-item__content")){
        this.isFocused = true
      }else{
// clicked outside, hide it
        this.isFocused = false
      }
    }

Remove the @blur binding from your input element.

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

Incrementing and decrementing a variable within the scope in AngularJS

Objective: When the next/previous button is clicked, the label on the current slide should change to display the next/previous category. View the Category Slider Wireframe for reference. Context: I previously tried a solution called 'Obtain Next/Prev ...

Tips for Achieving Observable Synchronization

I've encountered a coding challenge that has led me to this code snippet: ngOnInit(): void { this.categories = this.categoryService.getCategories(); var example = this.categories.flatMap((categor) => categor.map((categories) = ...

Tips for showcasing styled text in Vue using API data

I'm having trouble formatting text in Vue. Within a component, I have a textarea that stores a string with backspaces, etc ... in an API like this: A cellar but not only...\n\nWelcome to the Nature & Wine cellar, a true Ali-baba's cave ...

What is the best way to obtain a reference to the parent form element within an AngularJS directive?

Here is the HTML code I am working with: <form name="my-form"> <div class="col-sm-4"> <input type="phone" name="mobileNumber" ng-model="form.mobileNumber" value="0439888999" required> </div> </form> In addition, I ha ...

Is it better to make the search query to Elasticsearch from the backend or the client application?

I'm currently working on a website with the frontend developed using vue.js (SPA) and backend developed with node.js. I am planning to integrate Elasticsearch into the search functionality on the main page. Should I make the search query directly from ...

Is there a way to apply filters to jquery datatables during initialization process?

Is there a way to server-side filter datatable during initialization? I attempted the following code: function tableFilter(arg1, param2, value3) { var searchTable = $("#tblsearch").dataTable({ "bRetrieve": true, "b ...

Develop a pair of jQuery plugins that are able to communicate with one another

My mind is currently in disarray. I am interested in developing two unique jQuery plugins: A tab plugin A slider plugin. These plugins need to be able to communicate with each other. For example, clicking on a tab should activate the corresponding index ...

The functionality of Environment Variables only applies during the build process and does not affect the execution of npm run serve

After running into a problem with my environment variables, I discovered that they work smoothly with npm run build, but not with vue-cli-service serve --mode sandbox To troubleshoot, I referred to the Vue documentation here https://cli.vuejs.org/guide/c ...

Failed Azure Pipeline due to the absence of the vueapp.key file during the npm run build process

I'm encountering issues while trying to build a Vue app in Azure pipelines. I lack experience with NodeJS and Vue, so I'm unsure about what it requires. My application builds and runs smoothly locally. Any guidance would be highly appreciated. I ...

Calculating the number of rows in a dynamic jQuery table

I have a table structured like this: <div class="row"> <input type="button" id="btnAddGatePass" value="Add Gate Pass Requester" /> <div class="table-responsive"> <table id="gatePass" class="table table-striped table-ho ...

When using AngularJS and PHP together, I encountered an error that stated "

My POST http request is encountering an error with the message Undefined property: stdClass::$number and Undefined property: stdClass::$message. Below are the codes I've been using: smsController.js angular .module('smsApp') .contr ...

"Sharing fields between mongoose models: How can I reference a field from one model in another

I am currently working on linking specific fields from the User model to the Card schema using the username as a reference point. Let me provide an example using my Card schema: const CardSchema = new mongoose.Schema({ text: { type: String, ...

Displaying genuine HTML content in a React application using Algolia Instantsearch

After setting up a demo app using React with an Algolia search feature, I uploaded some indices into Algolia. The content consists of raw HTML. Is there a way to display this content as real HTML using Algolia? ...

Tips for capturing audio in flac codec format using JS/AJAX

Is there a way to record audio in flac codec instead of opus codec? I attempted setting the codec to flac like this: let blob = new Blob(audioChunks,{type: 'audio/ogg; codecs=flac' }); I also tried this: var options = { audioBitsPerSecond : ...

Enhance React scrollbar functionality without relying on third-party dependencies

Currently working on enhancing the appearance of the scrollbar within my ReactJS project. Specifically, I am aiming for a design similar to this example: https://i.stack.imgur.com/2Q0P4.png Experimented with CSS properties like -webkit-scrollbar, -webki ...

What are some ways to incorporate JQuery with getElementById?

i am trying to retrieve all input elements within a form using JavaScript formEditable = document.getElementById("formid").getElementsByTagName("input"); However, I would like to achieve the same result using jQuery instead formEditable = $("#formid").f ...

Guide to updating information in Firestore using Node.js, embedded in a map array

Encountered an issue with the following error message: Error: Unable to access 'set' property of undefined. I am attempting to update the endTime field in my script for each user, calculate total hours worked, and then update the 'totalTi ...

tips for choosing tags that are nested within specific parent tags

I'm looking to locate and count all the <"a"> tags within every <"code"> tag on a given webpage using JavaScript. How can I accomplish this? I attempted methods like document.getElementsByTagName("code").getElementsByTagName("a"); and document. ...

Using Jquery to toggle the visibility of divs based on radio button selections

I am struggling to hide the divs with the radio buttons until their title is clicked on. However, I am facing issues as they are not showing up when their title is clicked on. Any assistance would be greatly appreciated. SPIKE <!DOCTYPE html> &l ...

Can you please explain how to indicate a modification in a JSON object with Polymer, transferring information from Javascript, and subsequently displaying child elements?

Currently, I am creating a JSON file that contains a random assortment of X's and O's. My goal is to display these elements in a grid format using a custom Polymer element. Initially, everything works fine as I can see a new grid generated each t ...