trying to work with RowGroup extension and especially with Custom row rendering / aggregates example
trying to work with RowGroup extension and especially with Custom row rendering / aggregates example
Pliachas Paschalis
Posts: 68Questions: 13Answers: 1
in DataTables
Here is my code:
var Kalliertable = $('#FKall').DataTable({
ajax: {
url: 'php/table.KallierFromCust.php',
type: 'post',
data: function(d) {
var selected = custtable.row({
selected: true
});
if (selected.any()) {
d.Cust = selected.data().Id;
}
}
},
order: [
[6, 'asc']
],
rowGroup: {
startRender: null,
endRender: function(rows, group) {
var ageAvg = rows
.data()
.pluck(4)
.reduce( function (a, b) {
return a + b*1;
}, 0) / rows.count();
var salaryAvg = rows
.data()
.pluck(5)
.reduce( function (a, b) {
return a + b.replace(/[^\d]/g, '')*1;
}, 0) / rows.count();
salaryAvg = $.fn.dataTable.render.number(',', '.', 0, '$').display( salaryAvg );
return $('<tr/>')
.append( '<td colspan="3">Averages for '+group+'</td>' )
.append( '<td>'+ageAvg.toFixed(0)+'</td>' )
.append( '<td/>' )
.append( '<td>'+salaryAvg+'</td>' );
},
dataSrc: 'Kallier.kindkall'
},
columns: [{
"data": "Kallier.id"
},
{
"data": "Kallier.codefiel"
},
{
"data": "Field.codeklhr",
render: function(toFormat) {
var tKliros;
tKliros = toFormat.toString();
tKliros = tKliros.substring(0, 1) + '-' + tKliros.substring(1, 4) + '/' + tKliros.substring(4, 10);
return tKliros
}
},
{
data: null,
render: function(data, type, row) {
// Combine the first and last names into a single table field
return data.Cust.custepon + ' ' + data.Cust.custname;
}
},
{
"data": "Kallier.ektashka"
},
{
"data": "Kallier.kallxrev"
},
{
"data": "Kallier.kindkall"
},
{
"data": "Kallier.codecust",
"visible": false
}
],
select: true,
lengthChange: true,
buttons: [
],
language: {
thousands: ".",
decimal: ",",
processing: "Traitement en cours...",
search: "Αναζήτηση :",
lengthMenu: "Εμφάνιση _MENU_ εγγραφών",
info: "Εμφάνιση _START_ Από _END_ από σύνολο _TOTAL_ εγγραφών",
infoEmpty: "Εμφάνιση 0 Από 0 από σύνολο 0 εγγραφών",
infoFiltered: "(Ευρεθείσες εγγραφές από _MAX_ συνολικά)",
infoPostFix: "",
loadingRecords: "Φόρτωση Δεδομένων...",
zeroRecords: "Δεν υπάρχουν εγγραφές!",
emptyTable: "Δεν υπάρχουν εγγραφές σε αυτόν τον πίνακα!",
paginate: {
first: "Πρώτη",
previous: "Προηγούμενη",
next: "Επόμενη",
last: "Τελευταία"
},
aria: {
sortAscending: ": activer pour trier la colonne par ordre croissant",
sortDescending: ": activer pour trier la colonne par ordre décroissant"
}
},
"footerCallback": function(row, data, start, end, display) {
var api = this.api(),
data;
// Remove the formatting to get integer data for summation
var intVal = function(i) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '') * 1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.column(4)
.data()
.reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
// Total over this page
pageTotal = api
.column(4, {
page: 'current'
})
.data()
.reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer
var num = $.fn.dataTable.render.number(',', '.', 4, '').display(pageTotal);
var numT = $.fn.dataTable.render.number(',', '.', 4, '').display(total);
$(api.column(2).footer()).html(
' ' + num + ' ( ' + numT + ' Σύνολο)'
);
}
});
i get this error:
Uncaught TypeError: Cannot read property 'replace' of undefined in line ... where i have this part of code:
return a + b.replace(/[^\d]/g, '')*1;
My data:
{"data":[{"DT_RowId":"row_1542","Kallier":{"id":"1542","codefiel":"1551","owner":"2335","ektashka":"6.250","kallxrev":"281.00","kindkall":"\u0394\u0395\u039d\u0394\u03a1\u0391 \u0394\u0399\u0391\u03a6\u039f\u03a1\u0391","codecust":"9"},"Field":{"codeklhr":"00011551"},"Cust":{"custepon":"\u0392\u039b\u0391\u03a7\u039f\u03a3 \u039d\u0395\u039d\u039f\u0393\u039b\u039f\u03a5","custname":"\u03a3\u03a4\u0395\u03a1\u0393\u0399\u039f\u03a3 \u0391\u03a1\u0399\u03a3\u03a4\u0395"}},{"DT_RowId":"row_1556","Kallier":{"id":"1556","codefiel":"1565","owner":"1142","ektashka":"4.003","kallxrev":"180.00","kindkall":"\u0394\u0395\u039d\u0394\u03a1\u0391 \u039f\u03a0\u03a9\u03a1\u039f\u03a6\u039f\u03a1\u0391","codecust":"9"},"Field":{"codeklhr":"00011565"},"Cust":{"custepon":"\u0398\u0395\u039f\u0394\u03a9\u03a1\u0399\u0394\u0397\u03a3","custname":"\u0391\u039d\u03a4\u03a9\u039d\u0399\u039f\u03a3"}},{"DT_RowId":"row_2352","Kallier":{"id":"2352","codefiel":"2362","owner":"1730","ektashka":"5.250","kallxrev":"236.00","kindkall":"\u0394\u0395\u039d\u0394\u03a1\u0391 \u039f\u03a0\u03a9\u03a1\u039f\u03a6\u039f\u03a1\u0391","codecust":"9"},"Field":{"codeklhr":"000213"},"Cust":{"custepon":"\u0392\u039b\u0391\u03a7\u039f\u03a5","custname":"\u03a3\u03a4\u0391\u039c\u0391\u03a4\u0399\u0391"}},{"DT_RowId":"row_2913","Kallier":{"id":"2913","codefiel":"1551","owner":"2335","ektashka":"0.636","kallxrev":"28.00","kindkall":"\u0394\u0395\u039d\u0394\u03a1\u0391 \u0394\u0399\u0391\u03a6\u039f\u03a1\u0391","codecust":"9"},"Field":{"codeklhr":"00011551"},"Cust":{"custepon":"\u0392\u039b\u0391\u03a7\u039f\u03a3 \u039d\u0395\u039d\u039f\u0393\u039b\u039f\u03a5","custname":"\u03a3\u03a4\u0395\u03a1\u0393\u0399\u039f\u03a3 \u0391\u03a1\u0399\u03a3\u03a4\u0395"}},{"DT_RowId":"row_3087","Kallier":{"id":"3087","codefiel":"2935","owner":"2231","ektashka":"2.300","kallxrev":"75.00","kindkall":"\u0394\u0395\u039d\u0394\u03a1\u0391 \u039c\u0399\u039a\u03a1\u0391","codecust":"9"},"Field":{"codeklhr":"10018"},"Cust":{"custepon":"\u03a4\u03a1\u0399\u0392\u0399\u0391\u0397\u03a3","custname":"\u0394\u0397\u039c\u0397\u03a4\u03a1\u0399\u039f\u03a3"}},{"DT_RowId":"row_4346","Kallier":{"id":"4346","codefiel":"1551","owner":"2335","ektashka":"1.500","kallxrev":"10.00","kindkall":"\u03a0\u0391\u0393\u0399\u039f","codecust":"9"},"Field":{"codeklhr":"00011551"},"Cust":{"custepon":"\u0392\u039b\u0391\u03a7\u039f\u03a3 \u039d\u0395\u039d\u039f\u0393\u039b\u039f\u03a5","custname":"\u03a3\u03a4\u0395\u03a1\u0393\u0399\u039f\u03a3 \u0391\u03a1\u0399\u03a3\u03a4\u0395"}}],"options": ...
etc.
As i can see the function doesn't work in data as '2.300' or '75.00'.
How can i fix this?
Thanks
I have to say that the results in footer are Ok
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
There is no
5
in your row's data objects. Use the name of the property - for example.pluck('Kallier').pluck('kallxrev')
.Note the double pluck for the nested data. The
pluck
documentation has a little more information about this.Allan
Ok. Thanks Allan