Filtering array data in VueJS when utilizing a dropdown select option

Within my vue application, I am managing an array of job postings that have various states such as "active," "rejected," "draft," and "not_active." The interface includes a TabMenu with options for All Jobs, Drafts, and To Be Approved. Each tab has its dropdown menu for filtering the job postings by status. However, I've encountered complexities in implementing this feature – particularly when attempting to display all job postings under each specific tab. For instance, clicking on the "To Be Approved" tab should show job postings with statuses "Not approved" and "Rejected" (refer to the data below).

My question pertains to finding a proper solution to this issue. Should the job posting data object also include a category for better organization?

Any assistance or guidance on this matter would be greatly appreciated!

Below is the code snippet from my component:

<template>
        <div>
            ... // Component template content
        </div>
    </template>

    import DataTable from '../../snippets/DataTable';

    export default {
        components: { DataTable },
        data() {
            return {
                ... // Component data properties
            };
        },
        computed: {
            filteredData() {
                ... // Computed property for filtering data
            }
        },
        methods: {
            ... // Component methods for toggling tabs and dropdowns
        },
        created() {},
        mounted() {
            ... // Initialization logic for default tab selection
        }
    };
    

Answer №1

I'm not entirely sure if this solution will be beneficial to you, but I'll give it a shot anyway.

One approach is to save the selected tab upon clicking on it. After that, filter the this.tableData based on the chosen tab's options. It's also essential to map the tab option options to an array of strings so that you can easily check if the posting status matches any of them.

methods: {
  toggleList (tab, index) {
    this.selectedTabObject = tab
    // continue with your code...
  }
},
computed: {
  filteredData () {
    return this.tableData.filter(data => {
      const states = this.selectedTabObject.options.map(opt => opt.status)
      return states.includes(data.status)
    })
  }
}

Additionally, I've set up a simple example on fiddle to replicate your issue. https://jsfiddle.net/3hqnp7u2/7/

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

Is there a way to modify the image source upon clicking a button?

I am currently working on implementing an image swap functionality on a webpage. I need the image to change when a user clicks a button, but for some reason, my logic isn't working. I am more interested in understanding why it's not working and w ...

Create a time of 00:19:59 using JavaScript

I am attempting to display a countdown timer that starts at 20 minutes in the format (00:20:00) using setInterval. Once the countdown is finished, it should display as (00:00:00), but I am having trouble achieving this. <body onload = "onloadFunc();" ...

Tips for repeatedly clicking a button over 50 times using Protractor

Is it possible to click the same button more than 50 times using a loop statement in Protractor? And will Protractor allow this action? Below is my locator : var nudge= element(by.xpath("//a[@class='isd-flat-icons fi-down']")); nudge.click(); ...

Deciphering JSON Objects with JavaScript

I am using PHP on my server to handle the AJAX call and return a JSON object. Here's how it looks: $dataArray = array('order_id'=>$order_id, 'response'=>'Sucessfully Added'); header('Content-type: application/ ...

What is the reason for the jQuery callBack handler returning [object Object]?

Recently, I created a SessionMgr.cfc file in ColdFusion to manage session variables for my $.ajax calls. However, it seems like I might have made a mistake somewhere. Despite scouring through numerous pages on Stack Overflow and Google, I still can't ...

The string in the text remains unchanged

Currently, I am attempting to replace specific strings as text is entered into the input field. The strings in question are {b} and {\b}. However, I have noticed that not all instances of these strings are being replaced, leaving some behind. Below is ...

Utilize only certain JSON properties within JavaScript

I have access to an array of JSON objects. [ { Id: "1", StartNo: "1", ChipNo: "0", CategoryId: "0", Wave: "0", Club: "", FirstName: "Lotta", LastName: "Svenström", FullName: "Lotta Svenström", ZipCode: "24231" }, {...} ] My goal is to create a new data ...

What is the best way to transfer a data variable between two different views?

My goal is to transfer the variable localUser from the initial Login view to the Home route using router.push("Home");, and then retrieve the value of localUser in the Home view and its components. Attempts Made: I have experimented with various solutions ...

Content does not become interactive upon the initial loading of the page

I've modified a W3 schools slideshow template to use text instead of dots, but I'm encountering two issues 1: The first image doesn't load when the page loads. Although using a class ID and setting it to active fixes this issue, it leads to ...

Leveraging the (click) event within an HTML template to manage a [hidden] element located in a different template using Angular 2

Within my project, I have two TypeScript files, each containing HTML templates inside the @Component. In one of the templates, there are info cards that can be collapsed or expanded using [hidden]="collapsed". The following function is present in both file ...

Is there a polyfill for dragging and dropping?

Currently searching for a solution that supports native HTML5 drag-n-drop functionality while also providing compatibility for older browsers. Is there a tool out there that fits this criteria? Unfortunately, Modernizr's page (https://github.com/Mode ...

Using Vue.js to bind an array and data object to an img srcset

I am in need of a solution to add a host prefix and responsive sizing options to a list of image names stored in an array. Currently, I have the following setup: new Vue({ el: '#gallery_images', data: { desktop: 'w=400&h=400&am ...

Saving the subtracted value from a span into a cookie until the checkbox is unchecked - here's how to

I am working on a piece of code that includes numeric values within a span. When the checkbox is clicked, it subtracts 1 from the main value, essentially reducing the total value. How can I achieve this so that even after the form is submitted, the deducte ...

unable to retrieve the data from the secondary dynamic cascading dropdown menu

Hopefully, my day's troubles will be resolved by now. I am facing an issue with two drop-down boxes. After submitting, only the value of the first dropdown box is available in the action page. The second dropdown box is dependent on the selection made ...

Is it possible to send an array as a parameter from one function to another in C++?

I am struggling with passing an array from one function to another in my program. Specifically, I need to pass the k[arrsize] array, which is a parameter of the fillArray() function, to the create_file() function while still keeping the data intact (the ...

Feeling lost on how to utilize .map, .reduce, or foreach in my code

Recently delving into JavaScript, I find myself a bit perplexed despite scouring through various answers and resources on Mozilla.org. My struggle lies in seamlessly using .map and .filter on straightforward arrays, but feeling a bit lost when it comes to ...

How to transfer data between two components in a React application

In one of my components, I have a prop that I want to pass to another component and use as a variable there: const VerContinentToolbar = (props) => { return ( <Menu className="nav-icon"> <CountryList displayFields={["countryName ...

Unexpected syntax error occurs while retrieving data from web API using jQuery AJAX request

I'm attempting to retrieve a json object from the following URL: You may not understand much as it's in Greek, but the format is json. Below is the code snippet I'm using: function getDicts() { api_url = 'https://test3.diavgeia.gov ...

Retrieve the chosen option value from the foundation dropdown menu

I have been exploring the functionality of Foundation drop-downs. To see it in action, visit this link: Below is the code I used to create a dropdown: <a href="#" data-dropdown="drop1" >Date Range </a> <ul id="drop1" class="f-dropdow ...

Error message: "AJAX preflight request access control check failed"

Having an issue with sending a POST request to the SendGrid email API. Whenever I try to submit the form for the POST request, I encounter this error in the console: Failed to load https://api.sendgrid.com/v3/mail/send: Response to preflight request doe ...