Group rows - example requirement attached.

Group rows - example requirement attached.

GlyndwrGlyndwr Posts: 117Questions: 32Answers: 0

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,146Questions: 1Answers: 2,586

    Hi @Glyndwr ,

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

    Cheers,

    Colin

  • GlyndwrGlyndwr Posts: 117Questions: 32Answers: 0

    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: 20,322Questions: 26Answers: 4,773
    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: 117Questions: 32Answers: 0

    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.