Struggling to Get Vuejs Custom Search Filter Functioning Properly Using Computed Property

I am currently working on implementing a custom filter search feature for my project. However, I am encountering an issue where the filtering based on keywords does not seem to work as expected. Instead of displaying the filtered posts, it simply shows all posts. It appears that the value of this.search is always empty in this scenario. What could be a potential solution to address this issue?

<form>
    <fieldset class="w-100 left">
        <input type="text" placeholder="Filter posts by keyword" v-model="search"/>
    </fieldset> 
</form>
<div>
    <div id="mainSec">
        <div v-for="post in filteredPosts">

            <div v-for="result in results" v-if="post.userId == result.id" class="left ml-3 mt-3" > 
                <p >{{result.name}}</p>
                <p>{{result.email}} | {{result.address.city}}</p>
            </div>  
            <p style="float:left;width:100%"><strong>{{post.title.toUpperCase()}}</strong></p>
            <p style="float:left;width:100%">{{post.body}}</p>
        </div>  
    </div>
</div>
</html>
<script>

var  vm = new Vue({
    el: '#mainSec',
    data() {
        return {
            search : '',
            results : [],
            posts : []
        };
    },
    async created() {
            try{
                axios.get(urlUsers).then(response => {
                    this.results = response.data
                });
                axios.get(urlPosts).then(response => {
                    this.posts = response.data
                });

            }
            catch(error){
                console.log("Error is " + error);
            }

    },
    computed : {
        filteredPosts (){
            if(this.search){
                return this.posts.filter((item)=>{
                    return item.title.match(this.search);
            })
        }else{
            return this.posts;
        }
    }
})
</script>

Answer №1

It seems like the issue stems from using el: '#mainSec', to target a specific part of your HTML code. This means that the v-model="search" is not functioning, causing the variable to remain static and the computed function to never execute.

To resolve this, a simple solution would be to encompass both your <form> AND your <div id="mainSec"> within a div element that has the ID of mainSec. Alternatively, you can assign a separate ID and then reference it in the Vue el attribute.

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

Confirm proper functioning of Dropdown without the need for a form tag

We offer users the ability to choose a "Brand & Model" and then click on the "See cases" button. Once the selection is made, it will redirect to the appropriate URL. If someone clicks directly on "see cases" without selecting a Brand & Model, it redir ...

jQuery form validation with delay in error prompts

I am experiencing a strange issue with my HTML form validation function. It seems to be showing the alert div twice, and I can't figure out why this is happening. Adjusting the delay time seems to affect which field triggers the problem. Can anyone sp ...

Is it possible to divide a text node in jQuery or Javascript into two or three separate parts?

I have a section that contains 3 lines of text. My goal is to divide this section into two equal parts based on word count, with each part having approximately 150 characters. Allow me to provide more context: The following text snippet consists of 6 lin ...

Tips for building a dynamic navigation menu using AngularJS

Looking to dynamically generate a navigation menu using an angularjs controller: index.html: <body> <div ng-controller="navi"> <ul> <li ng-repeat="nav in navigations"> <a href="{{nav.path ...

"Exploring the capabilities of speech recognition in Ionic 4

Having some trouble with the Speech-recognition feature in my Ionic 4 app. Here is a snippet from my code: import {Component} from '@angular/core'; import {SpeechRecognition} from '@ionic-native/speech-recognition'; @Component({ se ...

What is the best way to query across multiple HTML tables?

Is there a way to search across multiple tables in HTML? I currently have code that works for one table, but I need it to work for two or more tables. This is the code I am using to search for "something" within my table. <script> function myFu ...

What is the best way to add an event listener to every item in a list within a Vue component

Here is a component structure that I am working with: Vue.component('navbar', { props: ['navitem'], methods: { users: function () { //users code }, test:function(){ } }, template: '<li v-on:cl ...

Verify that the JSON data contains the desired data type before adding it to jQuery

I'm working with JSON data extracted from an Excel file. I need to match the First Name in the JSON data with the 'First Name' field and append those elements to a specific div. Additionally, if the JSON data includes a 'status', I ...

How can you efficiently pass the index as a prop to a child component in React.js when dealing with arrays stored in

Just starting out with React, so bear with me if my terminology is a bit off. I'm working on a table that displays a list of people in a specific order. I want to be able to assign a this.props.tablePosition value based on the index of each person. t ...

Unable to expand the Navbar toggler feature in Bootstrap 5

Despite searching for solutions to this issue in previous discussions, I have been unable to successfully implement them. Recently, I was experimenting with Bootstrap 5, specifically the collapsible navbar button feature. The idea is that when the viewpor ...

Guide on integrating next-images with rewrite in next.config.js

I'm currently facing a dilemma with my next.config.js file. I've successfully added a proxy to my requests using rewrite, but now I want to incorporate next-images to load svg files as well. However, I'm unsure of how to combine both functio ...

Selecting either "THREE.NearestFilter" or "THREE.LinearFilter" will result in the black plane

During my experimentation with the THREE.js library, I encountered a notification stating: THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter. To address this issue, I made the ...

Validation of partial view is not being triggered after a button click when it is dynamically loaded using jQuery get method

I'm currently working on a project using MVC4 and implementing jQuery dialog to display a pop-up for users to edit data. When the user clicks on the Edit action link, a pop-up appears with a form allowing them to edit their information. <a href= ...

Is it possible to create a graph using only links from a JSON file when using D3.js?

I am looking to extract node information from links data stored in a JSON file. My ultimate goal is to visualize this data by creating a graph with connections and edges. Here is a snippet from my sample JSON file: { "links": [ { "source":0, ...

Is there a way to extract an icon from an object or array?

Currently, I am facing challenges in extracting an icon from either an object or an array for a project I am working on. My approach involves storing the icon in a variable and passing it as a prop to another component. However, the functionality is not wo ...

Enhance the functionality of your Rails application by implementing Ajax or jQuery to asynchronously load table elements separately from the page

Currently, I am facing an issue with a page that displays a list of user sites. The problem lies in the fact that I am making an API call for each site to check its status, which is causing the page to load very slowly. To address this issue, I would like ...

Creating a multilingual website with Nextjs 13 App Router using internalization, all without the need

As I develop a web app that requires user authentication to access, I am currently using Next.js 13 with the App Route (not Pages Route). My goal is to implement internationalization without having to create sub-routes like /en, or use domains such as mywe ...

Having issues with AngularJS ng-if when implemented within a Form

Is there a way to hide my form after it has been submitted using ng-if? I am facing an issue where clicking the 'See' button toggles the form on and off, but the same functionality does not work with the 'Add' button. Any insights on wh ...

Is there a way to retrieve the row and parent width from a Bootstrap and Aurelia application?

Is there a way to determine the exact width of a bootstrap grid row or grid container in pixels using Aurelia? I attempted to find this information on the bootstrap website, but I am still unsure as there are multiple width dimensions such as col-xs, colm ...

SQL AJAX Query Form

I have been searching for tutorials on how to create a good form with PHP and AJAX. I tried starting with a code given to me by a friend and managed to send the request successfully. However, it seems like the data I receive is empty. Could you please take ...