Discover the best method for incorporating Vuetify search functionality within a datatable, especially when the column data is in array

I am dealing with a Vuetify data table component that displays an array of items. Within this array, there is another array that I iterate over using a template tag to display the information in just one column. Take a look at the code snippet below:

<v-text-field
      v-model="searchManagers"
      append-icon="mdi-magnify"
      label="Search"
      single-line
      hide-details
    ></v-text-field>
  </v-card-title>
  <v-data-table
    :headers="headersManagers"
    :items="managers"
    :search="searchManagers"
    class="elevation-1"
  >
   //ATTENTION TO THIS PART HERE
    <template v-slot:item.departments="{item}">
      <p class="mt-2" v-for="(dpt, index) in item.departments" :key="index">{{dpt.name}}</p>
    </template>
    ----------------------------------------
    <template v-slot:item.status="{item}">
      <span v-if="item.status">
        <v-chip label outlined color="success">Active</v-chip>
      </span>
      <span v-else>
        <v-chip label outlined color="error">Inactive</v-chip>
      </span>
    </template>

    <template v-slot:item.action="{ item }">
      <v-btn fab small color="primary" text>
        <v-icon @click="editItem(item)">mdi-pencil-outline</v-icon>
      </v-btn>

      <v-btn fab small color="error" text>
        <v-icon @click="deleteItem(item)">mdi-delete-outline</v-icon>
      </v-btn>
    </template>
    <template v-slot:no-data>
      <v-alert class="mt-4" color="primary" dark :value="true">
        <v-icon x-large>mdi-emoticon-sad-outline</v-icon>
        <br />No users found
      </v-alert>
    </template>
  </v-data-table>

And here are the headers for this table:

headersManagers: [
  { text: "Name", align: "left", value: "fullName" },
  { text: "Username", align: "center", value: "username" },
  { text: "Office", align: "center", value: "office" },
  { text: "Department", align: "center", value: "departments" },
  { text: "Profile", align: "center", value: "profile.name" },
  { text: "Status", align: "center", value: "status" },
  { text: "Actions", align: "center", value: "action", sortable: false }
],

I am looking to make the search functionality work for the array of departments in the departments column as well, just like it works for the name/username/office fields. Currently, when I search for any department, no results are returned.

Here is an example of the full JSON object I am using:

{
"id": "bf6dbc41-4eca-42c4-ab3b-e4ada4aa671b",
"firstName": "name",
"lastName": "last name",
"username": "a.user",
"fullName": "name last name",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07626a666e6b47626a666e6b2964686a">[email protected]</a>",
"photoUrl": "http://some_url.com",
"personalPhone": "9999",
"workPhone": "9999",
"admissionDate": "2012-05-02T00:00:00",
"office": "office",
"jobTitle": "example",
"departments": [
  {
    "departmentId": "04aa5418-b217-4bca-9cf3-f77725508980",
    "name": "department name",
    "director": "director name"
  },
  {
    "departmentId": "12602b8b-68ea-4539-b95e-01968c74247d",
    "name": "other department name",
    "director": "director name"
  }
],
"status": true,
"country": null,
"city": null,
"neighborhood": null,
"addressNumber": 0,
"street": null,
"postalCode": null,
"profile": {
  "id": "35631c5d-3269-4db9-98c8-e9896961e537",
  "name": "example"
},
"createdAt": "2020-07-06T17:44:05.024359",
"isManager": true,
"isDirector": false

How can I adjust the search functionality to work for the departments column as well?

Answer №1

If you're looking to implement a custom filter in your v-data-table component, you can do so by adding a method like this...

 <v-data-table
    :headers="headers"
    :items="items"
    :search="search"
    :custom-filter="customSearch"
    class="elevation-1"
 >
    ...
 </v-data-table> 


 methods: {
      customSearch (value, search, item) {
          if (Array.isArray(value)) {
              return value.some(item=>Object.values(item).some(v=>v&&v.toString().toLowerCase().includes(search)))
          }
          return Object.values(item).some(v=>v&&v.toString().toLowerCase().includes(search))
      }
  },

For a live demonstration, you can check out this link:

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

Adjusting the X-axis in Highstock: Tips and Tricks

Is there a way to adjust the X axis on this chart? My goal is to shift it so that it only covers half of the width, leaving the other half blank for future plotlines. Any suggestions on how to achieve this? Thanks! :) ...

Adjust the height of a div containing variable elements

I'm having trouble adjusting the height of a div so that it can be scrolled all the way through. This div is positioned next to another one, so I set the overflow-y to scroll. Initially, I thought setting the height to auto would solve the issue, but ...

Perform the same actions on every element within the ul li

I'm facing an issue with my unordered list, where each list item contains a span element with an image inside. My goal is to set the background-image of each span to be the same as the image it contains, while also setting the opacity of the image to ...

Trouble experienced with the window.open() function on Safari

When using Safari, it can sometimes block the opening of a new tab through the window.open() function during an ajax call. To bypass this blocking, we must first call window.open() to open a new tab before making the ajax call. Refer to this Stack Overflow ...

`Node.js troubleshooting: Dealing with file path problems`

I am currently in the process of deploying a node.js based application to IBM's Bluemix and have made some modifications to one of their provided samples. I have included an additional javascript file that initiates an ajax call to PHP, but unfortunat ...

The term 'MapEditServiceConfig' is being incorrectly utilized as a value in this context, even though it is meant to refer to a type

Why am I receiving an error for MapEditServiceConfig, where it refers to a type? Also, what does MapEditServiceConfig {} represent as an interface, and what is the significance of these brackets? export interface MapEditServiceConfig extends AppCredenti ...

In JavaScript, generate a new column when the text exceeds the height of a div

Is it possible to create a multicolumn layout in HTML that flows from left to right, rather than top to bottom? I am looking to define the height and width of each text column div, so that when the text overflows the box, a new column is automatically ge ...

When using JSX, it's important to wrap adjacent elements within an enclosing tag to avoid errors. Make sure to properly wrap the JSX tags to

import React, { useState } from 'react'; import ReactDOM from 'react-dom'; function DisplayData(props) { //creating the DataList const dataList = data.map(data => ( <><span>{data.name}</span> nbsp; <span> ...

What steps can I take to prevent Vue from rendering template comments?

Recently, I have been working with a Vue application (version 2.6.0) and started adding comments to the templates using this syntax: <--! This comment describes what this component does --> The problem is that these comments are now appeari ...

Oops! We encountered an issue: Unhandled promise rejection. The function n.swapComponent is not defined

Currently, I am in the process of developing an application with Laravel using Jetsream and the Vuejs & Inertia stack. After executing the npm audit fix command, my entire application is displaying a blank page. The reason behind running the npm command n ...

Having trouble with Isotope masonry functionality

While experimenting with Isotope from , I tested the reLayout method using the provided example found at . I copied the css and js to my page () but encountered an issue where clicking on the first element caused all other elements to move to the first col ...

Having trouble loading SVG component in Next.js with SVGR integration

I recently obtained an SVG react component that was automatically converted from the SVGR playground page. I integrated it into my project and followed the installation instructions for @svgr/webpack and configured the setup as advised. However, upon loadi ...

Is it possible to capture a Browser TAB click event using JavaScript or AngularJS?

How can I trigger an event when returning to a tab? For instance: Let's say I have opened four tabs in the same browser with the same URL: such as: http://127.0.0.1:/blabla http://127.0.0.1:/blabla http://127.0.0.1:/blabla http://127.0.0.1:/blabla ...

Resolved plugin issue through CSS adjustments

Take a look at this template 1) After referring to the above template, I developed a fixed plugin using JavaScript. 2) Clicking the icon will trigger the opening of a card. 3) Within the card, I designed a form using mdb bootstrap. Everything seems to ...

NodeJS with Gulp Streams and Vinyl File Objects: Troubleshooting Incorrect Output Issue in NPM Package Wrapper

Mission My current project involves creating a Gulp wrapper for NPM Flat that can be seamlessly integrated into Gulp tasks. This initiative aims to benefit the Node community while also helping me achieve my objectives. You can access the repository here ...

How can we effectively implement alert notifications for validating image sizes and formats prior to uploading?

code playground : https://codesandbox.io/s/quizzical-lamport-ql5ep I'm encountering an issue with the code provided in the CodeSandbox link. https://i.sstatic.net/xg3aK.png I've attempted to resolve this issue using various methods, but unfortu ...

Having trouble with Next.js and Next-auth? When I make an HTTP request in getServerSideProps, getSession is returning null in my secured API Route

I am currently working on securing an API Route that is being called from both the Client and Server-side on different pages. When accessing the test page, it returns a 401 error. However, when accessing the test2 page, the content is retrieved successfu ...

Is it possible to integrate Vue.js with Laravel and bootstrap in a web development project?

In order to build a basic website equipped with a forum feature, my plan involves integrating Vue JS with Laravel and Bootstrap. I am confident in the necessity of Vue JS for this project, but I am open to alternative options for Laravel and Bootstrap. I ...

What is the best way to retrieve a particular field from a Firestore Document using JavaScript?

Within my Firestore database, I have a structure of users that looks like this: https://i.sstatic.net/jgeCq.png The rules set up for this database are as follows: match /users/{userID} { match /public { allow read: if request.auth != nu ...

The search function in the Vuetify datatable is unable to find the specified value

I came across the miss function on the Vuetify datatable, and I believe my configuration is correct. Below is my Vuetify datatable: <v-text-field v-model="search" append-icon="mdi-magnify" label="Search" single-line hide-d ...