dding a Row-Total Column?
dding a Row-Total Column?
Need to make Column(total) for Sum 3 Column(No.1, No.2, No.3)
Like this table
No1 --- No2 --- No3 --- Total
4 ------ 2 ------ 4 ------ 10
6 ------ 9 ------ 7 ------ 22
1 ------ 2 ------ 4 ------ 7
Need to know how I can sum this 3 column in on column if I can fetch it in SQL it will be good or make it in Javascript
This is my code
<script type="text/javascript" language="javascript" class="init">
var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax:
{
url: "examples/php/subjects.php?exam=<?php echo $examid ?>",
type: "POST",
},
table: "#example",
fields: [ {
label: "Name Subject:",
name: "subjects.Name"
}, {
label: "Name Class:",
name: "subjects.C_ID",
type: "select",
placeholder: "Select a Class"
}, {
label: "Name Exam:",
name: "subjects.ExamID",
type: "select",
placeholder: "Select a exam",
"default": <?php echo $_GET['exam'] ?>
}, {
label: "No1:",
name: "subjects.Exam1"
},
{
label: "No2:",
name: "subjects.Exam2"
},
{
label: "اNo3:",
name: "subjects.Exam3"
}
]
} );
var table = $('#example').DataTable( {
responsive: true,
dom: "Bfrtip",
"pageLength": 100,
ajax:
{
url: "examples/php/subjects.php",
type: "GET",
data: {
"exam": <?php echo $examid ?> },
},
ajaxUrl: "examples/php/subjects.php",
columns: [
{
data: null,
defaultContent: '',
className: 'select-checkbox',
orderable: false
},
{ data: "subjects.Name" },
{ data: "classes.Name" },
{ data: "subjects.Exam1" },
{ data: "subjects.Exam2" },
{ data: "subjects.Exam3" },
{ data: "subjects.Exam3" },
{ data: Need sum here [Exam1, Exam2, Exam3] },
],
autoFill: {
columns: [1],
editor: editor
},
keys: {
columns: [1],
editor: editor,
},
select: {
style: 'os',
selector: 'td:first-child',
blurable: true
},
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
]
} );
} );
</script>
{ data: "subjects.Name" },
{ data: "classes.Name" },
{ data: "subjects.Exam1" },
{ data: "subjects.Exam2" },
{ data: "subjects.Exam3" },
{ data: "subjects.Exam3" },
{ data: Need sum here [Exam1, Exam2, Exam3] },
Thank you
This question has an accepted answers - jump to answer
Answers
up
I would suggest using
columns.render
to sum the row data.Kevin
"columnDefs": [
{
this output not sum
output
Exam1 = 5
Exam2 = 10
Total = 510
They are being added together as strings. You will need to use parseInt to convert to
row.subjects.Exam1
androw.subjects.Exam2
to integers before adding them.Kevin
Solved
return (+row.subjects.Exam1 + +row.subjects.Exam2 + +row.subjects.Exam3)
Thank you kthorngren