TonyGermaneri / canvas-datagrid

Canvas based data grid web component. Capable of displaying millions of contiguous hierarchical rows and columns without paging or loading, on a single canvas element.
BSD 3-Clause "New" or "Revised" License
1.44k stars 187 forks source link

Possible to create a 'button' within a cell? #398

Closed david542542 closed 2 years ago

david542542 commented 2 years ago

In Excel and Google Sheets a 'button' is often used for a context menu related to a heading, for example:

image

Does canvas-datagrid currently support this, or is it something that would need to be added as a new feature? If it's something that's already supported, could we add an example of it to the Tutorials section?

ndrsn commented 2 years ago

It's not supported out of the box, but you can hook into the afterrendercell event to draw your own 'button' on top of the canvas. Here's a crude example (it's a box not a button, but you get the idea):

CleanShot 2021-11-12 at 14 17 44@2x
grid.addEventListener('afterrendercell', (event) => {
  if (event.cell.rowIndex === -1 && event.cell.columnIndex > -1) {
    drawButton(event.ctx, event.cell);
  }
});

Obviously, you'd want to make it actually look like a button, render differently on hover or active states, but what this looks like depends a little bit on what the rest of your application looks like — I don't think canvas-datagrid should be too opinionated in terms of UI look and feel, or you lose flexibility when integrating.

Alternatively, you could use HTML buttons. I have the same issue: I also want to show filter buttons like Excel, and canvas drawing seems cumbersome, so this is the route I've wanted to investigate, but haven't gotten around to yet.

david542542 commented 2 years ago

@xianzhi3 here are two more examples of where a button could be used: https://gyazo.com/905512e26cb23f9458d3a2fc66532fcb. In other words, it would be great to be able to arbitrarily position and style the buttons (ignore what they produce after clicked).

david542542 commented 2 years ago

@xianzhi3 + @ndrsn the button looks good. Can you do two demo examples on code-sandbox, one where the buttons are at the top of a table, and the other where there's just a one-off button (like you copy-paste example) and then I think this is ready for a PR. Nice work.

david542542 commented 2 years ago

image

xianzhi3 commented 2 years ago

I created a PR

ndrsn commented 2 years ago

Ah whoops I closed this by accident; have not seen an example yet of the second case.

david542542 commented 2 years ago

What's the second case? It should cover both I just tested them: https://gyazo.com/792000cb8dacd9d3c84f222a16be51a3

ndrsn commented 2 years ago

Ah, apologies — didn't see the other one. Not necessarily relevant for this particular issue, but is there a way to remove the button, as well?

This is the behavior I'm seeing:

CleanShot 2021-12-04 at 22 13 05

david542542 commented 2 years ago

Oh yea good catch, let’s pls fix that Xianzhi, or at least expose a handler to deal with it.

On Sat, Dec 4, 2021 at 1:14 PM Jona Andersen @.***> wrote:

Ah, apologies — didn't see the other one. Not necessarily relevant for this particular issue, but is there a way to remove the button, as well?

This is the behavior I'm seeing:

[image: CleanShot 2021-12-04 at 22 13 05] https://user-images.githubusercontent.com/950979/144724752-8c56900a-d693-4512-a3dc-8361919c7766.gif

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/TonyGermaneri/canvas-datagrid/issues/398#issuecomment-986093864, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG6DS3JT5CKZTCM3XPNPTLUPKAFHANCNFSM5HTOQIIQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

ndrsn commented 2 years ago

While you guys are at it, I noticed that not all areas of the button seem to activate the dropdown:

CleanShot 2021-12-05 at 00 01 03

Not a biggie, though.

david542542 commented 2 years ago

Yes exactly. Good call!

On Sat, Dec 4, 2021 at 3:02 PM Jona Andersen @.***> wrote:

While you guys are at it, I noticed that not all areas of the button seem to activate the dropdown:

[image: CleanShot 2021-12-05 at 00 01 03] https://user-images.githubusercontent.com/950979/144727193-9b4791f9-edc4-4553-8bd7-9eba033f9605.gif

Not a biggie, though.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/TonyGermaneri/canvas-datagrid/issues/398#issuecomment-986106028, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG6DSZ65SOP2ZNXQHW5H2DUPKMZRANCNFSM5HTOQIIQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

xianzhi3 commented 2 years ago

@ndrsn + @david542542 I fixed the bugs. Can you check it at the site https://90zfd.csb.app/?

xianzhi3 commented 2 years ago

@ndrsn I created a PR for the changes