Tips for controlling the addition and removal of multiple class names on table row mouse events

I am using a library called react-bootstrap-table to display my data. I want to dynamically add two buttons over the last two columns of any row when it is hovered over, similar to the image above.

To achieve this functionality, I have utilized classnames and innerHTML onRowMouseOver event.

The added innerHtml element is removed based on the added ClassNames onRowMouseOut event.

Although I can successfully add two buttons to the hovered row, there is a continuous flickering effect when hovering over these buttons.

Below is a code snippet for reference:

import React, { Component } from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
// Data array 
const products = [
  {
    id: 1,
    name: "abcdef",
    price: 120,
    email:"[email protected]",
    phone:"9856325632",
    submitted:"10/02/18",
    responded:"01/09/18",
    status:"active"
  },
  ...
];

// Total number of products
const total = products.length;

class Tables extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '',
      selectedDate: null,
      page: 1,
      goToPageNum:1,
      disableGoButton:true,
      disableGoToInput:false,
      size:5,
    };
  }

  // Function to handle size per page change
  onSizePerPageList = (sizePerPage) => {
    this.setState({size:sizePerPage},()=> this.hideGTP());
  }

  expandComponent(row) {
    return (
      <div className="col-3">
        ... // Code for expanding component
      </div>
    )
  }

  render() {
    const options = {
      page: this.state.page,
      onRowMouseOut: function(row, e) {
       ... // Functionality to remove elements on mouse out
      },
      onRowMouseOver: function(row, e) {
        ... // Functionality to add elements on mouse over
      }
    };
  
   // Render method to display table with BootstrapTable
    return (
      <div className="container py-5">
        <BootstrapTable
          trClassName="table-row"
          bordered={false}
          ref='table'
          data={products}
          options={options}
          search={true}
        >
          ... // Table Header Columns
        </BootstrapTable>
      </div>
    );
  }
}

export default Tables;

The issue I am facing is the Flickering Effect: There is a continuous flickering of the added elements when they are hovered over. Any advice on how to resolve this would be greatly appreciated.

View Codedandbox Demo: My Demo Codesandbox Link: https://codesandbox.io/s/p910j5k6x

Answer №1

It seems like the buttons you're inserting might be interfering with the mouse hover event on the row.

As soon as your mouse hovers over the row, a div with buttons is inserted into the DOM, covering the row and preventing any further mouse events from being triggered on the row itself. Because the mouse technically moves out of the row element, the buttons are then removed.

To avoid this issue, consider attaching the div containing the buttons directly to the row element.

Answer №2

I was able to solve the issue by implementing the code below:

import React, { Fragment, Component } from "react";
import { BootstrapTable, TableHeaderColumn } from "react-bootstrap-table";
const products = [
  {
    id: 1,
    name: "abcdef",
    price: 120,
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="badbd8d9faddd7dbd3d694d9d5d7">[email protected]</a>",
    phone: "9856325632",
    submitted: "10/02/18",
    responded: "01/09/18",
    status: "active"
  },
  {
    id: 2,
    name: "abcdef",
    price: 120,
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8decefeecdeae0ece4e1a3eee2e0">[email protected]</a>",
    phone: "9856325632",
    submitted: "10/02/18",
    responded: "01/09/18",
    status: "active"
  },
  {
    id: 3,
    name: "abcdef",
    price: 120,
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="68090a0b280f05090104460b0705">[email protected]</a>",
    phone: "9856325632",
    submitted: "10/02/18",
    responded: "01/09/18",
    status: "active"
  },
  {
    id: 4,
    name: "abcdef",
    price: 120,
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d4d7d6f5d2d8d4dcd99bd6dad8">[email protected]</a>",
    phone: "9856325632",
    submitted: "10/02/18",
    responded: "01/09/18",
    status: "active"
  },

  {
    id: 5,
    name: "abcdef",
    price: 120,
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e8898a8ba88f85898184c68b8785">[email protected]</a>",
    phone: "9856325632",
    submitted: "10/02/18",
    responded: "01/09/18",
    status: "active"
  }
];

const total = products.length;
class Tables extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: "",
      selectedDate: null,
      page: 1,
      goToPageNum: 1,
      disableGoButton: true,
      disableGoToInput: false,
      size: 5
    };
  }

  onSizePerPageList = sizePerPage => {
    this.setState({ size: sizePerPage }, () => this.hideGTP());
  };

  expandComponent(row) {
    return (
      <div className="col-3">
        <div className="card bg-warning">
          <div className="card-body card-custom d-flex justify-content-around">
            <div className="card-text">
              <button
                type="button"
                class="btn btn-warning"
                onClick={() => alert("hello!!!!!!")}
              >
                Change Status
              </button>
            </div>
            <div className="card-text d-flex align-items-center">Remove</div>
          </div>
        </div>
      </div>
    );
  }

  helloww() {
    alert("heyyy its working");
  }
  render() {
    const options = {
      page: this.state.page,
      onRowMouseOut: function(row, e) {
        let ind = row.id - 1;
        let element = document.querySelectorAll("tbody")[0].children[ind];
        const buttons = element.getElementsByClassName("position-child")[0];
        buttons.style.display = "none";
      },
      onRowMouseOver: function(row, e) {
        let ind = row.id - 1;
        let addClass = document.querySelectorAll("tbody")[0].children[ind];
        let buttons = addClass.getElementsByClassName("position-child")[0];
        buttons.style.display = "block";
      }
    };

    const priceFormatter = (cell, row) => {
      return (
        <Fragment>
          {row.status}
          <div className="position-child">
            <div class="row " onmouseenter="mouseEnter()">
              <div class="col-6">
                <button type="button" class="btn btn-warning">
                  Change Status
                </button>
              </div>
              <div class="col-6">
                <button
                  type="button"
                  class="btn btn-warning"
                  onClick={e => this.helloww()}
                >
                  Delete User
                </button>
              </div>
            </div>
          </div>
        </Fragment>
      );
    };

    return (
      <div className="container py-5">
        <BootstrapTable
          trClassName="table-row"
          bordered={false}
          ref="table"
          data={products}
          options={options}
          search={true}
        >
          <TableHeaderColumn dataField="id" isKey={true} width="20%">
            ID
          </TableHeaderColumn>
          <TableHeaderColumn dataField="name" width="20%">
            NAME
          </TableHeaderColumn>
          <TableHeaderColumn dataField="email" width="20%">
            EMAIL
          </TableHeaderColumn>
          <TableHeaderColumn dataField="phone" width="20%">
            PHONE
          </TableHeaderColumn>
          <TableHeaderColumn dataField="submitted" width="20%">
            LOGIN
          </TableHeaderColumn>
          <TableHeaderColumn dataField="responded" width="20%">
            SIGNUP
          </TableHeaderColumn>
          <TableHeaderColumn
            dataField="status"
            dataFormat={priceFormatter}
            width="20%"
          >
            STATUS
          </TableHeaderColumn>
        </BootstrapTable>
      </div>
    );
  }
}

export default Tables;
.position-child {
    width: 25%;
    position: absolute;
    right: 131px;
    background: red;
    margin-top: -30px;
    display: none;
  }

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

The use of Angular directives is restricted to one per page

I'm encountering a small issue with my angularJS directive. I want to display 2 photos in different ways using other HTML codes, but I'm running into a problem where only one directive works per page. The second one only works when I comment out ...

Sticky jQuery image not moving while scrolling

Upon visiting the mentioned website and scrolling down, you may notice that the logo image becomes stuck when transitioning between divs. This issue is likely due to my limited understanding of jQuery. The image is set as fixed using CSS and its fading eff ...

Issue with Bootstrap 4 tab.js not updating active states

I am having trouble with 2 out of the 4 tab.js examples in both CodePen and a local HTML file. The "nav" example is causing the items in the dropdown to stay active and inaccessible after clicking. The same issue occurs with all the items in the vertical t ...

Stop automatic form submissions from the enter key

My live chat messaging system is experiencing an issue where the page refreshes every time a user presses the enter button. I attempted to use prevent default code, but it did not work for me. I'm new to website programming, so if there are any proble ...

What steps are involved in creating a circular shape on canvas?

I am trying to create a circular shape in the canvas using the code below. The code involves moving an object with keyboard keys, and I need help making the canvas shape into a circle without affecting the functionality of the code. I attempted to modify t ...

`Manipulate the class attribute in HTML by utilizing jQuery or JavaScript`

Is it possible to modify this particular line of code? <li id="activehome" ><a href="#" >Home</a></li> to this: <li id="activehome" class="active"><a href="#" >Home</a></li> I need assistance on h ...

What is the best way to switch image position using javascript?

I'm experimenting with creating a function that toggles the image source when clicked - changing it back and forth. <img src="http://images.clipartpanda.com/laughing-smiley-face-clip-art-smiley-face-clip-art10.jpeg" longdesc="http://sm ...

Learn the Method Used by Digg to Eliminate "&x=0&y=0" from their Search Results URL

Instead of using a regular submit button, I have implemented an image as the submit button for my search form: <input id="search" type="image" alt="Search" src="/images/searchButton.png" name="" /> However, I encountered an issue in Chrome and Fire ...

The React Table is showing an error due to incompatible property types for 'accessor'

Currently experimenting with react-table in a create-react-app project (version ^7.0.25). Utilizing the example provided in their quick start documentation. However, encountered a type error between the accessor and data columns. Below is the snippet of co ...

Transmitting information via Ajax, jquery, Node.js, and Express

Seeking assistance as I struggle to comprehend the process while trying to implement it based on various online resources. My carousel directs users right after signing up, and I aim to gather information about their profile through simple input questions. ...

Displaying information in a drop-down list based on the selection made in a previous drop

Hey fellow developers, I could really use your expertise on a project I'm working on for attendance management. I've hit a roadblock with a specific feature - my goal is to dynamically display departments based on the selected block without requi ...

Is there a way to retrieve an environment variable while working within the public folder?

Within my create-react-app project, there is a javascript file located in the public folder. It is crucial for me to access an environment variable named REACT_APP_ACTUAL_ENV, but I am facing challenges as the usual method process.env.REACT_APP_ACTUAL_EN ...

Is there a way to align an image to the center using the display grid property?

Having some trouble centering an image within a 'display: grid' layout. Screenshot I attempted to use 'align-items' but it didn't seem to work either. Code: <section style="padding-top: 30px;"> <h4 sty ...

The collapsible navigation bar vanishes when toggling in Bootstrap 4

My first time using Bootstrap 4, and I'm encountering a challenge that was much simpler in Bs3: I want the link items in my navbar to collapse into a hamburger menu when the screen size is xs. While the documentation and another answer touch on this, ...

What is the best way to pass a variable between epics located in separate files?

I've incorporated redux-observable into my web application built with Next.js. In this setup, there is a crucial variable that needs to be initialized in epic1 and referenced later in epic2: let sharedVariable; const epic1 = (action$, state$) => { ...

What steps can be taken to modify the `select` element in IE?

I came across a solution to display an arrow on a select element in this answer: select { width: 268px; padding: 5px; font-size: 16px; line-height: 1; border: 0; border-radius: 5px; height: 34px; background: url(http://cdn1 ...

Fluid Bootstrap layout with a fixed sidebar width

My website has a responsive design based on Twitter's Bootstrap framework. <div class="container-fluid"> <div class="row-fluid"> <div class="span2"> <!--Sidebar content--> </div> <d ...

Failure to fetch data through Axios Post method with a Parameter Object

I've encountered an issue with Axios while attempting to make a post request with a parameters object to a Laravel route. If I use query parameters like ?username=user, the post request works successfully. However, when I use an object, it fails: Be ...

Discovering elements with Selenium through XPATH or CSS Selector

Having trouble locating the "import" element using Selenium Webdriver in C#. I've attempted the following codes but haven't had any luck: driver.FindElement(By.XPath("//*[@class='menu_bg']/ul/li[3]")).Click(); driver.FindElement(By.XPa ...

Arranging block-level elements in a horizontal format

In the book "CSS The definitive Guide", it is mentioned that "The values of these seven properties must add up to the width of the element’s containing block, which is usually the value of width for a block element’s parent". However, in the case provi ...