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

[data grid] Add new rows on paste #14209

Open
jamiedruce opened this issue Aug 14, 2024 · 3 comments
Open

[data grid] Add new rows on paste #14209

jamiedruce opened this issue Aug 14, 2024 · 3 comments
Labels
component: data grid This is the name of the generic UI component, not the React module! feature: Clipboard Related to clipboard copy or paste functionalities new feature New feature or request

Comments

@jamiedruce
Copy link

jamiedruce commented Aug 14, 2024

Summary

It would be great if pasting data into DataGrid could add additional rows if the length of copied data exceeds the selected rows.

So if I copied 5 rows, and selected the last row in a DataGrid, it would replace the selected row with the first row of copied data, and then add an additional 4 rows to complete the full clipboard import.

Obviously this wont be for everyone, so ideally would be enabled with a flag

Examples

No response

Motivation

Our use case typically involves adding a number of rows at a time, which may come from external sources.

Currently, If someone wanted to paste in 10 new rows, they would need to first add ten new blank lines, and then paste in the data.

Search keywords: datagrid
Order ID: 76116

@jamiedruce jamiedruce added new feature New feature or request status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 14, 2024
@michelengelen michelengelen changed the title Add new rows on DataGrid paste [data grid ]Add new rows on DataGrid paste Aug 15, 2024
@michelengelen
Copy link
Member

That's indeed an interesting use case.
I guess you can do this via a combination of splitClipboardPastedText (Format of the clipboard data) and onClipboardPasteStart

The order of execution is

  1. splitClipboardPastedText
  2. onClipboardPasteStart
  3. onClipboardPasteEnd (probably not needed in this case)

So with the first you can split the text into arrays, then in the second step you can add rows programmatically and fill them with the data.

I do think we could keep this as a feature request though so the implementation does not have to be that complex. A prop like addRowsForExcessPastedData or something similar.

Thoughts @joserodolfofreitas @mui/xgrid ?

@michelengelen michelengelen changed the title [data grid ]Add new rows on DataGrid paste [data grid] Add new rows on DataGrid paste Aug 15, 2024
@michelengelen michelengelen changed the title [data grid] Add new rows on DataGrid paste [data grid] Add new rows on paste Aug 15, 2024
@michelengelen michelengelen added component: data grid This is the name of the generic UI component, not the React module! feature: Clipboard Related to clipboard copy or paste functionalities and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 15, 2024
@github-project-automation github-project-automation bot moved this to 🆕 Needs refinement in MUI X Data Grid Aug 15, 2024
@ikitommi
Copy link

ikitommi commented Nov 8, 2024

I would really like to have this too. Or an example on how to do it if it's possible already.

@michelengelen
Copy link
Member

There could be a workaround similar to what we did in #15114

it could look something like this: Codesandbox demo

const handleBeforeClipboardPaste: DataGridPremiumProps['onBeforeClipboardPasteStart'] = ({
  data,
}) => {
  console.log(data.length);
  if (data.length === 0) {
    console.log('no data to paste');
    return Promise.resolve();
  }
  const selectedRowsCount = apiRef.current.getSelectedRows().size;
  // you need to have a function that will take the data from the clipboard and create a row object
  const newRows = parseDataIntoRows(data);
  const rowsToAdd = newRows.slice(selectedRowsCount);
  apiRef.current.updateRows(rowsToAdd);
  // we need to reject, because otherwise we will also override the selected rows with the values in the clipboard
  return Promise.resolve();
};

Please consider this a workaround and needs adjustment to fit your use case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! feature: Clipboard Related to clipboard copy or paste functionalities new feature New feature or request
Projects
Status: 🆕 Needs refinement
Development

No branches or pull requests

3 participants