gridjs icon indicating copy to clipboard operation
gridjs copied to clipboard

add 'onclick' to row or specific row/column with row context added to event

Open jlmodell opened this issue 6 years ago • 11 comments

Is your feature request related to a problem? Please describe. Looking for a way to add an onclick listener to a row or cell that has the context for the row. Specifically looking for a way to use the cell or row to pop up a modal to to make a http get request for more details or http put request to update the backend and then update the table.

Describe the solution you'd like Ability to add an "onclick" listener to the row or specific row/column that would append the row's context to the onclick event.

Describe alternatives you've considered I've used material-ui datatables that has this functionality but I really like the gridjs style and functionality.

Additional context Add any other context or screenshots about the feature request here.

jlmodell avatar Jun 08 '20 16:06 jlmodell

Makes sense. I will add some examples to the docs.

afshinm avatar Jun 08 '20 16:06 afshinm

Have come to second this.

Working on a table which displays information 'at a glance'. Each object has way to much information to show in the table (including images, tags, dates etc.); so we're looking to make it such that the user can click (vuejs @click/v-bind) on the row to go to a new dynamic page with all of the extra data.

I have not installed gridjs (yet).

It looks like the <grid :cols="cols" :rows="rows"></grid> syntax leaves no room for this level of activity.

perhaps some formatting with slots could be helpful?

e.g.:

<grid :cols="cols">
    <row v-for="row in rows" @click="function(row)" />
</grid>

McKlatch avatar Jun 27 '20 22:06 McKlatch

@McKlatch gotcha, makes sense.

I released 1.8.0 yesterday which adds the ability to load an array of objects (in addition to array of arrays). That means that we should be able to add an onclick callback to each row:

data: [
  { name: 'David', phoneNumber: '009 9900', onClick: (row) => console.log(row) } 
]

Give me a few days. I will release a new version which will add all Event Handlers to a row.

afshinm avatar Jun 28 '20 11:06 afshinm

Hey! Timely. We just decided to replace a current homegrown datatable with gridjs. Our front end is nuxt but I'm experimenting not with the grid vue integration but just wrapping a component around the main library, and the array of objects update you made just a couple days ago has made converting a snap.

The chief thing I was trying to figure out before stumbling on this issue was how I can emit a Vue event either from a custom formatter or from engaging with the row itself.

You can write javascript in the column formatter, such as adding a button with an onclick, but if there was a more convenient way to call a method from within a cell or even just engaging with the row, I think that would be really useful.

My use case is that I would like to have a cell with custom actions with which I can call methods within the Vue component. The reason I'm not using the integration is because I don't think the html rendering in a cell is available,

michaelschofield avatar Jul 01 '20 00:07 michaelschofield

After writing this I thought I would try the component. I did have to import html from the core library along with the component -

import { html } from 'gridjs';
import Grid from 'gridjs-vue';

And I added a wrapper to the component's methods

  methods: {
    html(value) {
      return html(value);
    },

and In the component's cols attribute, I added the following formatter:

formatter: (cell) => html(`<button v-on:click.native='test()'>hi</button>`)}]"

I think this is about as close as I can get with 1.8.0. You can onclick anything in there, but I can't access the component's methods. Bummer.

Anyway, I'm really liking the library and excited to see it grow!

michaelschofield avatar Jul 01 '20 01:07 michaelschofield

@michaelschofield yeah true, you can use the html function or you can create and bind an HTML element to a cell document.createElement('b'); .... I was very busy refactoring and fixing some internal parts. I'm hoping to release a new version with onClick functionality this week.

afshinm avatar Jul 01 '20 08:07 afshinm

@afshinm Awesome. Also just 👏 kudos for being so active and quick to respond on a bunch of issues.

michaelschofield avatar Jul 01 '20 13:07 michaelschofield

@michaelschofield @McKlatch just published 1.10.0 which adds a global EventEmitter. see https://gridjs.io/docs/examples/row-buttons and https://gridjs.io/docs/examples/event-handler

Please let me know if those handlers/events are helpful. I'm happy to add more events or update them.

afshinm avatar Jul 05 '20 12:07 afshinm

Thank you so much for reading and responding to my comment. I appreciate it.

If I were to go about using the rowClick event, which way could I indicate interactivity ('rowHover')? I can use the row-button anyway, since buttons natively indicate that they are clickable.

Grid.js is now at a place where I can install it on a project i'm working on - will do this Monday/Tuesday. I'm excited to try it.

McKlatch avatar Jul 11 '20 16:07 McKlatch

@McKlatch I guess you can use a button in that case where you have hover/click/etc, https://gridjs.io/docs/examples/react-cells

afshinm avatar Jul 12 '20 13:07 afshinm

@michaelschofield @McKlatch Could you possible include in the docs how I might use this feature in vue-grid?

Nathaniel-Reeves avatar Jan 29 '24 23:01 Nathaniel-Reeves