How do I replace clipboard string on copy

How do I replace clipboard string on copy

hm123hm123 Posts: 80Questions: 23Answers: 1
edited August 2017 in Free community support

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

Replies

  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    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.

  • hm123hm123 Posts: 80Questions: 23Answers: 1
    edited August 2017

    I am able to alter the data transfered to the clipboard using the following to remove two headers:

    data= data.replace( /\nCompany/, '' );
    data= data.replace( /Claims/, '' );
                  return data; },
    

    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:

    "\"top message\n\nExported data\n\nCompany\tClaims\tTotal Awarded\n1ST AMERICAN LAW CENTER, INC., A CALIFORNIA CORPORATION\t15\t$135,045.23\nABC TRANSPORT, INC., A CALIFORNIA CORPORATION\t12\t$108,910.99\nABSOLUTE PRODUCTION SERVICES, AN ILLINOIS CORPORATION;  AUDIO VISUAL MANAGEMENT GROUP, INC., A TEXAS CORPORATION\t10\t$109,952.88\""

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    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 the copy button type which can be used to manipulate the exported data in any way you want.

    Regards,
    Allan

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765
    edited August 2017

    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

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Sorry, missed the link to the Javascript Regex docs:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

    Kevin

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    I forgot to save the fiddle. Will try again tomorrow.

    Kevin

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Here is the saved version:
    https://jsfiddle.net/f0c6qfj2/1/

    Example output:

    top message
    
    pdt6
    
    Company: 1ST AMERICAN LAW CENTER, INC., A CALIFORNIA CORPORATION
    Claims: 15
    Total Awarded: $135,045.23
    
    Company: ABC TRANSPORT, INC., A CALIFORNIA CORPORATION
    Claims: 12
    Total Awarded: $108,910.99
    .....
    

    Kevin

  • hm123hm123 Posts: 80Questions: 23Answers: 1

    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?

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765
    edited November 2017

    In your copy button you have this configuration:

                      exportOptions: {columns: [':visible'],
                                      rows: [':visible']   } //copies currently displayed columns and rows
    

    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

  • hm123hm123 Posts: 80Questions: 23Answers: 1

    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/

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    The customize code was written as an example based on the three columns in the table. It relies on your columns variable:

    var columns = [{
     "data": "company",
      "title": "Company"
    }, {
      "data": "violations",
      "title": "Claims"
    }, {
      "data": "total-award",
      "title": "Total Awarded"
    }];
    

    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

This discussion has been closed.