Implementing a Vue.js search function with two v-for loops

I have computed the following filter:

<script>
    export default {
        data() {
        return {
           columns:{},
           search:"",
           trovato:{},
        };
        },
        
        
        computed: {

       
           searchit() {

              const sear =this.search.toLowerCase().trim();
              if(!sear) return this.columns;
              Array.from(this.columns).forEach(element => {
                 element.filter((item) => {
    
 
                    if(item.toLowerCase().includes(sear)){
                     
                       this.trovato = item;
                       console.log(this.trovato);
                       return this.trovato;
                    }
                 
                })
             })
           }
    }
</script>

It is functioning well, with the console outputting the correct column that matches the searched name.

The issue arises when trying to display it in a v-list. Initially, all values of the variable 'columns{}' are displayed, but after typing something, nothing appears; it's all empty. This is the code being utilized:

<v-list-item>
   <v-text-field append-icon="search" v-model="search" label="Search" ></v-text-field>
</v-list-item>
<v-list v-for="(item,index) in searchit" :key="index">
     <v-list-item v-for="ved in item" :key="ved.id">
         <v-list-item-content>
                                    
               <v-list-item-title >{{ved}}</v-list-item-title>
                                   
                                   
         </v-list-item-content>
     </v-list-item>
</v-list>

Answer №1

Array.prototype.filter creates a new array without changing the original one. (check out documentation)

By filtering here, you're not storing the result anywhere, so it just disappears.

Furthermore, it's not recommended to modify a component's data inside a computed property. Instead, you should return a fresh object or array based on that data.

searchit() {
  const sear =this.search.toLowerCase().trim();

  if(!sear) { return this.columns; }

  const newColumns = this.columns.map(col => {
     return col.filter((item) => {
        if(item.toLowerCase().includes(sear)){
           this.trovato = item; // It's best to avoid updating data within a computed property
           return this.trovato;
        }
      })
  })
  return newColumns;
}

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

Ensuring type safety while dynamic object property setting in VueX Store using TypeScript

Currently, I am working on a Vue 3 project with TypeScript, transitioning from JavaScript, and keeping it simple without using complex plugins for Vuex store. I have a mutation that dynamically sets various properties of an order object to eliminate the ne ...

I aim to customize the options of a dropdown list based on the selection of another dropdown

I am looking for a way to dynamically filter employees based on the department selected. Unfortunately, my knowledge of JavaScript and Ajax is limited. <div class="pure-checkbox ml-15"> <input id="checkbox2" name="sta ...

Ways to update the color of text exceeding the block boundaries: JavaScript and CSS

When dealing with background images and white text, it's common for the text to extend beyond the boundaries of the image and become invisible. I'm looking for a way to color the overflowing text in the same shade as the background image (blue). ...

Guide on submitting a form through the Angular 2 HTTP post method with JavaScript

Currently working on grasping the concepts of Angular2, but facing challenges when attempting to utilize http.post() for submitting a form to my Web API. ...

Updating Bootstrap modal content based on button clickExplanation on how to dynamically change the content

I am looking to dynamically change the content displayed inside a modal based on the button that is clicked. For example, clicking button one will only show the div with the class 'one' while hiding the others. $('#exampleModalCenter&a ...

Is there a way to view an image before and after uploading it?

I am encountering an issue while attempting to display a preview of an image in a form. The HTML code involved is as follows: <form action="" method="post" enctype="multipart/form-data" name="personal_image" id="newHotnessForm"> <p><lab ...

Updating MySQL records with Node.js

Currently, I am extracting data from an API and organizing it into an array ([arr1]). I am utilizing node-mysql to interact with my database and insert the fetched information. Everything is functioning smoothly when inserting new entries into the databas ...

Received an error while attempting an AJAX GET request to a distinct server hosting a WebAPI

This is my first time encountering an issue with an Ajax request in client-side code. I'm hoping that there's a simple mistake in my code that I'm just overlooking. Just to give some background, when I manually access the URL mentioned below ...

Tips for auto-saving data in Laravel with AJAX after a few seconds?

I've set up the article post section in my project. I want to automatically save data as a draft in my articles table when creating a new post. I have included the blade file with a form and ajax code, but the ajax code doesn't seem to be working ...

What is the best way to set the source of an image using JavaScript

On a recent project, I came across this function in PHP: foreach ($reports["included_reports"] as $index => $report_name ) { if (! in_array( $report_name, $reports["excluded_reports"])) { $optional_reports .= "<div class=\"la ...

Obtaining the image with PHP

As a newcomer to PHP and Ajax queries, I am trying to utilize the croppie.js plugin to upload a profile image. However, when I upload the image, it is saved as 1580192100.png. The problem arises when attempting to later retrieve the image based on the user ...

The condition is not being recognized after clicking the third button

When the button is clicked for the first time in the code snippet below, all divs except for the red one should fade out. With each subsequent click, the opacity of the next div with a higher stack order should be set to 1. Everything works fine until the ...

Setting attributes to a DOM element using String in jQuery

I am currently facing a challenge where I have a list of attributes saved as a string variable, and I need to add that variable to a <div>. Unfortunately, I am stuck and uncertain about the best approach. Here is what I have so far: HTML: <div&g ...

Having trouble getting Node.js to run Express.js and React.js simultaneously

My tech stack consists of reactjs for the frontend and expressjs for the backend API. I experimented with the following setup: { "name": "carweb", "version": "0.1.0", "private": true, "dependencies": { // list of dependencies }, "scripts ...

The navigation underline stays in place even after being clicked, and also appears below

Take a look at this js fiddle I've managed to create the underline effect on the navigation links when the user hovers over them. However, the underline only stays visible until the user clicks elsewhere on the screen. How can I make it persist as l ...

Showcasing the information stored within my li element, I am able to access and view the data through my console

I want to showcase the data in the browser Upon hitting the api call, I retrieve the data in my console. The challenge lies in displaying the data within my li tag. Below are snippets of my relevant code and the entire code can be found in the gist links p ...

looping through a collection of table rows using v-for

In a Vue app, I am facing an issue with rendering multiple table rows for each item in a collection. The current markup I have for rendering the table body is as follows: <tbody> <template v-for="item in collection"> <tr> < ...

Running a function following several iterations of angular.forEach

let values1 = [1, 2, 3]; angular.forEach(values1, function(value){ $compile(value)($scope); }); let values2 = ['a', 'b', 'c']; angular.forEach(values2, function(value){ $compile(value)($scope ...

Unforeseen behavior in Ajax success callback

My unordered list contains a href link. Initially, only the welcome page is visible on page load. Upon clicking the list for the first time after page load, the desired results are displayed in the corresponding div as expected. The issue arises when swi ...

The HTML button with Google's material-icons appears noticeably small when displayed on mobile devices

Looking to design a basic HTML page with a button placed in the lower right corner? Check out the code below: <!DOCTYPE html> <html> <head> <title>Sample Page</title> <style> /* Styles for the container * ...