Quantcast
Channel: Free community support — DataTables forums
Viewing all articles
Browse latest Browse all 35272

Filtering out identical rows

$
0
0

Hi,
I very much a javascript newbie and am really impressed by how quickly I've managed to get a very useful, functional tool using DataTables. So I'm a big fan.
I am using the colVis plugin to allow the user to dynamically hide / show columns. The main purpose of this appeared to me to be to allow the user to get an overview of the information and then to drill down where they want to (by adding in columns to get additional data). For this to work, I need a feature that doesn't appear to be available in colVis - that is to hide any duplicated row.

ie. If you had a table giving a list of car sales with three columns: car model, shop location and customer name, I want to be able to hide the customer name and shop location columns and get a list of distinct rows showing me which car models we've had at least 1 sale for.

I've managed to do this myself using the following bit of code within the $(document).ready function (my datatables object is $('#my_table):

        // When I'm doing this, I'm not updating the 'showing x to y of z entries' text, so that becomes incorrect
        $('#my_table').on( 'draw.dt', function() {
            var seen = {};
            $('table tr').each(function() {
                var txt = $(this).text();
                if (seen[txt])
                    $(this).remove();
                else
                    seen[txt] = true;
            });
        });

So when the table is redrawn, the above gets triggered and duplicate rows are removed.
As my comment indicates, the issue is that this gets applied after datatables has redrawn the table. So if, for example, I am displaying 20 rows per page, and they all happen to be duplicates of the same row, the bit of code above happily gets rid of the duplicates but then I'm left with a page showing just a single row, rather than the 20 distinct rows that I want.
In addition, the fairly useful comment (which gets calculated before I get rid of rows) that states 'Showing x to y of z entries' becomes completely wrong.

Any help / tips on what I could do to get this working would be very much appreciated!


Viewing all articles
Browse latest Browse all 35272

Trending Articles