column.render() calculated value
column.render() calculated value
RichJCG
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:
This discussion has been closed.
Answers
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
You don't need or want to use this in
columns.render
:The
actTable.row()
is always getting row 0. Therow-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 likesd = row.mp_act.start_date;
. This should work:Kevin
Works perfect. I appreciate your time in answering.