Solved

Datatables Select All from Filtered Rows

Posted on 2016-09-22
2
84 Views
Last Modified: 2016-09-23
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
Comment
Question by:mcazabonnet
2 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41811010
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
 
LVL 4

Author Closing Comment

by:mcazabonnet
ID: 41812419
Thanks for the assist

M
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question