Blending a series of filter lists together

I am trying to combine the firstname and lastname as a single filter input. Currently, I have 4 filters that work fine individually. How can I create a single input for both first name and last name so that when a user types a name, it will search for matches in both fields? Right now, I have separate inputs for first name and last name.

<template>
<div>
  <input type="text" class="form-control" placeholder="Filter by Full Name" v-model="search_full_name">
  <input type="text" class="form-control" placeholder="Filter by Employee Number" v-model="search_empNumber">
  <input type="text" class="form-control" placeholder="Filter by Department" v-model="search_department">
</div>
<table class="table table-hover rounded-lg tbl-responsive">
    <thead>
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Employee Number</th>
            <th>Contact</th>
            <th>Department</th>
            <th>Position</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr v-for="(employee,index) in filteredEmployee" :key="employee.id">
            <td>{{employee.id}}</td>
            <td>{{employee.firstname}}</td>
            <td>{{employee.lastname}}</td>
            <td>{{employee.employee_number}}</td>
            <td>{{employee.contact}}</td>
            <td>{{employee.department}}</td>
            <td>{{employee.position}}</td>
            <td>{{employee.basic_salary}}</td>
        </tr>
    </tbody>
</table>
</template>
<script>
export default {
    computed: {
        filteredEmployee: function(){
            // Combines all the filters
            return this.filterByDepartment(this.filterByEmpNumber(this.filterByFullName(this.employeeList)))
        }
    },
    methods:{
        filterByDepartment: function(employees){
            return employees.filter(employee => !employee.department.indexOf(this.search_department))
        },
        filterByFullName: function(employees){
            let searchName = this.search_full_name.toLowerCase();
            return employees.filter(employee => 
              (employee.firstname.toLowerCase().includes(searchName) || employee.lastname.toLowerCase().includes(searchName)));
        },
        filterByEmpNumber: function(employees){
            return employees.filter(employee => !employee.employee_number.indexOf(this.search_empNumber))
        },
    }
}

Answer №1

Combine first and last name into a single input field,

<input type="text" class="form-control" placeholder="Search by Name" v-model="search_by_name">

then create a method to search using both fields.

searchByName: function(employees){
    return employees.filter(employee => !employee.firstname.indexOf(this.search_by_name) || !employee.lastname.indexOf(this.search_by_name))
}

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

Using jQuery to trigger a click event on a radio button prevents the button from being checked

Having trouble with using the .trigger("click"); function on radio buttons to activate a button in a form. When I use the code below, the button is triggered as expected. However, it seems to be interfering with the radio button, causing it to appear chec ...

Guide on using Fetch API to send a request to a PHP file using the POST method

Hey everyone, I've recently started delving into the world of PHP and I encountered an issue while attempting to send a post request from my JavaScript file to my PHP file using the Fetch API. Upon making the request in my console, I received the foll ...

Issues with HTTPS in Puppeteer and VUE JS

I am encountering an issue when running tests from NODE net::ERR_CONNECTION_REFUSED at https://localhost:8087 at navigate (node_modules/puppeteer/src/common/FrameManager.ts:190:13) Test Suites: 2 failed, 2 total Tests: 7 failed, 7 total Snapshots: ...

The extent of a nameless function when used as a parameter

I am currently developing a straightforward application. Whenever a user hovers over an item in the list (li), the text color changes to green, and reverts back to black when the mouse moves away. Is it possible to replace lis[i] with this keyword in the ...

Problem with Bcryptjs Async Implementation

I have implemented a function in my node server using bcryptjs to hash and compare passwords. Here is the code snippet: this.comparePasswords = function(password1, password2, callback) { bcrypt.compare(password1, password2, function(error, result) { ...

If I use npm install to update my packages, could that cause any conflicts with the code on the remote server?

As I navigate through the numerous issues, I stumbled upon the command npm ci that is supposed to not change the package-lock.json file. However, when I attempt to run npm ci, it fails: ERR! cipm can only install packages when your package.json and package ...

`Troubleshooting a rotation problem with PhysiJS`

I've recently started working with this library and I've run into a persistent issue that has been quite challenging for me. My current setup involves two cubes, one utilizing physi.js and the other using three.js. I have a function in place to ...

Ensure that this regular expression is able to accurately match all numbers, even those without decimal points

Seeking help to create a script that can extract the negative percentage value between two numbers. One of the numbers is dynamic and includes different currencies, decimals, etc., so I believe a regex is needed for this task. The current script works, but ...

Troubleshooting: Passing data from child to parent component in Vue causing event malfunction

This is a custom component created by Jobs that triggers an event to its parent component. <div class="card py-1 my-1" @click="$emit('active-job', job.id, job.slug)"></div> The parent component is set up to listen ...

Steps to verify the current time and execute a task if it matches the designated time

After successfully implementing a time function which changes the value of an input text based on a specific time, I encountered an issue. Although the time function is designed to change the input text value to 1 when the time reaches 2:30:35 PM... if(b ...

fetching indexeddb information using the equivalent of a "WHERE IN (a,b)" query

I've been working on transitioning from websql to indexeddb, but I'm struggling to recreate the SELECT query: "SELECT * FROM tableA WHERE cid ='"+cid+"' AND hid IN("+hid+",1) ORDER BY hid DESC LIMIT 1"; function getMyData(e) { var ...

BlendModeGlobal operations

Experimenting with the globalCompositeOperation property in a loop by passing different strings (source-atop, source-over, etc.) to the same 2D context, I observed that Firefox only allowed me to draw a few shapes while Opera only displayed the last one. ...

Guide to changing CSS style dynamically using AngularJS

<div style="width: 50px !important"> I am looking for a way to dynamically set the pixel number using angularjs. The final width should be calculated based on a base pixel width as well. How can I make this happen? <div ng-style="{{width: (model ...

Video demo: Issue with Bootstrap navigation bar expansion being truncated

When developing my React app, I installed the latest Bootstrap using npm by running: npm install bootstrap followed by: npm install bootstrap jquery and then: npm install jquery popper.js Initially, my Navbar functioned perfectly when the window was wid ...

How to dynamically generate Angular component selectors with variables or loops?

Looking to dynamically generate the Selector Tag in my app.component.html using a variable. Let's say the variable name is: componentVar:string What I want in my app.component.html: <componentVar></componentVar> or <app-componentVar& ...

Displaying items using a filter function in ng-repeat

I am facing an issue with using a filter on my ng-repeat that involves a function with a parameter passed in, but for some reason, the filter is not working as expected. The function I am using for the filter compares two arrays to find any matching eleme ...

Click function unresponsive following the completion of an Ajax call

I'm having an issue with my WordPress site where the JavaScript code I am using for an Ajax filter is not functioning properly once the filter is activated. Specifically, the click event is not working as expected. function addStoreLinkListeners() { ...

AngularJS - Calculate multiple currencies

I need to calculate the product of a value and months. For the input value, I am utilizing a Jquery Plugin to apply a currency mask. Unfortunately, the calculations are not functioning properly with this plugin. My goal is to multiply the value, includin ...

Telegram Bot does not have the ability to be constructed in TypeScript

Why am I encountering this error message: TypeError: node_telegram_bot_api_1.default is not a constructor This is the TypeScript code snippet I have written: import * as dotenv from 'dotenv'; dotenv.config({ path: __dirname + '/.env&ap ...

Enhancing the angular-ui-bootstrap typeahead module

Currently, I am using the typeahead feature from angular-ui-bootstrap to allow users to select a person's name or add a new name if it is not available in the options. I have customized the getMatchesAsync function with the following code: if(scope ...