Group rows - example requirement attached.

Group rows - example requirement attached.

GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

Is it possible to group rows as per the attached example please?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Glyndwr ,

    Take a look at RowGroup, that should do the trick for you,

    Cheers,

    Colin

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    Hi Colin,

    Thank you. I have read RowGroup and changed my code; however, no grouping is occurring:

    "info": false,
    "scrollY": "200px",
    "scrollCollapse": true,
    "paging": false,
    "pageLength": 30,
    dom: 'Bfrtip',
    buttons: ['copy', 'csv', 'excel', 'pdf', 'print'],
    rowGroup: {
    dataSrc: 'surname'
    },
    columns: [
    { data: null, render: function ( data, type, row ) {
    // Combine the first and last names into a single table field
    return data.surname+', '+data.firstname;
    } },
    {data: 'startDate',
    defaultContent: ""},
    {data: 'type',
    defaultContent: ""},
    {data: 'description',
    defaultContent: ""},
    {data: 'kilometres',
    defaultContent: "0"},
    {data: 'nightsUnderCanvas',
    defaultContent: "0",
    },
    {data: 'nightsInBuilding',
    defaultContent: "0",
    },
    ],
    columnDefs: [ {
    targets: [1],
    render: $.fn.dataTable.render.moment( 'DD/MM/YYYY' )
    } ],

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited November 2018 Answer ✓

    Interesting question. You don't have a columns.data defined to match the dataSrc: 'surname' dataSrc. If you look in the comments section of rowGroup.dataSrc it looks like someone was able to use a function to define the rowGroup.dataSrc. I tried it and it seems to work:
    http://live.datatables.net/jedelezu/1/edit

    Just make the function return the same string you return in columns.render.

    EDIT: I made an update to the columns.render. If the type is display return a blank string. Otherwise for all other types return the concatenated name. This eliminates the name being displayed on all rows.

    Kevin

  • GlyndwrGlyndwr Posts: 128Questions: 35Answers: 1

    Hi Kevin,

    This works beautifully! Thank you. Now to work out how to get my totals to work (the subject of another post).

    Kind regards,

    Glyn

This discussion has been closed.