Is there a way to conceal the search bar within my Vue.js web app upon clicking a link within a child component?

In my component hierarchy, I have a child component called home-book-line:

<template>
  <div>
    <div v-if="items && items.length > 0">
      <nuxt-link :to="`/books/${item.id}`" @click="closeAndHideSearchBar" class="d-flex align-center pb-4 pr-2" v-for="item in items" :key="item.id">
        <v-img width="30" height="44" contain :src="item.image" class="bookImage" />
        <div class="mr-3 text-right">
          <span class="body4-medium" style="color: #262626">{{ item.title }} / </span>
          <span class="body4-medium" style="color: #262626" v-for="(associate, index) in item.associates.author" :key="associate.id">
            {{ associate.title }}
            {{ index < item.associates.author.length - 1 ? ', ' : '' }}
          </span>
          <span class="caption3-regular s60--text d-block" v-for="associate in item.associates.category" :key="associate.id">{{ associate.title }}</span>
        </div>
      </nuxt-link>
    </div>
    <div v-else class="body1-medium text-center mt-4">No results found.</div>
  </div>
</template>

<script>
export default {
  name: "bookLink",
  props: ['items'],
  methods: {
    closeAndHideSearchBar() {
      this.$emit('closeSearchBar');
    },
  },
}
</script>

This component is used in another child component called home-search-bar-app:

<template>
  <v-card class="borderRadius-4 pa-2" style="box-shadow: 0px 0px 8px rgba(145, 145, 145, 0.15) !important" :showSearchBar.sync="showSearchBar">
    <v-row class="ma-0">
      <v-col cols="12">
        <v-tabs v-model="tabs" color="#091E42">
          <v-tab href="#all">All</v-tab>
          <v-tab href="#book">Book Name</v-tab>
          <v-tab href="#category">Category</v-tab>
          <v-tab href="#editor">Author Name</v-tab>
        </v-tabs>
        <x-hr />
      </v-col>

      <v-col cols="8">
        <div>
          <v-tabs-items v-model="tabs">
            <v-tab-item value="all">
              <home-book-link v-if="allBookLoadig" :items="allSearchResult" @closeSearchBar="closeSearchBar" />
              <!-- <home-expansion-panels :panels="panels" /> -->
            </v-tab-item>

            <v-tab-item value="book">
              <home-book-link v-if="bookNameLoading" :items="bookNameSearchResult" @closeSearchBar="closeSearchBar" />
              <!-- <home-expansion-panels :panels="panels" /> -->
            </v-tab-item>
            
            <v-tab-item value="category">
              <home-book-link v-if="categoryLoading" :items="categorySearchResult" @closeSearchBar="closeSearchBar" />
            </v-tab-item>
            <v-tab-item value="editor">
              <home-book-link v-if="authorLoading" :items="authorSearchResult" @closeSearchBar="closeSearchBar" />
            </v-tab-item>
          </v-tabs-items>
        </div>
      </v-col>
    </v-row>
  </v-card>
</template>

<script>
export default {
  name: "SearchBarApp",
  props: { toggle: false, searchText: String, showSearchBar: false },
  methods: {
    closeSearchBar() {
      this.$emit('close');
    },
  },
}
</script>

This component is then used in a parent component as follows:

            <v-col cols="7" style="position: relative" class="px-0 mx-2">
              <x-input
                class="body3-regular serach-input"
                id="grey"
                placeholder="Search for book name, category or author name"
                appendIcon="mdi-magnify"
                v-model="searchText"
                @input="showSearchBar = searchText.length > 0"
              />

              <home-search-bar-app
                ref="searchBar"
                :showSearchBar.sync="showSearchBar"
                @close="showSearchBar = false"
                style="position: absolute;top: 90px;left: 0px"
                :search-text="searchText"
                @changeSearch="(val) => changeSearch(val)"
                class="d-none d-sm-block w-full"
              />
            </v-col>

I am trying to hide the home-search-bar-app component when clicking on the home-book-link component and set showSearchBar to false. However, I have been unsuccessful in handling the emit event. If anyone could assist me with this issue, I would greatly appreciate it.

Answer №1

It appears that you have not utilized the v-if or v-show directives on the element that needs to be toggled. You should consider implementing v-if="toggleElement". This way, when "toggleElement" is false, the element will be removed, and when it's true, the element will be displayed.

v-if will completely remove the element from the DOM, while v-show will simply hide it without removing it. I trust this explanation clarifies things for you.

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

Ways to verify whether a vue instance is empty within a .vue file by utilizing the v-if directive

I am facing an issue with a for-loop in Vue that iterates through a media object using v-for to check if it contains any images. Everything is working correctly, but I want to display a div below the loop saying "There is no media" when the object is empty ...

Achieving dynamic key assignment when updating with mongoose in NodeJS and Express

With a multitude of keys requiring updates from a single function, I am seeking guidance on how to dynamically set the key for updating. static async updateProfile(req, res, next) { const userId = req.body.userId; // The key requiring an update ...

Ways to Manage modals responses as services in AngularJS

I am a beginner in the world of JavaScript and AngularJS. I am fascinated by some of the concepts within JS, like trying to create a modal service using the example I found online. I am eager to delve deeper into what exactly is happening under the hood, e ...

Is there a way to customize the ::selection style using mui createTheme?

I'm looking to globally change the color and background color of ::selection in my app. Can anyone advise on how to achieve this using createTheme()? ...

How can I trigger an Onclick event from an <a tag without text in a Javascript form using Selenium?

Here is my original source code: <a onclick="pd(event);" tabindex="0" issprite="true" data-ctl="Icon" style="overflow: hidden; background: transparent url("webwb/pygridicons_12892877635.png!!.png") no-repeat scroll 0px top; width: 16px; height: 16px ...

Tips for integrating Search functionality using Jquery

I'm struggling to figure out how to create a search function that automatically updates the results as soon as a key is pressed. It also needs to be case insensitive, and if there are no results, it should display a message with the class "not-found". ...

Encountered JSON Parsing Issue (lack of knowledge in JSON handling)

As I am moving a system from one server to another, I encountered an error when trying to access a particular page. Strangely, the code is identical to the live version of the system where it works perfectly fine. The JavaScript code causing the error is ...

Encountering an issue with Tiptap2 floating menu while using script setup in Vue 3

Currently, I am trying to integrate a floating menu in Vue 3 by following the guidelines in the tiptap documentation. However, I am facing difficulties as the floating menu does not seem to render properly. I am using script setup and not sure if that is c ...

Avoid multiple delays when clicking the identifier

I came across this code snippet: $(element).on('click', function() { $this.closest('div').before('<span id="message" style="display:none"></span>'); $('#message').fadeIn().delay(5000).fadeOut(); ...

Resizing tables dynamically using HTML and JavaScript

I am attempting to reproduce the functionality demonstrated in this example When dealing with a large table, I want it to resize to display 10 entries and paginate them accordingly. The only thing I require is the pagination feature without the search bar ...

Determining the vertical position of an element that is not currently visible on the

I am incorporating elements using Ajax. I need to determine their placement based on height, similar to a box-packing algorithm. However, when I iterate through the elements like this, the outerHeight always returns 0: posts.each(function(i, e) { var ...

Disabling caching in bootstrap tabs for loading ajax content

My goal is to make bootstrap tabs load content through ajax queries. While this process is straightforward with Jquery tabs, which default to loading content via ajax query, it seems to be a different case for bootstrap. As such, I have come across the fo ...

A guide on transferring MySQL data from PHP to JavaScript using XMLHttpRequest and displaying the data in chronological order

After successfully retrieving the necessary MySQL data and displaying it in the notification container, I encountered an issue with manipulating the data. Instead of printing individual objects, the same object is being printed multiple times. Here is the ...

Encountering issues with querySelector() across certain classes

I need to display the actual date in my first column, but my JavaScript script is only working for one class in my HTML code. Question: How can I make my JavaScript code work for every class in the HTML code? I am currently using querySelector(), but I fe ...

The unpredictable number generator fails to generate the anticipated numbers within a specified range by utilizing input values from the fields

Trying to create a random number within specified boundaries has been more challenging than expected. Although I have a function that works flawlessly under normal circumstances, it seems to give unexpected outputs when the minimum and maximum values are d ...

What is the process for accessing the data attributes of div elements and then transferring them to a cookie?

Although this question may have been answered before, I couldn't find a specific solution. Imagine I have the following div elements... <div class="listings-area"> <div itemtype="http://schema.org/Product" itemscope=""> <a class="lis ...

What is the best way to close ngx-simple-modal in angular7 when clicking outside of the modal component?

Can anyone help me with closing the modal in my angular7 app when the user clicks outside of the modal component? I have been using the ngx-simple-modal plugin, and I tried implementing the following code: this.SimpleModalService.addModal(LinkPopupCompone ...

What is the best way to ensure that a navbar dropdown appears above all other elements on

I'm having trouble creating a navbar dropdown with material design. The dropdown is working fine, but the issue I'm facing is that other elements are floating above it. https://i.stack.imgur.com/aJ0BH.png What I want is for the dropdown to floa ...

What is the best way to incorporate delete buttons for clearing the entire state and removing specific states generated by the .map function?

I attempted to initialize the array to clear all elements inside of it, however, it was not successful. clearAll(){ this.setState({ posts: [] }) } componentDidMount() { axios.get('https://jsonplaceholder.typicode.com/posts') ...

Attempting to grasp the concept of working with React Native and retrieving data from local storage using AsyncStorage

I'm struggling to display the saved storage value in a component when console logging it. Below is the basic structure of a react native page: import React from 'react'; import { AsyncStorage, StyleSheet, Text, View } from 'react-nati ...