Disabling RowGroup when all the data has the same group

Disabling RowGroup when all the data has the same group

Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10
edited February 2018 in DataTables 1.10

For some of my tables I like to disable the rowGroup feature if all the data falls under the same group.

For example, I might have a page that only shows the employees at the office in London, so it doesn't add anything to show "London" in it's own row and then have every employee listed under it.

Here's how I do it:

.on("init", function() {
    var groups = table.column("[insert column name here]:name").data().unique();
    (groups.length == 1 ? table.rowGroup().disable().draw() : table.rowGroup().enable().draw());
})

This can also be easily modified to disable rowGroup if all the data is unique as well, such as when grouping on employee names, by checking the groups.length against the number of total records.

@allan, 1) I'm biased :-), but I think this would make a good example under the RowGroup extension examples on how to use the API, and 2) there is not RowGroup option in the "Ask a Question" or "New Discussion" Category dropdown.

Replies

  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin

    Thanks for posting this!

    1) Good suggestion - thanks!

    2) Oops - will fix that right now.

    Allan

  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin

    Hmmm - actually no I won't. There seems to be a bug in the forum software stopping me from adding it. its due for an upgrade so I'll get it done then.

    Allan

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

    This has quit working for me a while back and I'm just now getting around to asking about it.

    Again, I want to disable rowGroup if there is only one group.

    Here's the code (table is named "conference_standings_C_038"):

    .on("init", function() {
        var groups = conference_standings_C_038.column("sub_conference:name").data().unique();
        (groups.length == 1 ? conference_standings_C_038.rowGroup().disable().draw() : conference_standings_C_038.rowGroup().enable().draw());
    });
    

    The problem seems to be in the "sub_conference:name" portion since when I swap it out with an integer like
    var groups = conference_standings_C_038.column(4).data().unique();
    it works.

    Has something changed with the ability to use a column name?

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    I think you are not using the correct selector. It should be this:
    .on("init.dt", function() {

    Selecting the column use :name should still work.

    Kevin

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

    Thanks, Kevin. Unfortunately that's not it. I've changed it ti init.dt, but still no luck.

    Also, the init executes as is, but using the :name doesn't work although using an integer will.

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    Are you using columns.name?

    Using :name seems to work in this example:
    http://live.datatables.net/famafejo/1/edit

    Kevin

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10
    edited November 2018

    I'm using column('[insert column name here]:name') (no "s"), which I think should work.

    I've slightly modified your example code (I like your rowGroup disable upfront) and it still works:
    http://live.datatables.net/hetupufe/3/edit

    Here's where I'm trying to use it on my webpage, which looks practically identical to the example with the exception of the table name and the column name:
    https://cfbha.org/w/Special:SeasonHome?view=conferences&season=2018&org=NCAA&div=FBS

    However I get a conference_standings_C_205.column(...).data(...) is undefined where the conference_standings_C_205 is the table name.

    All conferences should have two divisions (referred to as "sub_conference" in the data) except the Big Twelve and the Sun Belt. I'd like the ones with the divisions to show the division name in the row group header while have the row group disabled for the other two.

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769
    edited November 2018

    I looked at the source code and found what I think is the Datatable (onference_standings_C_205) you reference. I might be missing it but I don't see a column defined for sub_conference. I removed all of the config options except columns and rowGroup. I also removed some of the config for the columns themselves. Here is what I see:

    conference_standings_C_205 = $('#conference_standings_C_205').DataTable({
    ....
    "rowGroup": {
        "dataSrc": "sub_conference"
    },
    "columns": [{
        "data": null,
        "name": null,
    ....
    }, {
        "data": "school_name",
        "name": "school_name",
    ....
    }, {
        "data": null,
        "name": null,
    ....
    }, {
        "data": "conference_scored",
        "name": "conference_scored",
    ....
    }, {
        "data": "conference_allowed",
        "name": "conference_allowed",
    ....
    }, {
        "data": null,
        "name": null,
    ....
    }, {
        "data": "scored",
        "name": "scored",
    ....
    }, {
        "data": "allowed",
        "name": "allowed",
    ....
    }, {
        "className": "map-pin",
        "orderable": false,
        "render": function(data, type, row) {
            return '<i class=\"fa fa-map-marker fa-lg fa-fw\" style=\"color:hsl(' + ((row.latitude && row.longitude) ? '240, 100%, 50%' : '0, 0%, 83%') + ')\"></i>'
        }
    }, {
        "data": "coaches",
        "name": "coaches",
    ....
    }, {
        "data": "school_code",
        "name": "school_code",
    ....
    }, {
        "data": "fully_paired",
        "name": "fully_paired",
    ....
    }],
    

    I might be looking at the wrong Datatable.

    Kevin

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

    @kthorngren

    Thanks, Kevin . . . I'm thoroughly embarrassed!

    I have a PHP object that spits out the Datatables code and I must have removed "sub_conference" from the columns without realizing it.

    I've spent all my time troubleshooting the init function and even trying to figure out if the rowGroup extension had changed and I didn't even think to double check the columns.

    Sometimes a fresh set of eyes does wonders!

  • kthorngrenkthorngren Posts: 20,299Questions: 26Answers: 4,769

    Sometimes a fresh set of eyes does wonders!

    Happens to me all the time :smile:

    Kevin

This discussion has been closed.