DataTables gets array data from selected rows.

Using the select plugin, I try to get data from selected rows using rows().data() , but the data it pulls is much larger than the array of cell data, I want to present this data in an AJAX POST request, but I get an error 413, saying "the request object is too large."

https://datatables.net/reference/api/rows () .data ()

 var dataTable = $('#products').DataTable( { "processing": true, "ajax": "/products", "columns": [ { "className": 'select-checkbox', "defaultContent": '<span style="display:none;">0</span>', "orderDataType": "dom-text", type: 'string' }, { "data": "sku", "className": 'sku-value', "defaultContent": "" }, { "data": "name", "className": 'name-value', "defaultContent": "" }, { "data": "our_price", "className": 'our-price-value price', "defaultContent": 0 }, { "data": "sale_price", "className": 'sale-price-value price', "defaultContent": 0 }, { "data": "rrp_price", "className": 'rrp-price-value price', "defaultContent": 0 }, { "data": "similar_price", "className": 'similar-price-value price', "defaultContent": 0 }, { "data": "stores.one.store", "className": 'store-one store', "defaultContent": "" }, { "data": "stores.two.store", "className": 'store-two store', "defaultContent": "" }, { "data": "stores.three.store", "className": 'store-three store', "defaultContent": "" }, { "data": "end_date", "className": 'end-date-value date', "defaultContent": "" }, { "data": "updated_at", "className": 'updated-at-value date', "defaultContent": "" } ], "select": { "style": 'multi', "selector": 'td:first-child' }, }); var rowData = dataTable.rows( { selected: true } ).data(); 

But out of the three rows selected with 5 cells with some very small values, the Chrome dev tools freeze trying to load all of this, since this value also contains all 2200 rows in the table.: /

My code is taken from here: https://datatables.net/extensions/select/examples/api/get.html

In an object where rowData is an array mixed with a bunch of other functions, the array has all the data I need, but I cannot separate it from everything else.

Data inside rowData: enter image description here

I just need the first three lines, an array.

Switch to:

 $.post('/generate', rowData, function() { console.log('done'); }); 
+5
source share
1 answer

The rows().data() API method returns an object of type Array, which is also an instance of the DataTables API.

Use the toArray() API method to convert an instance of the API into a native Javascript array object.

 var rowData = dataTable.rows( { selected: true } ).data().toArray(); 
+4
source

Source: https://habr.com/ru/post/1237299/


All Articles