Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading cell values from selection #361

Open
cerdamario13 opened this issue Aug 26, 2024 · 1 comment
Open

Reading cell values from selection #361

cerdamario13 opened this issue Aug 26, 2024 · 1 comment

Comments

@cerdamario13
Copy link

Is there a way to read cell values from a selection?

I made a simple code to do just that, but I am wondering if there is a built in method. Maybe I missed something in the docs?

.....
    const [data, setData] = React.useState<Row[]>([
      { firstName: 'Elon', lastName: 'Musk', custom: "" },
      { firstName: 'Jeff', lastName: 'Bezos', custom: "" },
      { firstName: "", lastName: "", custom: "" },
    ])

    const getSelectionValues = (selection) => {
      //If no selection, return
      if (!selection['selection']) return

      const colId = selection['selection'].min.colId;
      const startRow = selection['selection'].min.row;
      const endRow = selection['selection'].max.row;

      //Get the values from the state
      let values = [];
      data.forEach((row, idx) => {
        if (idx >= startRow && idx <= endRow) {
          values.push(row[colId]);
        }
      });
      console.log(values);
    };
  
    return (
      <DataSheetGrid
        value={data}
        onChange={setData}
        columns={columns}
        onSelectionChange={getSelectionValues}
      />
    )
.....

I know current methods onSelectionChange and selection return
({ selection: { min: Cell, max: Cell } | null }) => void

Would be nice if {value: 'Elon'} was included in the object. I can take a look at the code and submit a PR

Doc reference:
https://react-datasheet-grid.netlify.app/docs/api-reference/props/#onselectionchange

@cerdamario13 cerdamario13 changed the title Reading values from selection Reading cell values from selection Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@cerdamario13 and others