Fahad Khan
Fahad Khan

Reputation: 1635

Show only unique data for a column - jQuery data tables

I am using Data Tables and want to show only unique data for a column day.

This is my table:

#   Day         Count
---------------------
1   Friday      2
2   Friday      2
3   Saturday    4
.   .       .
.   .       .

JS:

var myTable = $('.table').DataTable({
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "oLanguage": {
            "oPaginate": {
                "sPrevious": "←", // This is the link to the previous page
                "sNext": "→", // This is the link to the next page
            }
        }
    });

How can I filter the data to show only unique days data in table ?

I know there is unique() function there but thats for only selecting unique column data from the table, instead I need the (by rebuilding may be) table show the column with unique data.

Thanks for any help.

Upvotes: 0

Views: 3473

Answers (1)

davidkonrad
davidkonrad

Reputation: 85538

You could achieve that by creating a custom row filter :

var day,
    days = [];

$.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
        //return true if iDataIndex already is processed
        if (days[iDataIndex]) return true;
        //process day, return true and insert into days if not already known
        day = aData[1];        
        if (days.indexOf(day)<0) {
           days[iDataIndex]=day;
           return true;
        } else {
           return false;
        }     
    }     
);

demo -> http://jsfiddle.net/4abqjxcu/

Upvotes: 1

Related Questions