row grouping on year

row grouping on year

crush123crush123 Posts: 417Questions: 126Answers: 18

I have a table of events, each of which has a date (yyyy-mm-dd) associated with it

I would like to apply row grouping, as used in the 'drawCallback' example, but would like to group by year only

I can utilise the date column twice from my ajax source and render one of the date columns to display as a year, but the grouping is not affected

This question has an accepted answers - jump to answer

Answers

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited November 2015 Answer ✓

    worked it out

    in my first date column, i returned the year as a substring

    { data: "tbleventarchive.EventDate", 
            visible: false,
            render: function ( data, type, row ) {
                var year = data.substr(data.length-4)
                return year;
            } },
    

    then I changed drawCallback to

    api.column(0, {page:'current'} ).data().each( function ( group, i ) {
                group = (group.substr(group.length-4));
                if ( last !== group ) {
                    $(rows).eq( i ).before(
                        '<tr class="group"><td colspan="5">'+group+'</td></tr>'
                    );
    
                    last = group;
                }
            } );
    

    UPDATE

    Better still, for my case there was no need to include the date column twice (I would be hiding the year column anyway) and as i am using the moment library on the page, rather than using a substring, I am better setting the group value in the callback with ...

    group = moment(group).format("YYYY")
    

    so i could potentially format the group to include quarter or month or whatever. ;-)

    eg

    group = moment(group).format(" YYYY [(Quarter] Q)");
    
This discussion has been closed.