Collapsing and uncollapsing grouping rows

Collapsing and uncollapsing grouping rows

adambelshawadambelshaw Posts: 24Questions: 4Answers: 1

I'm currently grouping rows slightly similarly to https://www.datatables.net/examples/advanced_init/row_grouping.html.

Currently, when you click a grouping row I add a function to the $.fn.dataTable.ext.search so that rows in that group are filtered out.

I have 2 problems. Firstly, leaving the grouping row in the right place after collapsing. As the grouping rows are added after the filtering, if those rows are filtered out, then it wont be added. Currently, I save the collapsed rows to an array, then show them at the beginning of the table, but it isn't particularly usable.

Secondly, on click I add an anonymous function to the $.fn.dataTable.ext.search, what I'd like to be able to do, is remove it when I click the row again. Unfortunately, indexOf doesn't seem to find it.

Push on click

$.fn.dataTable.ext.search.push(ActivitiesFilter.GroupCollapseFilter(groupName));

Actual filter method

ActivitiesFilter.GroupCollapseFilter = function(groupName){
    return function (settings, searchData, index, rowData, counter) {
        return searchData[1].indexOf(groupName) == -1;           
    };
};

Remove on the next click

$.fn.dataTable.ext.search.splice($.fn.dataTable.ext.search.indexOf(ActivitiesFilter.GroupCollapseFilter(groupName)), 1);

Anyone have any better ideas for doing this? I've thought of removing the rows from the .aiDisplay by hand in the drawCallback. But I would think there's a better way to do it than that.

I can provide code or debug information if it will help, but haven't managed to get an example on the live site yet.

This question has an accepted answers - jump to answer

Answers

  • adambelshawadambelshaw Posts: 24Questions: 4Answers: 1

    I've made a quick example on the live.datatables site: http://live.datatables.net/nozofiko/3/edit

    It's not 100% the same, but it should illustrate the problems.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    I'm not sure that there is going to be a good way of doing this in DataTables to be honest. It hasn't been designed for this, so any approach taken is always going to be a bit hacky.

    The best I can really suggest is to include a grouping row in your data set - one that will never be filtered out. Otherwise you are going to end up with some potentially complex logic in order to decide if a grouping show should be shown or not.

    Sorry I don't have a better answer.

    Allan

This discussion has been closed.