Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
How to implement dragging on table cells to select/highlight a range of cells in React?

I have a fairly complicated "datagrid" or basic "spreadsheet" implementation spanning several modules/files, totaling a few thousand lines of code so far, so it's not that easy to create/demonstrate a working example for StackOverflow. But you can imagine creating a basic html <table>:

<table>
  <thead>
    <tr>
      <th>A</th>
      <th>B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>foo</td>
      <td>bar</td>
    </tr>
    <tr>
      <td>hello</td>
      <td>world</td>
    </tr>
  </tbody>
</table>

This should look like a simple spreadsheet if given no styling. What I would like to figure out how to implement is dragging from a cell to highlight/select multiple cells, like in a spreadsheet. Here is a GIF recording a Google Spreadsheet, which I would like to mimic. (I only care about the functionality, not necessarily the styling / how it looks).

enter image description here

I could figure out how to implement this in vanilla / plain JS, by capturing mousemove after mousedown, and then every time the mouse moves (throttled perhaps in some way), find which table cell is under the mouse. I would store the starting cell which was first selected, then as the mouse moves, figure out the index of the hovered table cell, and then set the cell rectangle to be at that point.

So let's say we had an object storing the start and end index of the cell bounds.

{
  topLeft: [2, 2],
  bottomRight: [6, 6]
}

This would select the 3rd column, 3rd row, all the way to the 7th column, 7th row, similar to my image above.

The problem which I am having trouble imagining a solution for (in React), is twofold:

  1. How to properly track the mouse move and fetch the table cell under the mouse (using React paradigms).
  2. How to then get the index (row/column) associated with that table cell (i.e. the [6, 6] in the above example).

How would you do this in React, either generally speaking, or if it's straightforward, with a quick example? It seems extremely difficult and complex to me, hence it will take me a few days it feels like to come up with a simple reproducible (and not-really-working) example for SO. I will try, but wanted to get help as well too.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda