DataTables - RowGroup extension question about DateTime

DataTables - RowGroup extension question about DateTime

ArnioxArniox Posts: 2Questions: 2Answers: 0

At the moment, I managed to get the RowGroup "plugin" working pretty easily and wanted to group the DateTime section of my table by weekly, or yearly sections. However, it seems to auto group by unique data entry so every single item has been made it's own group out of 10,000+ rows

How do I create RowGroups on DateTime rows that encompass a specific time range?

This is what I currently have and what it looks like (mostly redacted)

$(document).ready(function () {
        var table = $('#currentJobsTable').DataTable({
            order: [[5, 'desc']],
            rowGroup: {
                dataSrc: 5
            }
        });
    });

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    I have tried this but you might be able to use rowGroup.dataSrc as a function. See the comments at the end of the documentation.

    Kevin

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Yep, as Kevin said, here's an example of that.

    Colin

  • imaginetimaginet Posts: 5Questions: 0Answers: 0

    Same problem here, i am seeing duplicates i am grouping by

    rowGroup: {
                    dataSrc: (row) => {
                        return moment(
                            row.date_from.split(" ").shift(),
                            "YYYY-MM-DD"
                        ).format("DD/MM/YYYY");
                    },
                }
    
  • imaginetimaginet Posts: 5Questions: 0Answers: 0

    Tried pre sorting , changing format, same results.

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    edited October 2020

    @imaginet Are you sorting by the date_from column?

    If not that might be why you are seeing duplicates. If this doesn't help then please provide a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • imaginetimaginet Posts: 5Questions: 0Answers: 0

    you have the same bug here
    http://live.datatables.net/ziweyuyu/54/
    It is a similar usecase

  • imaginetimaginet Posts: 5Questions: 0Answers: 0
    $(document).ready( function () {
      var table = $('#example').DataTable({
                    order: [[3, 'asc']],
                rowGroup: {
                    dataSrc: function(row) {
                      console.log(row);
                      return row[4].substr(0, 4);
                    }
                }
      });
    } );
    
    

    this is the change i made, i grouped by Year

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    edited October 2020

    I'm not sure if there are any duplicate years in the data for the test case you linked to. I made the change, `return row[4].substr(0, 4);, to that test case and it seems to be working:
    http://live.datatables.net/zeyesufi/1/edit

    There are no duplicate years.

    In your code snippet you have order: [[3, 'asc']], which is not the date column for this test case.

    Kevin

  • imaginetimaginet Posts: 5Questions: 0Answers: 0

    Thank you so much, you have put me on the right track.
    the issue was as following:

    ...(window.isMobile && {
                rowGroup: {
                    dataSrc: (row) => {
                        return moment(
                            row.date_from.split(" ").shift(),
                            "YYYY-MM-DD"
                        ).format("DD/MM/YYYY");
                    },
                },
            }),
            ...(window.isMobile && {
                order: [[14, "asc"]],
            }),
    
    1. I wasn't sorting by the right column
    2. I was using the fixed sort

    This code works as expected

    Thanks,
    keep up the amazing work

This discussion has been closed.