2

I have a DataTable with paging, filtering and ColVis plugin (column visibility). By pressing a button, I need to get the visible and filtered data of all pages, and generate a new regular table below with this data (this one without datatables, pager, ...).

I tried with oTable.rows({search:'applied'}).data() to get the rows, but instead of getting only the data of the visible columns, it gets the hidden ones as well. And anyway I don't know how to generate the new table.

Here's a demo

How could I do this?

Thanks in advance

2 Answers 2

2

My answer is based on @davidkonrad's answer with some modifications:

$('button.print-bt').on('click', function() {               
    var fvData = oTable.rows({ search:'applied', page: 'all' }).data(); 

    $('#main_wrapper').append(
       '<table id="newTable">' +
       '<thead>'+
       '<tr>'+
          $.map(oTable.columns().visible(),
              function(colvisible, colindex){
                 return (colvisible) ? "<th>" + $(oTable.column(colindex).header()).html() + "</th>" : null;
           }).join("") +
       '</tr>'+
       '</thead>'+
       '<tbody>' +
          $.map(fvData, function(rowdata, rowindex){
             return "<tr>" + $.map(oTable.columns().visible(),
                function(colvisible, colindex){
                   return (colvisible) ? "<td>" + $('<div/>').text(rowdata[colindex]).html() + "</td>" : null;
                }).join("") + "</tr>";
          }).join("") +
       '</tbody></table>'
    );
});

My answer may not work with a table having objects as data source and may need to be modified where data is retrieved with rowdata[colindex].

I'm using $('<div/>').text(data).html() trick to encode HTML entities that could be present in the data.

See this JSFiddle for demonstration.

Sign up to request clarification or add additional context in comments.

6 Comments

Nice! You both helped alot. Thank you
Your answer says "I'm using $('<div/>').text(data).html() trick to encode HTML ..." but in some of my TDs there are SPANS that are not being encoded. I forked your demo to show the issue.
If you're using HTML in your cells, then get replace $('<div/>').text(rowdata[colindex]).html() with rowdata[colindex]. I need to experiment, maybe this conversion isn't necessary at all.
Yes, replacing that worked like a charm. Thanks again!
I'm sorry I need so much attention. I have a test environment (local) where it works perfectly, but when I bring the same code to production, then every cell has the content of the entire row, repeated. Something like: <tr><td>cell1-data, cell2-data, cell3-data</td><td>cell1-data, cell2-data, cell3-data</td><td>cell1-data, cell2-data, cell3-data</td></tr> It looks that rowdata[colindex] is droping all cells of each row instead of one cell each time. Any idea? Thanks in advance
|
1

oTable.rows({ search:'applied', visible:true }).data(); is not valid. See the docs for rows() selector-modifier.

In order to get filtered visual rows you should use page: 'current', so

var fvData = oTable.rows({ search:'applied', page: 'current' }).data();

...would do the trick. To create a completely new table from scratch, and insert the above filtered visible rows you could add this to your click handler :

$('#main_wrapper').append('<table id="newTable">'+
    '<thead>'+
       '<tr>'+
          '<th>Column 1</th>'+
          '<th>Column 2</th>'+
          '<th>Column 3</th>'+
          '<th>Column 4 (hidden)</th>'+
       '</tr>'+
     '</thead>'+
     '<tbody></tbody></table>');

var newTable = $("#newTable").DataTable();
for (var i=0;i<fvData.length;i++) {
    newTable.row.add(fvData[i]).draw();
}    

forked fiddle -> https://jsfiddle.net/sdz1n1gk/

7 Comments

This only extracts data on current page. It seems that original poster wanted visible and filtered data of all pages.
Thanks for your answer. It was very helpful. But in the forked fiddle you provided is still needed NOT to show the hidden column. And actually it's not necessary to attach the new table to DataTable(), it can be a plain table. Also, I will be using page: 'all', since I need to see all the results without pager: New fork
This answer could be modified for DataTables 1.10 to filter out hidden columns.
Thanks @Gyrocode.com. Could be something similar to var fvData = oTable.columns({ search:'applied', page: 'all' }).visible().data();? (no success yet)
@davidkonrad, since I based my answer on yours, if you update your answer, I will remove mine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.