Search code examples
javascriptreactjscopyag-gridclipboard

AG Grid table data copy to clipboard not working in react


I tried to copy the ag grid table to clipboard using class name when click on button but it was not working.

 copyTable() {       
            var tblDat= document.getElementsByClassName('tableStats');
            tblDat.select();
            document.execCommand('copy'); 
    }

Solution

  • Don't use document to select the data, AG Grid provides clipboard functionality to help with this.

    Change your function to:

    copyTable()
    {
        this.gridApi.selectAll();
        this.gridApi.copySelectedRowsToClipboard();
    }
    

    This is will first select all the rows, then copy them to the clipboard.