The search and filter functionality in Vue JS is still malfunctioning

I am currently working on improving the functionality of my search and filter features. I have successfully implemented a search feature that filters results by stock, distance, price, and time response. While the search feature is functioning properly, the filter feature that I created is not working as expected.

My goal is to provide users with additional filtering options after they conduct a search. For example, selecting criteria such as stock and distance should dynamically update the search results based on the selected filter (e.g., displaying items available in stock). Other filters like price and time response should also work similarly - sorting items by price or response time upon selection.

Example Scenario:

When choosing Type as BMW and performing a search, 2 item results with type BMW should be displayed. Subsequently, selecting 500 KM in the Distance filter should refine the results to show only 1 item. However, the filters are currently malfunctioning.

In cases where there are 6 item results, clicking on the price filter should organize the items from lowest to highest price.

I have included a code snippet below detailing my implementation. Any assistance in resolving this issue would be greatly appreciated.

new Vue({
  // JavaScript code goes here
})
.list-item{
  /* CSS styling for list items */
}

/* Additional CSS styles go here */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.1/vue.js"></script>

<div id="app">
  <!-- HTML markup for the app goes here -->
</div>

Answer №1

To see a live demonstration based on your code, please check out this codesandbox link.

Changes I Made

  1. I introduced a new filterResult in the data section to handle filter results separately from search results. This makes the logic of 'search' and 'filter' clearer and more organized.
  2. Instead of using computed properties, I changed the trigger for the filterItems method to occur upon a value change with @click. This simplifies the process and makes it more direct.
  3. When a search is initiated, the filter select values are reset and all search results are displayed. Since no filter value is selected, filterResult remains the same as searchResult.
  4. For sorting functionality, I updated the this.sort data when sorting criteria are modified within the two sort span elements. Using a watch trigger ensures that the sortItems method is called whenever the sorting criteria change. You can customize the sortItems method according to your requirements. Alternatively, you have the option to create separate methods for each 'sort' button.

Answer №2

To trigger the filtering process whenever the select value is changed, utilize the @change event along with the filterItem computed property.

<select v-model="selectedStock" @change="filterItem">

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

What is the best way to create a flexible Ul-LI menu?

Is it possible to create a flexible menu where clicking on an item does not cover other items in the menu? #demo { margin: 30px 0 50px 0; font-family: sans-serif; } #demo .wrapper { display: inline-block; width: 180px; height: 20px; position ...

Troubleshooting a CORS problem with connecting an Angular application to a Node server that is accessing the Spotify

I am currently working on setting up an authentication flow using the Spotify API. In this setup, my Angular application is making calls to my Node server which is running on localhost:3000. export class SpotifyService { private apiRoot = 'http://lo ...

Retrieve an image from the server and display a preview of it on the client side

I'm currently in the process of fetching an image from a server and previewing it on the client side. I have successfully retrieved the image, but I am unsure of how to asynchronously display it on a web page. axios.get(link,{responseType:'strea ...

I'm experiencing an issue where the code I copy from jsfiddle does not function properly when pasted into notepad++

While browsing through the code on this page javascript countdown timer pause resume, I came across the following interesting snippet: var CountDown = (function ($) { // Length ms var TimeOut = 10000; // Interval ms var TimeGap = 1000; var CurrentTime = ...

Guide on incorporating v-slot functionalities into a custom component with Vuetify 3 data-table

I am trying to utilize slots in my custom component (@/components/MyDataTable.vue) to retrieve v-data-table items and headers. <template> <div> <v-data-table :headers="headers" :items="items"> </v-data-table&g ...

Are you experiencing issues with the AJAX auto-suggestion system displaying duplicate entries?

I've implemented a search bar on my website that allows users to search for specific titles. To enhance the user experience, I added an auto-completion feature using a simple MySQL table with suggested completions. Here's an image of the setup: ...

Extending State Interface in ReactJs and Typescript: A Step-by-Step Guide

I have the following: interface EditViewState<T> { entity?: T; } abstract class EditView<T, P, S> extends React.Component<P, EditViewState<T> & S> { constructor(props: P, ctx: any) { super(props, ctx); this. ...

What is the best way to leverage an existing user database and integrate user authentication into a fresh project?

I have recently taken on the task of revamping an established project by creating a brand new frontend from scratch. After careful consideration, I have opted to develop a straightforward Vue/Node.JS project. Within the current framework lies a Postgres d ...

What is the best way to attach an event listener to a div so that clicking on it increases the width and height of the div by 20%?

I've started working on it and decided to use the same dimensions (100px by 100px) as a previous question. Right now, I'm sticking with just Javascript, but I might incorporate jQuery down the line. One big issue is that in the line (this.style.w ...

Can the getState() method be utilized within a reducer function?

I have encountered an issue with my reducers. The login reducer is functioning properly, but when I added a logout reducer, it stopped working. export const rootReducer = combineReducers({ login: loginReducer, logout: logoutReducer }); export c ...

Starting Array index value at 1 in React js: A step-by-step guide

Is there a way to make the index value start from 1 instead of 0? {props.useraccountListData.useraccountTypeList.map((item, index) => ( {index} ))} The current output is starting from 0, 1, 2. However, I would like it to start from 1, 2, 3... ...

Error: When executing the npm run build command, I encountered a TypeError stating that Ajv is not a

I keep encountering an issue whenever I try to execute npm run build error: /node_modules/mini-css-extract-plugin/node_modules/schema-utils/dist/validate.js:66 const ajv = new Ajv({ ^ TypeError: Ajv is not a constructor at Object.<anon ...

Ways to dynamically include onClick on a div in a react component based on certain conditions

Is it possible to conditionally set the onClick event on a div element in React based on the value of a property called canClick? Instead of directly checking this.state in the event handler, I am hoping to find a way to implement this logic within the re ...

Tips for dynamically resetting the dataTable

When I create a datatable and add rows dynamically based on the selected option, I encounter an issue where I need to reinitialize the dataTable every time the option is changed. To address this, I have placed the reinitialization code inside the $("#selec ...

Having trouble with passing the callback for nested mysql queries in Async.waterfall?

I am facing an issue with my nested MySQL queries where async.waterfall is not working as expected. The second step of the waterfall is failing to append its result to the array: async.waterfall([ function(callback) { connection.query(query, function( ...

Customize the HTML tags in the Froala text editor with easy insertion and removal

In my AngularJS project, I am utilizing Froala editor. I want to create a unique functionality where a custom button wraps selected text with <close></close> tags when activated. Moreover, if the selected text is already wrapped with these tags ...

Tips for utilizing express in your typescript projects

I'm curious about the transition of definition files from tsd to typings, and now to @types. How can I incorporate @types in a node/express project? What is currently preferred and what's the reason for moving from tsd to typing and finally to @t ...

Error: Attempted to search for 'height' using the 'in' operator in an undefined variable

I came across the following code snippet: $('.p1').click(function(){ var num = 10; var count = 0; var intv = setInterval(anim,800); function anim(){ count++; num--; ...

Vue.js array successfully updated based on the current status of the API

I am a beginner in Vue.js and I am working on calling an API. It is functioning perfectly, but I need to update the Vue.js array based on a condition of the API response. However, when trying to do so, I encounter the error "do not mutate vuex store stat ...

Unexpected absence of Aria tags noticed

I've been working on integrating ngAria into my project. I have injected it into my module and created the following HTML: First Name: <input role="textbox" type="text" ng-model="firstName" aria-label="First Name" required><br> Employee: ...