How do I replace clipboard string on copy
How do I replace clipboard string on copy
hm123
Posts: 84Questions: 27Answers: 1
The current clipboard output is:
header 1
header 2
header 3
Row 1 data 1
Row 1 data 2
Row 1 data 3
Row 2 data 1
Row 2 data 2
Row 2 data 3
etc.
How do I achieve the following output:
(header 1 removed from here)
(header 2 removed from here)
(header 3 removed)
(header 1 prepended here followed by) Row 1 data 1
(header 2 prepended here followed by) Row 1 data 2
Row 1 data 3
(header 1 prepended here followed by) Row 2 data 1
(header 2 prepended here followed by) Row 2 data 2
Row 2 data 3
etc.
I tried the following:
{extend: 'copy',
customize: function( data ) {
console.log(JSON.stringify(data));
data= data.replace( /<th>(.?)</th>/, "<th>(.?)</th>+<tr>(.*?)</tr>" );
return data; },
But it doesn't work.
Jsfiddle:
https://jsfiddle.net/gfevt2qa/
Actual data example, I want to change this:
Company
Claims
Total Awarded
1ST AMERICAN LAW CENTER, INC., A CALIFORNIA CORPORATION
15
$135,045.23
ABC TRANSPORT, INC., A CALIFORNIA CORPORATION
12
$108,910.99
To this:
Company: 1ST AMERICAN LAW CENTER, INC., A CALIFORNIA CORPORATION
Claims: 15
$135,045.23
Company: ABC TRANSPORT, INC., A CALIFORNIA CORPORATION
Claims: 12
$108,910.99
This discussion has been closed.
Replies
My understanding is that the Copy button immediately transfers data to the clipboard, so altering the data is impossible.
Perhaps you could reformat your table for copying purposes.
I am able to alter the data transfered to the clipboard using the following to remove two headers:
https://jsfiddle.net/jrn6w772/
But I think this method only looks for any and all strings containing those texts wherever they might be, header or not, and replaces them with blanks.
I'm having trouble capturing specifically the headers (th) and row data (tr) along with (\n \t) and repositioning/removing them as stated originally in the first post.
The json stringifiied output that is given to the console is:
The export options that the buttons can use have functions which can be used to modify the header, body and footer text independently for the export. This example shows how the
body
one can be used - you can do similar for the header and footer as required.If you need something more than just changing the text on a cell by cell basis there is also a
customize
callback for thecopy
button type which can be used to manipulate the exported data in any way you want.Regards,
Allan
I looked at this a bit. I'm guessing you want a generic function that can work with any number of columns. You want to display each column on its own line with the appropriate column name. I would opt for using regex substitutions. There are multiple ways this can be done. I chose to break it up into small tasks.
This doc will help to understand the regex functions I'm using. It explains how
var re = new RegExp('pattern', 'flags');
works, the flags,g
in this case and regex substitution.You can try the code at:
https://jsfiddle.net/f0c6qfj2/
Hopefully the comments are enough for you to understand what I did.
Kevin
Sorry, missed the link to the Javascript Regex docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Kevin
I forgot to save the fiddle. Will try again tomorrow.
Kevin
Here is the saved version:
https://jsfiddle.net/f0c6qfj2/1/
Example output:
Kevin
This works brilliantly, until the Colvis toggle is used
Then it reverts back to the old bunched up string
Why isn't the formatted copy output string maintained when the column visibility button is used to hide a column?
In your copy button you have this configuration:
Remove the
columns: [':visible'],
if you want to export all columns regardless of visibility. You can change it to one of these other column selectors if you still need to control which columns are copied:https://datatables.net/reference/type/column-selector
Kevin
Removing the
columns: [':visible'],
does what is expected, ie: exports all columns whether colvis is toggled or not.But what I want to do is be able to export the columns according to their visibility as defined by the colvis toggle, while maintaining the formatted string that has been customized. Although a column might be toggled off and not displayed, the corresponding output string should still keep the line breaks as set before, instead of losing it's formatting.
I tried using
visIdx
instead but it doesn't seem to work:https://jsfiddle.net/hj3Lj947/
The customize code was written as an example based on the three columns in the table. It relies on your
columns
variable:If you remove one of the columns then the regex's built with the three columns won't work. Additional code will need to be written to handle column visibility.
Kevin