Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables get array data of cells of selected rows Only

Using the select plugin, I'm trying to get the data from the selected rows using rows().data() but the data it pulls is much more than the array of cell data, I want to submit that data in a Ajax POST request, but I'm getting a 413 error saying "request entity 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 from 3 rows selected with 5 cells with some very small values, Chrome dev tools freezes up trying to load it all, as somehow that value also contains all 2200 rows in the table as well. :/

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

In the object that rowData is there's a array mixed in with a heap of other functions, the array has all the data I need, but I cant separate it form all the other stuff.

The data inside rowData: enter image description here

I just want those first 3 lines, the array.

To pass to:

$.post('/generate', rowData, function() {
    console.log('done');
});
like image 362
Nicekiwi Avatar asked Dec 03 '15 02:12

Nicekiwi


People also ask

How to get data from selected row in DataTable?

It can be useful to provide the user with the option to select rows in a DataTable. This can be done by using a click event to add / remove a class on the table rows. The rows(). data() method can then be used to get the data for the selected rows.

How do I show only 5 entries in DataTables?

There is a option called pageLength . You can set this for show only 5 entries.

How many rows can DataTables handle?

The maximum number of rows that a DataTable can store is 16,777,216.

How do I find DataTables?

Searching on individual columns can be performed using the columns().search() and column().search() methods. DataTables has a built in search algorithm referred to as "smart" searching and is designed to make searching the table data, easy to use for the end user.


1 Answers

API method rows().data() returns Array-like object which is also DataTables API instance.

Use toArray() API method to convert API instance to native Javascript array object.

var rowData = dataTable.rows( { selected: true } ).data().toArray();
like image 53
Gyrocode.com Avatar answered Oct 29 '22 05:10

Gyrocode.com