Why won't the $emit from custom filter reach the callback in vue-tables-2?

I'm facing a challenge with implementing a custom filter in vue-tables-2 that should emit an event from a nested, single-page component in Vue. It seems like the issue might be related to not handling it correctly in the parent components.

Inside the custom template for dataTable, I have a custom filter called <filter-brand /> which triggers the event

Event.$emit("vue-tables.filter::filterByBrand", this.brand)
.

The goal is to capture the 'filterByBrand' event in a top-level router component named <Grid />, where I have the <v-client-table /> along with the relevant options including the customFilters.

Any insights on where things might have gone wrong?

Grid.vue

...
customFilters: [
  {
    name: "filterByBrand",
    callback: function(row, query) {
      console.log("filter=", query); // nothing?
      return row.name[0] === query;
    },
  },
],
...

main.js

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import { ClientTable, Event } from "vue-tables-2";
import "./scss/index.scss";

Vue.use(ClientTable, {}, false, "bootstrap4", {
  filtersRow: FiltersRow,
  genericFilter: FilterKeyword,
  sortControl: SortControl,
  tableHeading: TableHeading,
  dataTable: DataTable, // where my custom filter <filter-brand /> resides
});

Vue.use(Event);

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount("#app");

FilterBrand.vue

<template>
  <div class="form-group position-relative">
    <label for="brandFilter">
      Brand:
    </label>
    <select
      name="brandFilter"
      id="brandFilter"
      class="form-control select"
      @change="handleChange($event)"
      v-model="brand"
    >
      <option disabled selected value="">Choose</option>
      <option value="All">All</option>
      <option value="Brand 1">Brand 1</option>
      <option value="Brand 2">Brand 2</option>
    </select>
  </div>
</template>
<script>
import { Event } from "vue-tables-2"; // importing here to avoid 'window' event

export default {
  name: "FilterBrand",
  props: ["props"],
  data() {
    return {
      brand: "",
    };
  },
  methods: {
    handleChange(event) {
      this.brand = event.target.value;
      Event.$emit("vue-tables.filter::filterByBrand", this.brand); // where does this go?? :)
    },
  },
};
</script>

Answer №1

My mistake stemmed from placing the customFilters block outside of the options: {} block in my main Grid.vue file.

Originally it looked like this:

data() {
  return {
    brandEventCount: 0,
    columns: [],
    eventsData: [],
    options: {
      columnsDropdown: true,
      perPage: 25,
      debounce: 500,
    },
    customFilters: [
      {
        name: "filterByBrand",
        callback: function (row, query) {
          return row.brand === query;
        },
      },
    ],
  };
},

The correct structure should have been as follows:

data() {
  return {
    brandEventCount: 0,
    columns: [],
    eventsData: [],
    options: {
      columnsDropdown: true,
      perPage: 25,
      debounce: 500,
      customFilters: [
        {
          name: "filterByBrand",
          callback: function (row, query) {
            return row.brand === query;
          },
        },
      ],
    },
  };
},

Answer №2

import {ClientTable, Event} from 'vue-tables-2';
Vue.use(ClientTable, {}, false, 'bootstrap4');
window.vtEvent = Event; //changing event name for vue-table-2

to use on a component, do the following:

vtEvent.$emit('vue-tables.filter::filterprodusen', value);

I trust this information will be beneficial.

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

Comparison of Web Development Techniques: localStorage versus Cached HTTP

Imagine a scenario where there is a web server responding to a GET request by sending a .json file. The response instructs the browser to cache it for a period of 5 years. Furthermore, picture a webpage that initiates this GET request for the JSON data du ...

What is the best way to establish a default translation setting in i18n for Nuxt.js?

In my nuxt.config.js file, I had set the default routing like this : https://i.sstatic.net/PSuXV.png However, when I access localhost, it redirects to: http://localhost:3000/en. Shouldn't it redirect to http://localhost:3000/ instead? What can be d ...

What is the process for activating a button after a checkbox has been selected? Additionally, how can a value be dynamically transferred from a checkbox to the rezBlock_1 block?

What could be the reason for my code not functioning properly? How can I enable a button once a checkbox is selected? Also, is there a way to dynamically move text from a checkbox to the rezBlock_1 block? CODPEN https://codepen.io/RJDio/pen/RwPgaaZ doc ...

Saving numerous files with Promises

There is a Node URL (created using Express) that enables users to download static images of addresses. The calling application sends a request to the /download URL with multiple addresses in JSON format. The download service then calls Google Maps to save ...

Issue encountered while incorporating a PHP file into Javascript code

I'm facing a particular issue where I have a PHP file that is supposed to provide me with a JSON object for display in my HTML file. Everything seems to be working fine as I am receiving an output that resembles a JSON object. Here's the PHP file ...

What is the best way to add multiple rows using a parameter in SQL?

My goal is to insert multiple rows in SQLite using the ionic framework. Inserting a single row works fine, as does running the following query: INSERT INTO categories (category_id, category_name, category_type) VALUES (1,"test",1),(2,"test again", 2); ...

What is the process for updating the list to retrieve fresh data from the service?

I am currently in the process of calling a web service to retrieve data and display it in a list using directives. Upon loading my fiddle, I successfully load data from a JSON file. The data is displayed correctly in the list. If I click the delete butto ...

What is the most efficient way to retrieve the key at a specific index within a JavaScript map object?

If I have the map object shown below: const items = new Map([['item1','A'], ['item2','B'], ['item3', 'C']]) I am trying to retrieve the key at index 2. Is there a method other than using a for ...

Tips for choosing a week on a calendar with JavaScript

Here is the code for my calendar: <script> $(document).ready(function() { var events = <?php echo json_encode($data) ?>; $('#calendar').fullCalendar({ header: { left: 'prev,next', center: &apos ...

What is the best way to integrate various JQuery and DataTables functionalities and organize them in a specific order

I managed to implement some features into jQuery datatables by following tutorials. However, I am struggling to combine these features and make them work together seamlessly. If you search for these features online, you will likely find where they originat ...

unleashing the magic of AJAX: a guide to extracting

In my Symfony project, I am attempting to retrieve the content of an AJAX request in order to check the data using dump(). The purpose is to process this data and perform a SQL query. However, when I use dump() in my controller, there doesn't appear t ...

Best method for reusing a component in React?

Here is a simplified code featuring a reusable button component: import React from 'react' import styles from './Button.module.scss' const Button = (props) => { return ( <button type={props.type} className={styles.btn} onC ...

Detect mouse events using electron even when the window is not in focus

Is there a way to detect mouse events such as hover and click even when the Electron window is not the active focus? I want to ensure that my buttons' hover and click effects still function properly. Currently, I find that I have to switch back to th ...

How to deactivate choices in v-autocomplete

Is there a way to deactivate certain options in v-autocomplete? I tried using the item-disabled attribute and passing the string value of the option, but it didn't work as expected. <v-autocomplete :items="states" item-text="name" labe ...

It never fails to function automatically, no matter which script is being executed

By default, the script will always be executed regardless of its environment. Check out my code snippet: import { Pool } from 'pg'; import config from './../config'; const connectionString = () => { switch (process.env.NODE_EN ...

Trouble with CSS loading in Node.js with express.static

Currently, I am working on a web project using Node.js and Express. However, I am encountering an issue with my CSS not loading correctly when serving the static files. Despite trying to use express.static, it doesn't seem to be functioning as expecte ...

Guide on transmitting Cookie from Server to User's Browser

I'm currently exploring the functionality of cookies and how they work. I'm attempting to send a cookie from the server and set it in the user's browser. However, when I check within the developer tools--> application-->cookie, I can&a ...

Utilizing a CSS identifier with jQuery

I'm struggling with a basic .each statement for comments where I want a form at the bottom to add new comments. The goal is simple - when someone submits a comment, I want it to display just above and move the form down using jQuery. Although this fun ...

If the connection is weak or there is a high volume of database connections, the Ajax GET requests

There is a code snippet that I use to retrieve JSON data from my hosting platform. However, there seems to be an issue where it duplicates the data when the internet connection is unstable. //Interval to run getChat() every 2 seconds setInterval(function( ...

Error: Module 'config' not found by Jest

I have encountered an issue while using Jest to test my api calls file. When running a simple test, I received an error Cannot find module 'config' from 'api.service.js'. This error is related to the import statement at the top of my ap ...