Trigger the rowContextMenu in Tabulator Table by clicking a Button

Is there a way to add a button at the end of a table that, when clicked, opens a rowContextMenu below the button? Additionally, can the rowContextMenu pop up when right-clicking anywhere on the row?

I have attempted some solutions without success. Here is one example:

var menuButtonFormatter = (cell: any) => {
    var menuBtn = document.createElement('button');
          menuBtn.type = 'button';
          menuBtn.innerHTML = '<span class="material-icons" style="color: #707070">more_horiz</span>';
          menuBtn.classList.add('menu-button');
          menuBtn.addEventListener('click', (event) => {
            event.stopImmediatePropagation();
            const myEvent: any = new Event('row-contextmenu');
            myEvent.pageX = event.pageX;
            myEvent.pageY = event.pageY;
            cell.getRow().getElement().dispatchEvent(myEvent);
          });

    buttonHolder.appendChild(menuBtn);
    return buttonHolder;

}

The code snippet above creates the button. However, I am struggling to make it function as desired.

This is the Button below:

Below is an example of how the column looks:

{
        title: this.$t('actions'),
        field: 'actions',
        // formatterParams: poParams,
        formatter:menuButtonFormatter,
        headerSort: false,
        width: 110,
        frozen: true,
}

I have tried various things but none seem to be working. For instance, using

const myEvent: any = new Event('contextmenu');
as the button Event did not produce the desired result and no errors were shown in the Console.

Answer №1

Consider utilizing the MouseEvent event as an alternative. See below for a code snippet:

const tableData = [
  { id: 1, fullName: 'Oli Bob', age: '12', color: 'red' },
  { id: 2, fullName: 'Mary May', age: '1', color: 'blue' },
  { id: 3, fullName: 'Christine Lobowski', age: '42', color: 'green' },
  { id: 4, fullName: 'John Smith', age: '30', color: 'red' },
  { id: 5, fullName: 'Tim Burton', age: '51', color: 'blue' }
]

var menuButtonFormatter = (cell, formatterParams, onRendered) => {
  const menuBtn = document.createElement('button')
  menuBtn.type = 'button'
  menuBtn.innerHTML = '<span class="material-icons" style="color: #707070">more_horiz</span>'
  menuBtn.classList.add('menu-button')
  menuBtn.addEventListener('click', (event) => {
    const myEvent = new MouseEvent('contextmenu', {
      bubbles: true,
      clientX: event.pageX,
      clientY: event.pageY
    })

    cell.getRow().getElement().dispatchEvent(myEvent)
  })

  onRendered(() => {
    cell.getElement().appendChild(menuBtn)
  })
}

const table = new Tabulator('#table', {
  data: tableData,
  layout: 'fitDataFill',
  rowContextMenu: [
    {
      label: 'Delete Row',
      action: (e, row) => {
        row.delete()
      }
    }
  ],
  columns: [
    { title: 'Name', field: 'fullName' },
    { title: 'Age', field: 'age' },
    { title: 'Color', field: 'color' },
    {
      title: 'Button',
      field: 'actions',
      formatter: menuButtonFormatter,
      headerSort: false,
      width: 110,
      frozen: true
    }
  ]
})
<!DOCTYPE html>
<html lang="en">

<head>
  <link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4809596819895809b86d9809596989187b4c1dac2dac5">[email protected]</a>/dist/css/tabulator.min.css" rel="stylesheet" />
  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abdfcac9dec7cadfc4d986dfcac9c7ced8eb9e859d859a">[email protected]</a>/dist/js/tabulator.min.js"></script>
</head>

<body>
  <div id="table"></div>
</body>

</html>

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

Simplifying complex JSON structures by un-nesting arrays

Within my Formik form, I have 3 fields: MemberMemberID, EventEventID, and event_date. This form represents an event (such as a Tuesday club) taking place on a specific date and attended by various members. Formik stores the data in key-value pairs within ...

The Array of Objects is not being generated from Action and Effects

I'm attempting to retrieve an array of objects stored in the User model. I've created both an Action and an Effect for this purpose. The structure of the User Model is as follows: export interface User { _id: string, firstName: string, lastName: ...

Filtering data based on dates in AngularJS

I need to implement a data filter based on event date. The options for filtering include the current day, current month, and current year. Below is the current implementation: function filterDate($scope) { var d = new Date(); var curr_date = d.get ...

Swap out a term in a sentence with an interactive span element in real-time

I'm struggling with a seemingly simple task, and I feel quite embarrassed that I can't seem to solve it. My goal is to identify words in a string that begin with '@' or '#' and change their color to blue. The string itself com ...

Having trouble utilizing Vue3 methods while utilizing the `<script setup lang="ts">` syntax

Looking to incorporate Vue into a non-Vue Class using the Composition API for a Chrome Extension project where having the entire thing as a Vue App doesn't make sense. The Vue Instance is being instantiated in the usual manner and then injected into ...

What is the best way to add a constant value to all objects within an array without having to iterate through each one

Is there a more concise way to add a fixed value to each object in an array without using a loop in JavaScript? Programming Language used: JavaScript Example Array: "cars": [ { "name":"Ford", "models":"Fiesta" }, { "name":"BMW", "models":"X1" }, ...

Is it acceptable to assign a value to exports.new?

In my venture with Node.js and Express, I am aiming for simplicity. Drawing from my experience with Rails, I am following the RESTful structure typical in Rails applications. In setting up my controllers (or routes), I want them to resemble this: // route ...

TailwindCSS applies CSS settings from tailwind.admin.config.js without overriding tailwind.config.js. The @config directive is utilized for this purpose

I'm currently working on a project using Vite and React. I have a tailwind.admin.css file that is similar to the example provided in the documentation. @config './configs/tailwind.admin.config.js'; @tailwind base; @tailwind components; @tai ...

Displaying multiple arrays using ng-repeat

My task is to display a list organized by date, but the problem is that the list is not sorted and I can't figure out when the date changes. This situation is similar to having the following Json: list = {name: first, date: 2014-05-21}, { {name: sec ...

After toggling the class, Jquery will no longer select the button

I am having an issue with my jQuery code where I have a button that, when clicked, should switch classes from #testButton to .first or .second. The image toggle shows that the first click works fine and toggles the image, but the second click does not seem ...

Guide to accessing component methods within slots using the Vue 3 Composition API

I have child components within a slot in a parent component and I am trying to call methods on them. Here are the steps I followed: Use useSlots to retrieve the child components as objects Expose the method in the child component using defineExpose Call t ...

Tips for incorporating conditions when updating data in MongoDB

I am looking for assistance with updating the secondary phone number in the code below. I want to update it only if a 10-digit number is passed from the web form; otherwise, I would like to use the already inserted phone number during the insert operation. ...

JavaScript: Issue with launching Firefox browser in Selenium

I'm currently diving into Selenium WebDriver and teaching myself how to use it with JavaScript. My current challenge is trying to launch the Firefox browser. Here are the specs of my machine: Operating System: Windows 7 64-bit Processor: i5 Process ...

Having trouble with integrating user input from HTML into a JavaScript file to execute a GET request

I am currently working on a project to create a website that integrates the google books API for users to search for books. To start, I have set up a server using express in index.js at the root of the project directory, and all my static files are stored ...

Encountering an issue with NextJS useRouter when it is utilized within a function

I have encountered a puzzling issue that I can't seem to resolve. In my experimentation with NextJS, I am attempting to access the params in the router by utilizing the useRouter hook and integrating it with the querystring plugin to parse the asPath, ...

Executing multiple instances of the cascading dropdown fill method in Angular 7

I am currently working on an Angular app that includes cascading comboboxes for country and state selection. However, I have noticed that the get states() method in my state.component.ts file is taking a long time to run. What could be causing this issue? ...

Motion graphics following the completion of a form input

In my HTML, I've created a div container with a form field: <div class="flex_item" id="b_one"> <form id="f_one"> <input id="i_one" type="text"> </form> </div> I'm attempting to change the backgroun ...

Encountering a problem when trying to create a node in Neo4j using Node.js

Here is my code for a Node.js application using Neo4j: var neo4j = require('neo4j-driver').v1; var express = require('express'); var logger = require('morgan'); var path = require('path'); var bodyParser =require(&a ...

What is the best way to incorporate several functions within a resize function?

Is it possible to incorporate multiple functions within the windows.width resize function? I have been experimenting with some code, but I would like to restrict its usage to tablet and mobile devices only, excluding laptops and PCs. Any suggestions on h ...

What is the best way to display the next and restart buttons at the bottom of every question?

How can I display the next and restart buttons at the bottom of each question in my JavaScript quiz web app? Why is the user unable to choose the wrong answer from the provided options? I'm facing difficulty showing the next and restart buttons for i ...