Utilizing Vue and Axios for advanced data filtering techniques

Struggling with an API listing project. I need to display the results of an API call and allow users to search for specific values. The displayed result is already in v-for, so filtering it again isn't an option. I'll have to make another axios call upon clicking the search button and filter the response. The API's structure is complex, making it challenging to simply attach +this.searchedValue because it's a nested array. Is this approach feasible? Open to alternative solutions.

The code below displays the list, but I'd like to generate a new list based on the searched value entered in the search bar.

<ul>
  <li @click="select(content.id)" 
  v-for="(content,id) in contents" 
  :key="id"
  >
    <h4> {{ content.department.label }} </h4>
    <p> {{ content.location.city }} </p>
  </li>
</ul>

 mounted() {
      axios.get('https://api/')
                        .then(response => {
                            this.contents = response.data.content;
                        })
    }

I attempted looping in the filter as follows, but the list didn't appear:

 computed: {
  Filter: function(){
            return this.contents.filter((content) => {
               return content.match(this.filter);
            });
     }
  }

Answer №1

To optimize the code, I recommend moving the filter logic into a computed property:

<input type="text" placeholder="Search" v-model="search" />
<li v-for="(item, index) in filteredList">
 {{item}}
</li>

data() {
    return {
        search: ''
    }
},
computed() {
    filteredList() {
        let self = this
        return this.contents.filter(item => item.department.label.toLowerCase().indexOf(self.search.toLowerCase()) >= 0)
    }
}

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

Encountering an error when using Angular Material virtual scroll in conjunction with Angular-UI

Trying to incorporate Angular material's virtual scroll feature on angular-ui-tree is proving to be a bit challenging. The error message that keeps popping up is: Controller 'uiTree', which is required by directive 'uiTreeNode', ...

Redefining the onClick function in the Redux 'presentation' component to include parameters

How can I pass an object as an argument to a function in a redux "presentation component" efficiently? In my <BookList /> container component, I display a <BookListRow/> presentation component for each book. I want to add a button in each Boo ...

Having difficulty with building a basic module in Node JS, it's just not cooperating

As a newcomer to Node JS, this platform, and the English language, I apologize in advance for any linguistic errors. I seem to be encountering a "return" error within my code. Specifically, when I include the hi.myFunc(); function, I receive the ...

Using JavaScript and jQuery to compare two JSON objects, then storing the result in a separate object

I am currently working on an API call that provides an updated JSON object, as well as a static JSON object file. My goal is to compare a specific value in the objects for teams with the same name. For example, if Team John had 22 members in the old file ...

How come it's so difficult for me to find a simple demonstration of v-for in action in Vue JS?

After searching for a simple "hello world" example of using v-for in Vue.js, I have spent an hour looking at various posts, like the one on Stack Overflow, but still no luck. This is my HTML: <ul> <li v-for="item in history"> ...

`Vue Component failing to display data from Blade File`

In my project using Laravel Blade, I am currently in the process of converting some blade files to Vue components. One challenge I encountered is trying to display a dynamically created page title on the screen from the Vue component rather than the blade ...

Jquery recommended value feature is malfunctioning

I am having trouble with this function and I really need some assistance to make the data auto-complete in the suggested text box. $('#BrandName').autocomplete({ source: function (request, response) { $.getJSON("/DataCollect ...

Converting a JavaScript IIFE library into a React Component

Hello, I am currently in the process of learning React JS and there are two JavaScript files that I am working on: Polyfill.js -> hosted on github CustomNavbar.js -> created by me Here is the structure of polyfill.js: export default (function(win ...

When using $http in AngularJS, an error may occur when it

I ran a test on my PHP code independently and obtained the following output: {"tabId":1,"tabName":"Main","uId":"1"}{"tabId":2,"tabName":"Photography","uId":"1"} However, my AngularJS application is unable to receive the callback and is throwing an error ...

Exploring how Node.js utilizes regular expressions in parsing JSONStream data

Here is an example of a payload I am working with: { "rows": [{ "id": "1234", "data": { "updatedby": "uid1", "resource": { "resourceid": "abcd" } } }, { "id": "123 ...

An unexpected issue occurred while converting XML data into JSON format

I have been using a piece of code that converts XML to JSON: // Converting XML to JSON var XmlToJson = function xmlToJson(xml) { //console.log('called xmltojson'); //console.log(xml); // Creating the return object var self = this ...

ASP.NET - The Power of a Long Press

I am currently working on implementing a Long Press event in JavaScript on an ASPX page. Due to my limited experience with JavaScript, I am facing a few challenges. I found a similar question that was previously asked here. When running the code, I encoun ...

Determining the parameter type for the directive

How can you specify the types of parameters for the directive in AngularJS? Which type should be used for & binding? Refer to ngdoc or jsdoc for an example code. UPDATE: I am looking for answers to the following questions * @param {<< What sh ...

What is the best way to repair all the data or preserve it using my current theme?

Seeking assistance here. I am new to developing themes and have a query. Let's say, I have developed a theme myself. Now my question is, after creating menus, posts, pages, etc., when I install just the theme on another WordPress site, all the data ge ...

Issue Detected: The function this.route.params.flatMap is not recognized

Seeking guidance on transitioning to id, or a similar concept that's hard to articulate. Upon clicking a button to navigate to a specific user, encountering the following error: ERROR TypeError: this.route.params.flatMap is not a function at UserSho ...

Can someone help me identify the table rows that are adjacent to a dropped row in a jQueryUI Sortable setup?

My current setup involves using the jQueryUI Sortable plugin on a table, and thankfully everything appears to be functioning correctly. Both the HTML and JavaScript are passing validation. However, I am faced with the challenge of finding a way to identi ...

What could be causing the array state change to not trigger a re-render?

As I was going through the React learning section, I stumbled upon an interesting issue. In my code, I am creating three shapes from an array called "shapes". When I press a button, the entire array is modified and I can see the changes reflected in the ar ...

Could the sluggish WebGl rendering speed be attributed to the size of the JSON file?

Attempting to display a highly complex model using a JSON file, which is quite large at 40MB. Despite being able to render the model on canvas, encountering severe sluggishness during the process. The issue arises when trying to manipulate the model by ro ...

Trigger the submission of a form on the current php page when a checkbox is altered

Looking for assistance with the following code: <form id="form" method="post" action=""> <input type="checkbox" name="checkbox" onchange="$('#form').submit();"> <label class="onoffswitch-label" for="myonoffswitch"> <span ...

Error occurred while parsing JObject in JSON.NET

I need to send a JSON object using AJAX and Web Api to my Server: var data = [ ["fdsfsd", "Kifdsfa", "fsdfsa", "fadsf", "fasdfsd", "fadsf", "fasdfsd"], ["2008", "-5", "11", "12", "13"], ["2009", "20", "-11", "14", "13"], ["2010", "30", "15", "-12" ...