Fully customizing output of copyHtml5 using exportOptions format

Fully customizing output of copyHtml5 using exportOptions format

zvuczvuc Posts: 6Questions: 2Answers: 0

I'm trying to build an copy to clipboard feature that formats the output in a certain way where it doesn't get affected by the current column visibility/order (but does print just rows that are visible now according to the current filter settings)

I thought of using the format and header/body to completely customize the return value, but it seems that data that can be accessed from within the function (data, row, column, node) is limited, and also that the function runs each for every column cell.

Below is an example I tried... column3header / column3data would be where the appropriate data gets inserted (I couldn't figure out how to fetch appropriate attributes from columns_params)

{
extend: 'copyHtml5',
text: 'Copy to Clipboard',
messageTop: '',
messageBottom: '',
fieldBoundary: '',
fieldSeparator: '',
exportOptions: {
format: {
header: function (data, row, column, node) {
var output = column3header + '/' + column1header + '/' + column2header;
return output;
},
body: function (data, row, column, node) {
var output = column3data + '/' + column1data + '/' + column2data;
return output;
}
},
columns: ':visible'
},
title: ""
}

Does anyone have a suggestion of ways I could achieve what I'm trying to do?

Answers

  • kthorngrenkthorngren Posts: 21,725Questions: 26Answers: 5,027

    I'm not clear what you want to customize. Take a look at the customize option of the copyHtml5 button. See an example of this using the print button here. The first parameter for the copyHTML customize function is the "The data to be copied as a string".

    Kevin

  • zvuczvuc Posts: 6Questions: 2Answers: 0
    edited March 1

    To give you more exact context — I'm trying to build a 'copy to clipboard' button that prints out the contents of the table converted to a 'wiki' syntax instead of the default HTML syntax.

    If my data goes:

    | ID     | Name     | Category  | Color    |
    --------------------------------------------
    | 1      | Apple    | Fruit     | Red      |
    | 2      | Banana   | Fruit     | Yellow   |
    | 3      | Chair    | Furniture | Green    |
    

    The output should become:

    {{{#!wiki style="word-break: normal"
    ||<tablewidth=100%><rowbgcolor=#ddd,#000><:>'''Category'''||<:>'''Name / Color'''||
    ||<:>{{{-1 {{{#42b6ff '''Fruit'''}}} }}}||<(>'''[[Apple]]''' [br] {{{-2 {{{#888 Red}}} }}}||
    ||<:>{{{-1 {{{#42b6ff '''Fruit'''}}} }}}||<(>'''[[Banana]]''' [br] {{{-2 {{{#888 Yellow}}} }}}||
    ||<:>{{{-1 {{{#acd249 '''Furniture'''}}} }}}||<(>'''[[Chair]]''' [br] {{{-2 {{{#888 Green}}} }}}||
    }}}
    

    I've managed to customize the opening head/enclosing foot syntax using

    messageTop: '{{{#!wiki style=\"word-break: normal\"',
    messageBottom: '}}}',
    

    And since the header will most likely be the same every time, I did this:

    exportOptions: {
      format: {
        header: function (data, row, column, node) {
          if (row === 1) {
            const output =
              "||<tablewidth=100%><rowbgcolor=#ddd,#000><:>'''Category'''" +
              "||<:>'''Name / Color'''||";
            return output;
          } else {
            return '';
          }
        },
    

    Then the problem goes for customizing the body.
    I understand that whatever mechanisms I put into the function (data, row, column, node) gets applied to the data, but I see that it runs sequentially for all columns from start to finish. So if I want to switch around column order (only for clipboard export) and merge, I'll need some way to access data from 'outside' because when the body function runs it only has access to the current node.

  • kthorngrenkthorngren Posts: 21,725Questions: 26Answers: 5,027

    There have been som e recent changes to how the order of the exported columns is handled. See if this thread helps and does what you want.

    Kevin

  • zvuczvuc Posts: 6Questions: 2Answers: 0

    Unfortunately the thread you linked to doesn't seem to be entirely related to my issue here.

    Any way could I access the DataTables api() from within the body function?

  • zvuczvuc Posts: 6Questions: 2Answers: 0

    I figured out a way myself, ended up manually getting the entire DataTable then finding the row I want using the node provided from within, then finding the columns I need.

    let rowData = $('#table').DataTable().row(node).data();

  • kthorngrenkthorngren Posts: 21,725Questions: 26Answers: 5,027
    edited March 1

    Sorry I haven't been much help. I'm not sure what you mean by column order, etc. Sounds like you have your issue resolved. Maybe creating a custom button, like this example would make it easier to customize the output. You will need to use Javascript to copy to the clipboard, maybe something like this.

    Kevin

Sign In or Register to comment.