datatable icon indicating copy to clipboard operation
datatable copied to clipboard

Extract filtered records

Open patrickaglibut opened this issue 6 years ago • 11 comments

Good Day Sir,

Is there a way to extract all the table record, after filtering, etc, including the other paginated data?

Currently when using the datatable.all(true) option will only return the viewable part of the table but not the other data. Hoping for your response. Thanks.

Based on the screenshot below, I can only extract page 1 but i also need to extract pages 2 - 4.

image

patrickaglibut avatar May 31 '18 06:05 patrickaglibut

Are you sure that you get only the first page? datatable.all(true) should return everything, even filtered data.

Holt59 avatar May 31 '18 06:05 Holt59

Yes Sir, only the viewable data. Unless I change the pageSize to say 20 then that is the only time i can get all the record.

image

Btw I'm using jquery 1.12 and here's the code i use to extract the data.

$(document).on('click','#btnextract', function() { var datatable = new DataTable(document.getElementById('MyTable'), { identify: 'id' }); var savedData = datatable.all(true); console.log(savedData); });

patrickaglibut avatar May 31 '18 06:05 patrickaglibut

I cannot reproduce... If you go to this page: http://holt59.github.io/datatable/ and type datatable.all(true) in the console, you'll get 29 entries, whatever the current filters are on the example table.

Could you add more details regarding your code?

Holt59 avatar May 31 '18 06:05 Holt59

here's the html file i used using your sample table. Thanks for checking on this Sir.

test.zip

patrickaglibut avatar May 31 '18 06:05 patrickaglibut

On your site when I tested what you have instructed, the datatable.all(true) extracts all data even though it is already filtered wherein it should only be 7 records.

image

patrickaglibut avatar May 31 '18 06:05 patrickaglibut

Yes, this is the expected behavior currently, but it's different from having a single page.

Holt59 avatar May 31 '18 06:05 Holt59

In your code, you create a new DataTable to extract the data, this is not correct (you should never have two datatables on the same table). You should simple use the existing one:

$('#first-datatable-output table').datatable('select', true):

Holt59 avatar May 31 '18 06:05 Holt59

I see, so the .all feature will extract data regardless of the filters used. Thanks for noting this and I'll try to create a function to extract only the filtered result. Any pointers would be very helpful. Thanks again.

patrickaglibut avatar May 31 '18 06:05 patrickaglibut

You can use the filterIndex attribute:

// Retrieve the datatable object from the element.
var dt = $('#first-datatable-output table')[0].datatable; 

// Retrieve filtered data
var filteredData = [];
for (var i = 0; i < dt.filterIndex.length; ++i) {
    filteredData.push(dt.data[dt.filterIndex[i]]);
}

Not tested, but you get the idea.

Holt59 avatar May 31 '18 06:05 Holt59

Noted and Thanks

patrickaglibut avatar May 31 '18 06:05 patrickaglibut

I reopen this to keep it on the TODO list.

Holt59 avatar May 31 '18 08:05 Holt59