5

I have a datatable in which in which I'm trying to get all the checked rows. This table has row grouping and uses a checkbox plugin from gyrocode. I've tried the code listed on the api, but I had no luck. I only get the first record returned, regardless of what is selected. The code I used for the is shown below:

var tbl;
$(document).ready(function (){
          tbl = $('#example').DataTable({
            columnDefs: [{
                targets: 0,
                data: 2,
                'checkboxes': {
                    'selectRow': true
                }
            },
            { "visible": false, "targets": 1 }],
            select: {
                style: 'multi'
            },
            order: [[1, 'asc']],
            iDisplayLength: 10,
            drawCallback: function () {
                var api = this.api();
                var rows = api.rows({ page: 'current' }).nodes();
                var last = null;

                api.column(1, { page: 'current' }).data().each(function (group, i) {
                    if (last !== group) {
                        $(rows).eq(i).before(
                            '<tr class="group"><td colspan="6">' + group + '</td></tr>'
                        );
                        last = group;
                    }
                });
            }
        });
});

function getSelected(){
    alert(tbl.columns().checkboxes.selected().length);
}

I have the code in my jfiddle here. I'm not sure if their is interence between the checkbox and the row grouping? Please let me know where I am going wrong.

Note: The checkbox is based on the plugin by gyrocode The datatables is version 1.10.12

4
  • So what is the problem?Not very clear in your question Commented Mar 7, 2017 at 20:59
  • 2
    It seems to be working actually, if you do console.log(tbl.columns().checkboxes.selected()), you can see that there is an array that contains the unique, selected student ids. If you use different Ids per row, i can see it working. The length property is not what you want. Commented Mar 7, 2017 at 21:01
  • 2
    You need to use column(0).checkboxes.selected() instead. Also you have duplicate ids in the table which affects the total count. Commented Mar 7, 2017 at 21:40
  • That works as well. Thank you for clarifying. Commented Mar 7, 2017 at 22:50

5 Answers 5

13

I'm too late to answer this question. But my answer can help others in the community.

//datatable has to be initialized to a variable
var myTable = $('#calltable').dataTable();

//checkboxes should have a general class to traverse
var rowcollection = myTable.$(".call-checkbox:checked", {"page": "all"});

//Now loop through all the selected checkboxes to perform desired actions
rowcollection.each(function(index,elem){
    //You have access to the current iterating row
    var checkbox_value = $(elem).val();
    //Do something with 'checkbox_value'
});

I hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

this code resolved my issue of not getting all the ids of checked box across all pages. just to state the obvious in above code [.call-checkbox] is a class which needs to be appended when table data generated.
Thanks that really help me, im strugling finding the answer that suits my code, this one help big ups to you
6

I did a quick check and Eric Guan is correct. I'll just post a code snippet:

function getSelected() {
   var selectedIds = tbl.columns().checkboxes.selected()[0];
   console.log(selectedIds)

   selectedIds.forEach(function(selectedId) {
       alert(selectedId);
   });
}

See: https://jsfiddle.net/nwmmbLso/3/

I just noticed you have duplicatie Student Id's which might also cause unexpected behavior from the library you are using. The code provided above should work if the Student Id's are unique.

3 Comments

I'm the author of the mentioned plug-in. Good answer and point about duplicate IDs. However recommended method is to use column(0).checkboxes.selected() if only one column data is accessed, although your code would work too.
It was the [0], that I wasn't aware of. Thank you for clarifying.
See this jsbin for getting row node and all checked rows across all pages of datatable. live.datatables.net/mutavegi/34/edit
4

Working and tested.

      var id = "";
      var oTable = $(".table").dataTable();

      $(".check_quality:checked", oTable.fnGetNodes()).each(function() {
          if (id != "") {
              id = id + "," + $(this).data('id');
          } else {
              id = $(this).data('id');
          }
      });

1 Comment

While this might answer the question, you should edit your answer to include some explanation of how this code answers the question, to provide context to future users. A code block by itself is not immediately useful to those who would come across the same issue later on.
0

Simple answer - use either table.rows( '.selected' ) or table.rows( {selected:true} )

var count = $('#datatable').DataTable().rows( '.selected' ).count();

var checked_rows = $('#datatable').DataTable().rows( '.selected' ).data();

for(var i=0; i<checked_rows.length; i++)
{
  console.log( checked_rows[i] );               
}

Document Link: https://datatables.net/reference/api/count()

Comments

0

for people still looking today for the answer

var rowcollection = table.columns(0).context[0].checkboxes.s.data;

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.