Holt59 / datatable

Javascript Plugin for data tables creation
http://holt59.github.io/datatable/
MIT License
107 stars 42 forks source link

Extract filtered records #33

Open patrickaglibut opened 6 years ago

patrickaglibut commented 6 years ago

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

Holt59 commented 6 years ago

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

patrickaglibut commented 6 years ago

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); });

Holt59 commented 6 years ago

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?

patrickaglibut commented 6 years ago

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

test.zip

patrickaglibut commented 6 years ago

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

Holt59 commented 6 years ago

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

Holt59 commented 6 years ago

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):
patrickaglibut commented 6 years ago

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.

Holt59 commented 6 years ago

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.

patrickaglibut commented 6 years ago

Noted and Thanks

Holt59 commented 6 years ago

I reopen this to keep it on the TODO list.