grouping doesn't work

grouping doesn't work

gib65gib65 Posts: 29Questions: 13Answers: 0

Hello,

I'm trying to implement grouping on my DataTable. I found an example of how to do that here:

https://datatables.net/examples/advanced_init/row_grouping.html

However, it's not working for me.

Here's my code:

[code]

column 1 column 2
A Z
A Z
B Y
B Y
B Y
C X
C X
$(document).ready(function() { var table = $('#JQuery_DataTable').DataTable({ "columnDefs": [ { "visible": false, "targets": 2 } ], "order": [[ 1, 'asc' ]], "displayLength": 25, "drawCallback": function(settings) { var api = this.api(); var rows = api.rows({ page: 'current' }).nodes(); var last = null; api.column(0, { page: 'current' }).data().each(function(group, i) { if (last !== group) { $(rows).eq(i).before( '' + group + '' ); last = group; } }); } }); // Order by the grouping $('#JQuery_DataTable tbody').on('click', 'tr.group', function() { var currentOrder = table.order()[0]; if (currentOrder[0] === 21 && currentOrder[1] === 'asc') { table.order([1, 'desc']).draw(); } else { table.order([1, 'asc']).draw(); } }); }); [/code]

I'm expecting it to group by column 1. So all the A's would be a group, all the B's would be a group, and all the C's would be a group.

The Chrome debugger shows that it's not entering the "drawCallback" event handler. It also shows that it doesn't enter the onClick function when I click anywhere on the table.

Can anyone see what I'm doing wrong? Thanks.

PS - Sorry, I do not have access to my server right and can't upload an example for you to try.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,449Questions: 26Answers: 4,975

    IT would be best if you can build an example for us.

    Also this forum uses different (not [code]) markup to display the code. Here are the instructions:
    https://datatables.net/manual/tech-notes/8

    Kevin

  • gib65gib65 Posts: 29Questions: 13Answers: 0

    Thanks Kevin,

    The best I can do is upload this example zip file. I don't have access to a public server right now. Hope you can do something with this.

  • kthorngrenkthorngren Posts: 21,449Questions: 26Answers: 4,975
    Answer ✓

    Looks like the problem is this config option:
    { "visible": false, "targets": 2 }

    There is no column 2. I think you should see some sort of error in the browser's console. Columns start counting at 0. You probably want to change it to:
    { "visible": false, "targets": 1 }

    BTW, you can quickly post these types of code snippets on the DataTables JS Bin site:
    https://datatables.net/manual/tech-notes/9

    Kevin

  • gib65gib65 Posts: 29Questions: 13Answers: 0

    Cool, thanks Kevin. That worked.

This discussion has been closed.