Refining a selection from a list using a multi-choice array

I have a filtering component that filters a list of people based on multiple input values. The string-based inputs filter properly, but when I select more than one item in the multi-select, nothing is displayed. This is likely due to person.role not containing both values in the array.

<!-- rest of form -->
<v-select
  outlined
  multiple
  v-model="filter.roleFilter"
  :items="['Designer', 'Developer']"
/>
<!-- rest of form -->

The filtering logic

 computed: {
   filteredPeople() {
      const searchVal = this.search;
      const type = this.filter.accountypeFilter;
      const role = this.filter.roleFilter; 
      const start = this.filter.dateStartFilter;
      const end = this.filter.dateEndFilter;

      if (type === '' && role === '' && start === '' && end === '' && searchVal === '') {
        return this.people; // show everything if no filters are selected
      }
      // apply the filter function and return the filtered array
      return this.people.filter((person) => (type === '' || person.accountType === type)
        && (role === '' || person.role.includes(role)) // <--- issue lies with this line
        && (start === '' || person.dateStart === start)
        && (end === '' || person.dateEnd === end)
        && (searchVal === '' || person.displayName.toLowerCase().includes(searchVal.toLowerCase())));
    },
  }

Is there a way to use something like

role.every(person.role.includes(role))
to make this functionality work?

Answer №1

If you want to implement a solution that caters to individuals with a single role, consider the following approach:

computed: {
   filteredPeople() {
      const searchVal = this.search;
      const type = this.filter.accountypeFilter;

      // Multiple roles scenario
      const role = this.filter.roleFilter;

      const start = this.filter.dateStartFilter;
      const end = this.filter.dateEndFilter;

      if (type === '' && role === '' && start === '' && end === '' && searchVal === '') {
        return this.people; // Return all entries when no filters are applied
      }

      // Apply filters and return updated array
      return this.people.filter((person) => (type === '' || person.accountType === type)
        && (role.length === 0 || role.includes(person.role))
        && (start === '' || person.dateStart === start)
        && (end === '' || person.dateEnd === end)
        && (searchVal === '' || person.displayName.toLowerCase().includes(searchVal.toLowerCase())));
    },
  }

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

Which SSO framework works best for integrating Facebook and Twitter logins?

I own a website and I'm looking for a way to let users leave reviews and rate products. I need an SSO system that allows them to sign in with their Facebook or Twitter accounts and ensures each user is uniquely identified so they can't rate produ ...

What is the process for resolving the file paths of custom modules in node.js, whether they are absolute or relative?

require doesn't seem to recognize any path other than './parser1'. Despite placing both js files on the Desktop, attempting to run node my_parser.js always results in an error stating "Cannot find module" for relative paths like require(&apo ...

Using Unicode JSON in Laravel blade to pass data to React components, encountering an issue with JSON parsing

I currently have a JSON object stored in the database: { "ui": {}, "title": "Hola mundo 2", "values": {}, "properties": {}, "description": "descripcion" } Within the Laravel controller, ...

Issue with iview-ui: Unable to make admin panel full height. The iView example suggests using min-height: 200px

Is there a reason why the iView admin template can only scale to a preset height? Can someone assist me in setting the height of the admin to fill the entire browser (100vh or 100%)? Many thanks in advance. The example on the site displays a predefined m ...

What is the correct method to choose an element in jQuery based on the function's id parameter

I'm having trouble deleting an item in my to-do list app. deleteToDoItem: function(id) { $(id).remove(); console.log(id); }, Here is the function that calls deleteToDoItem: function deleteItem(event) { var itemID, splitID, t ...

Stop the background from scrolling and prevent auto-jumping to the top on mobile devices

When users click on the hamburger icon in the top right of our mobile site, I want the drop-down menu to appear and be scrollable without the background scrolling. I tried using JavaScript to set the body to fixed when the menu icon is clicked, but this ca ...

Command for Sniping with Discord.js

I am currently working on creating a snipe command using Discord.js in my bot. I have set up command handlers and everything seems to be working fine, including the on messageDelete event. However, I encounter an error when I delete a user message and try ...

Tips for utilizing the getJson method to pass a variable to a PHP file

I need help with my JavaScript code, as I am trying to pass a datastring containing the value "no" to data.php using getJson in order to receive JSON as a response. However, my JavaScript code is not functioning correctly. Below is the code that I have: J ...

I'm facing issues with Angular commands not functioning properly even after installing the Angular CLI and configuring the

Every time I attempt to create a new project using Angular CLI by typing: ng n app I encounter the following error message: C:\Users\Venkateshwarn M\AppData\Roaming\npm\node_modules\@angular\cli\bin\ng: ...

Cannot use Axios instance in Next.js due to error stating 'Localstorage is not defined'

I am currently working on a React app and have created an Axios component that I would like to reuse: import axios from 'axios' import dynamic from 'next/dynamic' const baseUrl = 'http://127.0.0.1:8000/' const axiosInstan ...

Exploring how to iterate through multiple arrays simultaneously in JavaScript

I have a specific problem with processing two sets of array objects to achieve a desired output. var parts={"Glenn": [12,22,32], "Ryan": [13,23,33], "Steve K": [14], "Jerom":[15,25,35], }; var labor={ "Glenn": [12,22,32], "Ryan": [13,23,33], "Steve K": [ ...

Encountering an issue when using both the Google Maps API and Google URL Shortener API within the same program

Recently, I developed a program that involves passing data to an iframe through a URL. However, due to the limitation of Internet Explorer supporting only 2083 characters in a URL, I decided to use the Google URL Shorten API to shorten the URL before sendi ...

Implementing multiple components in a Vue route: A comprehensive guide

As a beginner in Vue, I am experimenting with incorporating two components in a route - a navigation bar and some sales data. The assets are being bundled by Laravel mix using Webpack, but I keep encountering failures with npm. index.php <body> ...

Creating a Dynamic Clear Button for a Text Area in Angular

Working on my Angular application, I have implemented a form with a textarea element. My goal is to incorporate a clear button inside the textarea element that should: Appear only when the textarea is focused Disappear when the textarea is out of focus ( ...

Personalized configurations from the environment in the config.json file

I need to dynamically populate a setting object in my config.json file based on environment variables. The settings should vary depending on the environment. "somesetting": { "setting1": "%S1%", "setting2": "%S2%" } I am currently working on Wind ...

Is it necessary to implement the useCallback hook along with React.memo for rendering optimization in React components?

As I delved into understanding how useCallback and useMemo function in React to optimize app performance, I decided to create a simple app for experimentation. What I observed was that the callback function (OnBlurHandler) passed to my child component trig ...

How can I submit a form or retrieve HTML content using JavaScript without using an iframe?

Background: My current job involves transcribing paper reports using a webapp that is quite old and cannot be updated or connected to a database directly. The system only checks for duplicate unique IDs once the entire form is submitted. This often leads ...

Convert JSON objects within an array into HTML format

Is there a way to reformat an array of JSON objects that has the following structure? [{"amount":3,"name":"Coca-Cola"},{"amount":3,"name":"Rib Eye"}] The desired output in plain HTML text would be: 3 - Coca-Cola 3 - Rib Eye What is the best approach to ...

Enter a socket.IO chat room upon accessing an Express route

Encountering difficulty when attempting to connect to a socket.IO room while accessing a specific route in my Express application. The current setup is as follows: app.js var express = require('express'); var app = express(); var http = requir ...

How can I position 7 images absolutely within a specific div?

I've been working on a website where users can have their avatars displayed using a JS function that loads 7 different images onto the page. These images correspond to different elements such as skin base, hair, eyes, mouth, shirt, shoes, and pants, a ...