我的问题是我试图在数据表的底部获得一个“总计”行。我得到了这一行,但我总共得到 0 美元。如果我将 api.column(4) 更改为 api.column(3),它会正确添加列,所以我知道它可以工作。
我认为我的问题是我如何获取列索引 4 的数据(项目总数)。我将 data: 设置为 null,因为我通过将成本和数量相乘来呈现数据。所以我不确定为什么 api.column(4).data() 没有得到渲染的信息。
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
processing: true,
seriverSide: true,
ajax: "DataTables-1.10.0/extensions/Editor-1.3.1/examples/php/test.php?PID=<? echo $PID ?>",
table: "#pexpenses",
fields: [ {
label: "Type:",
name: "tbl_pexpenses.type",
type: "select"
}, {
label: "Cost:",
name: "tbl_pexpenses.cost"
}, {
label: "Quantity:",
name: "tbl_pexpenses.quantity"
}, {
label: "Description:",
name: "tbl_pexpenses.description"
}, {
label: "PEID:",
name: "tbl_pexpenses.PEID",
type: "hidden"
}, {
label: "PID:",
name: "tbl_pexpenses.PID",
def: '<? echo $PID; ?>',
type: "hidden"
}
]
} );
$('#pexpenses').DataTable( {
dom: "Tfrtip",
pageLength: -1,
type: 'POST',
paging: false,
info: false,
idSrc: "tbl_pexpenses.PEID",
ajax: "DataTables-1.10.0/extensions/Editor-1.3.1/examples/php/test.php?PID=<? echo $PID ?>",
columns: [
{ data: "tbl_types.type_desc" },
{ data: "tbl_pexpenses.description" },
{ data: "tbl_pexpenses.cost" },
{ data: "tbl_pexpenses.quantity" },
{ data: null,
render: function ( data, type, row ) {
return (data.tbl_pexpenses.cost*data.tbl_pexpenses.quantity);
} }
],
tableTools: {
sRowSelect: "os",
sSwfPath: "../DataTables-1.10.0/extensions/TableTools/swf/copy_csv_xls_pdf.swf",
aButtons: [
{ sExtends: "editor_create", editor: editor },
{ sExtends: "editor_edit", editor: editor },
{ sExtends: "editor_remove", editor: editor },
"print",
{
"sExtends": "collection",
"sButtonText": "Save",
"aButtons": [ "csv", "xls", "pdf" ]}
]
},
"order": [[ 0, 'asc' ]],
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(0, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="grouping" ><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
} );
},
initComplete: function ( settings, json ) {
editor.field( 'tbl_pexpenses.type' ).update( json.tbl_types );
},
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
data = api.column( 4 ).data();
total = data.length ?
data.reduce( function (a, b) {
return intVal(a) + intVal(b);
} ) :
0;
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+ total
);
}
} );
// Order by the grouping
$('#example tbody').on( 'click', 'tr.group', function () {
var currentOrder = table.order()[0];
if ( currentOrder[0] === 0 && currentOrder[0] === 'asc' ) {
table.order( [ 0, 'desc' ] ).draw();
}
else {
table.order( [ 0, 'asc' ] ).draw();
}
} );
<table id="pexpenses" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Type</th>
<th>Description</th>
<th>Cost</th>
<th>Quantity</th>
<th>Item Total</th>
</tr>
</thead>
<tfoot>
<tr>
<td> </td>
<td> </td>
<td> </td>
<th style="text-align:right">Total:</th>
<th></th>
</tr>
<tr>
<th>Type</th>
<th>Description</th>
<th>Cost</th>
<th>Quantity</th>
<th>Item Total</th>
</tr>
</tfoot>