Is there a way to live filter results using Vue?

Looking to apply a filter on my input v-model to search through a list of products.

The current filter only shows items with an exact match to the input. How can I adjust it to display partial matches as the user types?

Vue.filter('byName', function (data, input) {

if(input){
    return data.filter(function (item) {
        return item.name == input
    });
}

return data

});

Answer №1

If you are looking for real-time results, it's important to adjust your filter to return items that partially match.

One way to achieve this is by utilizing the startsWith() method. The below filter utilizes startsWith() to identify all items that begin with the specified input:

Vue.filter('byName', function (data, input) {
  if(input){
    return data.filter(function (item) {
      // check if the item's name starts with the input
      return item.name.startsWith(input);
    });
  }
  return data;
});

Experience this filter in action through the following JSFiddle link.

Alternatively, if you prefer to retrieve items that match anywhere within their content, rather than just those starting with the input, you can opt for String.prototype.indexOf().

To implement this change, simply substitute

// we check if the item's name starts with the input
return item.name.startsWith(input);

with

// we check if the item's name contains the input as a substring
return item.name.indexOf(input) > -1;

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

Changes in a portion of the state for Vaadin's AbstractJavascriptComponent

I am currently working on implementing a JavaScript-based component for Vaadin that will be responsible for displaying and updating a large data set. To achieve this, I am extending AbstractJavaScriptComponent. My goal is to keep the JavaScript side as si ...

Exploring the possibilities of using AngularJS for AJAX functionality in a Ruby On Rails

I recently started learning AngularJS and Rails, and I attempted to develop a Rails application incorporating AngularJS. Currently, I am looking to make a POST request to send data and insert it into the database. In the Activity Controller: def create ...

Exploring each list item within the specified unordered list

Here is a basic code snippet: var ulreq = $("#abc").children("ul.ghi"); var lists = ulreq.find("li"); for( var i = 0; i < lists.length; ++i){ alert(lists[i].text()); // Display the values in these li }<script src="https://ajax.googleapis.com/ajax ...

Error 500: Issue with JQuery AJAX File Upload

Hey there, I'm facing an issue with doing a file upload using JQuery's AJAX feature as I keep getting the error 500. $(function() { $( 'form' ).submit ( function() { $.ajax({ type: &a ...

Visualization with D3 - Putting an image at the heart of a circular chart made with SVG

Is it possible to add an image to the center of a donut SVG in D3 charts using JavaScript? I've been attempting to achieve this but haven't had any success so far. Currently, I have inserted the image in HTML, but it does not align with the cent ...

What steps can I take to stop Highcharts from showing decimal intervals between x-axis ticks?

When the chart is displayed, I want to have tick marks only at integer points on the x-axis and not in between. However, no matter what settings I try, I can't seem to get rid of the in-between tick marks. Chart: new Highcharts.chart('<some i ...

Which data structure should I utilize in my Action if the ajax request sends an array?

After experimenting, I found that the List<int> always ends up null in my Action: $('td').delegate('.roleoption select', 'change', function() { var userRoles = new Array(); $(this).parent().parent().find('s ...

What is the best way to renew an access token with axios?

I have been struggling to understand the concept of refreshing tokens after reading numerous articles on the topic. They all seem too convoluted for me to grasp. Could someone please simplify it for me? Here is an overview of what I am trying to achieve: ...

Is it possible for me to automatically send the user's email and username through code without requiring any information from them while using the tawk.to chat widget?

I need assistance with automatically sending the user's email and name when they open a chat window. I have tried various methods to pre-fill the form data but it still appears empty. Please let me know if there is something I am missing. Thank you ta ...

Can the URL be updated in Vue Router without navigating to a new page?

I am working on a Nuxt.js website and trying to implement a "Load more" button on the /catalog page. When clicking on the button, I want to load more products without navigating to a new URL like /catalog/page_2 (?page=2 is not an option). If I use $router ...

Guide to extracting formData request data in Laravel

I am using a fetch api call from my Vue js code to send form-data to the Laravel backend. I have been posting requests successfully, but I need assistance with parsing the data in Laravel. Can you provide guidance on how to achieve this? POST /acapp/pub ...

Explain what mongoose and Schema are at the beginning, just once

Hello, I am currently working on a Node.js application using Express and MongoDB. In order to utilize MongoDB and schema, I have to define Mongoose and schema in all of my routing JavaScript files. However, I would like to only define them once. As I am ne ...

Tips on configuring the Access-Control-Allow-Origin header using XMLHttpRequest

Check out this code snippet to validate geoJSON based on a provided sample: function processSuccess(data){ if(data.status==="ok") console.log("Valid geoJSON has been posted"); else if(data.status==="error") console.log("There was ...

Out of nowhere, jQuery suddenly fails to recognize that my checkbox has been selected

http://pastebin.com/r30Wfvi3 Out of the blue, all that functionality suddenly ceased. var showMyLocation = document.createElement('input'); showMyLocation.type = "checkbox"; showMyLocation.className = "checkboxForRange"; showMyLocation.id = "sh ...

What is the best way to properly deserialize JSON using JSON.NET and C# without encountering any null properties?

While using JSON.NET for deserialization, I am facing an issue where the object is instantiated with all its properties set to null. Here is the JSON data: { "verify-purchase": { "item_name":"Pipeline.NET Task Scheduler", "item_id ...

The integration of ngx-translate with an HTTP interceptor is currently experiencing difficulties

Using ngxtranslate for application translation has been seamless so far. However, I am facing an issue with the HttpInterceptor. This interceptor attempts to retrieve data from local storage at the start and displays a dialog prompting you to either load t ...

What is the process for updating the ID within a Select2 ajax script function?

I am facing an issue with my select2 ajax script. The unique_ID in my table field is named "no", which is causing me to be unable to select an item in Select2 drop down. However, when I changed the name of my database table field from "no_donatur" to "ID", ...

Incorporating external script into React website's functional component

I have invested countless hours on this particular issue, which appears to be quite trivial, yet I am unable to pinpoint where my mistake lies. I recently purchased terms and conditions from Termly.com and now wish to showcase these terms and conditions o ...

Interactive canvas artwork featuring particles

Just came across this interesting demo at . Any ideas on how to draw PNG images on a canvas along with other objects? I know it's not a pressing issue, but I'm curious to learn more. ...

Is there a more straightforward alternative method to retrieve the title attribute from an input within a table cell?

Could you please help me with extracting the title attribute of the input located in the 2nd column (th) of the row with the "row_selected" class from the following table? Here's a link to the table. <table id="tabla_descuentos" class="tablesorter ...