Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Datatables Select All from Filtered Rows

Hi,
I'm trying to get the items selected from the filtered rows

I've been reviewing this https://www.datatables.net/forums/discussion/comment/47085
with no luck..

The datatable buttons are set as follows

buttons: [
                    'selectAll',
                    'selectNone'
                ],
                select: true,
                select: {
                    style: 'multi',
                    selector: 'td:not(.no-clickable)'
                },

From a function I try to process the selected rows

 var myTable = $('#RequestsTable').DataTable();
       
        var data = "";
        var data = myTable.rows({ selected: true }).data();

I'm missing something here.....
0
Mauro Cazabonnet
Asked:
Mauro Cazabonnet
1 Solution
 
zephyr_hex (Megan)DeveloperCommented:
I would add a class to a row when it's clicked or selected, and then look for those rows when you want to process the selected rows.  Here's an example:

https://jsfiddle.net/zephyr_hex/vLx2tup3/1/

HTML
<input id="button" type="button" value="Count of Rows">
<table>
  <thead>
    <tr>
      <th>Rendering engine</th>
      <th>Browser</th>
      <th>Platform(s)</th>
      <th>Engine version</th>
      <th>CSS grade</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 4.0</td>
      <td>Win 95+</td>
      <td> 4</td>
      <td>X</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 5.0</td>
      <td>Win 95+</td>
      <td>5</td>
      <td>C</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 5.5</td>
      <td>Win 95+</td>
      <td>5.5</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 6</td>
      <td>Win 98+</td>
      <td>6</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 7</td>
      <td>Win XP SP2+</td>
      <td>7</td>
      <td>A</td>
    </tr>
  </tbody>
</table>

Open in new window


jQuery
var dt = $('table').DataTable();


$('table tbody').on('click', 'tr', function() {
  $(this).toggleClass('selected');
});

$('#button').click(function() {
  $.each(dt.rows('.selected').data(), function(key, value) {
    console.log(key);  //F12 console to see the result
    console.log(value);
  });
  alert(dt.rows('.selected').data().length + ' row(s) selected');
});

Open in new window

0
 
Mauro CazabonnetSenior Software EngineerAuthor Commented:
Thanks for the assist

M
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now