column.render() calculated value

column.render() calculated value

RichJCGRichJCG Posts: 12Questions: 3Answers: 0

I'm attempting to calculate total number of working days between two dates using moment.js and column.render().

It's working fine for the first row but then copies that same value to all following rows. I think I've looked at it too long and missing something simple.

Any help is greatly appreciated.

    var actTable = $('#activity').DataTable({
        paging:true,
        ajax: {
            url: '".site_url()."ajax/mp_acts',
            type: 'POST',
            data: function(d) {
                d.pid = ".$pid.";
            }
        },
        rowId: 'mp_act.aid',
        columns: [
        {data: 'mp_act.name'},
        {data: 'mp_atype.name', editField: 'mp_act.atid'},
        {data: 'mp_act.start_date'},
        {data: 'mp_act.end_date'},
        {data: null, render: function (data, type, row)
            {
                sd = actTable.row().data().mp_act.start_date;
                ed = actTable.row().data().mp_act.end_date;
                start = moment(sd);
                end = moment(ed);
                numdays = 0;
                while(start <= end) {
                    if(start.isoWeekday()!=6 && start.isoWeekday()!=7)
                    {numdays++;}
                    start.add(1, 'days');
                }
                return numdays;
                
            }},
        {data: 'mp_act.manhours'}
        ],
        orderFixed: [[1, 'asc']], 
        select: {style: 'single'},
        dom: 'Blpfr<t>i',
        buttons: [
            {
                extend: 'create',
                editor: actEditor
            },
            'excel'
        ]
    });

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓
                sd = actTable.row().data().mp_act.start_date;
                ed = actTable.row().data().mp_act.end_date;
    

    Yep, this will always get the first row as row() is being called without any parameters, so defaults to the first row.

    You can use third parameter passed into columns.render, as that is all the data for that row,

    Colin

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

    You don't need or want to use this in columns.render:

    actTable.row().data().mp_act.start_date;
    

    The actTable.row() is always getting row 0. The row-selector states that a null values returns row 0. So yes you are right that all the rows will have the same calculated value from row 0.

    Instead use the row parameter of the function to access the rows data, something like sd = row.mp_act.start_date;. This should work:

        {data: null, render: function (data, type, row)
            {
                sd = row.mp_act.start_date;
                ed = row.mp_act.end_date;
                start = moment(sd);
                end = moment(ed);
                numdays = 0;
                while(start <= end) {
                    if(start.isoWeekday()!=6 && start.isoWeekday()!=7)
                    {numdays++;}
                    start.add(1, 'days');
                }
                return numdays;
                 
            }},
    

    Kevin

  • RichJCGRichJCG Posts: 12Questions: 3Answers: 0

    Works perfect. I appreciate your time in answering.

This discussion has been closed.