Utilizing computed properties to implement table filtering in Vue.js

I am currently working on a table generated from an array of objects in VuE.js and struggling with implementing computed properties for filtering. I need help figuring out the correct way to use filter() within my computed properties to filter the table based on user input.

new Vue({
  el:"#app",
  data: () => ({
    search:'',
    programs: [],
    editableKeys: ['date', 'company', 'funding', 'funded', 'recruit', 'program'],
  }),
  created () {
    this.getPrograms();
  },
  methods: {
    // Existing methods here
  },
  computed: {
    searchContents(){
      // Filter function implementation needed here
    }
  }
})

You can find the code snippet here

Answer №1

When working with computed properties, it is important to remember that they need to return a value just like regular data and props. In the case of filtering results, you simply need to return the filtered result. If there is no specific search criteria specified, you can simply return the raw data without any filters applied.

For example, if you are filtering programs based on their funding attribute, your computed property would look something like this:

computed: {
  searchContents(){
    if (this.search === '') {
      return this.programs
    }

    return this.programs.filter(x => this.search === x.funding)
  }
}

You can then use this computed property within a v-for loop:

<tr v-for="(program, index) in searchContents">

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

Customize image position based on div width using jQuery and CSS

I am dealing with dynamic input sourced from a JSON feed that populates a div with different names of varying lengths. Within this setup, there is an image positioned absolutely, and I need to adjust the CSS left value of the image dynamically based on th ...

`Gradient blending in ChartJS`

Currently, I am facing an issue with my line chart having 2 datasets filled with gradients that overlap, causing a significant color change in the 'bottom' dataset. Check out my Codepen for reference: https://codepen.io/SimeriaIonut/pen/ydjdLz ...

Breaking down a URL based on one of two distinct components

Currently, I have a piece of code that splits the URL and removes everything after &7. Is there a way to modify it so that it also checks for |relevance simultaneously and splits based on whichever one is found? $(document).ready(($) => { const pa ...

Error encountered when attempting to have multiple chrome.runtime.onMessage listeners - port disconnection issue

I am currently developing a chrome-extension that utilizes the Dexie indexedDB Wrapper, various jQuery Libraries, and syncfusion's eGrid to manage and display data. While I know this issue has been addressed multiple times in the past, I have encounte ...

Is there a way to utilize variables from a source XML file to establish points on an SVG polygon?

I've been struggling to figure out if it's possible to dynamically set points on an SVG polygon using variables that are defined by an XML document which is constantly changing. All I want is to set the path like this: var polygonToUse = window. ...

Using the toggle method or IF statements in JavaScript to implement show and hide functionality upon clicking

I’ve been struggling with this issue for days now. It seems like I may have overcomplicated a simple piece of code. I'm trying to show some divs when a button is clicked, but I’m having trouble getting it to work properly. Initially, I used VAR.st ...

What is the best way to spin an element around its center?

I'm faced with a challenge where I have an element that needs to be rotated using JavaScript. The rotation is currently functional, but it's rotating around points other than its center. My goal is to rotate the element around its own center. ...

Compel instantaneous XMLHttpRequest / Ajax without any prior notification

It's no secret that synchronous XMLHttpRequest has been deprecated due to its negative impact on user experience. The deprecation of Synchronous XMLHttpRequest on the main thread is a result of its adverse effects on end users' experience. For ...

Error: The ng-click directive is encountering a parsing syntax error. The token 'Object' is unexpected and is causing the error, it is expected to be enclosed in

When a user clicks on a point on a Google map, I am conducting reverse geocoding in the following manner: geocoder.geocode({'location': latlng}, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { ...

Eliminate event listener using unique identifier

Is there a way to retrieve information about all event handlers for an element in JavaScript? $._data($('#element_id')[0], "events"); This will provide a detailed record of every event handler attached to the element. 0: {type: "c ...

Dynamic Code for Removing List Items Based on Date

I need assistance in resolving an issue with my company's website design and function. Specifically, I am working on a page that displays a list of events where employees will be present throughout the year. Here is an example: <div class="contai ...

h1 tag set for jQuery AJAX activation

Currently working on a website that heavily relies on ajax. Encountering an issue that hasn't been solved through online resources. Now, I'm sharing my function for fetching a page: function loadTemplate(name, replaceWholePage = true){ $.wh ...

Launching a new tab with a specific URL using React

I'm attempting to create a function that opens a new tab with the URL stored in item.url. The issue is, the item.url property is provided by the client, not by me. Therefore, I can't guarantee whether it begins with https:// or http://. For insta ...

Convert object to JSON format using AJAX request to a PHP file

Despite receiving a 200 green response, my data is still not getting written to the json file and it remains blank. The JavaScript: $(function() { $('form#saveTemp').submit(function() { let savdAta = JSON.stringify($('form#save ...

Is it possible to add a new entry to a SharePoint list using REST API without statically specifying the field names?

I am currently in the process of creating a new list item in a SharePoint list using the REST API. To make the process more flexible, I am storing the field names in an array that will be dynamically updated by another function. However, I have encountered ...

Enhancing Pinia setup stores with custom getters, setters, and actions for improved reusability

If we think about a Pinia setup store that has a basic set of state, getters, and actions in place: export const useBaseStore = defineStore('base-store', () => { const name = ref<string>(''); const age = ref<number>(1 ...

Jquery failing to trigger click() on a div with an id

Within my erb file, there exists a script that looks like the following: function checkJquery() { if(window.jQuery) { jQuery(document).ready(function() { alert('onready'); $('div#habla_topbar_div').click(function( ...

Replacing an array element in React.js

Here is a React.js code snippet where I am trying to utilize an array called distances from the file Constants.js in my Main.js file. Specifically, I want to replace the APT column in the sampleData with a suitable value from the array distances extracted ...

Ways to combine extensive value within an array containing various objects

I am working with an array of objects and I need to merge all the values of params within each object into a single object. Array [ Object { "key": "This week", "params": Object { "thisWeekfilterDistance": [Function anonymous], "this ...

I am in need of some guidance - where can I locate the documentation for vue cli

Searching for specific build modes tailored to my Vue CLI 2 application has proven to be a challenge. Despite consulting various resources, all the information I come across pertains to Vue CLI 3. After attempting to locate documentation for Vue CLI 2, u ...