Solved

Datatables Select All from Filtered Rows

Posted on 2016-09-22
2
52 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 earned 500 total points
Comment Utility
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
Comment Utility
Thanks for the assist

M
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now