· 4 years ago · May 20, 2021, 11:42 AM
1$(document).ready(function () {
2 $('#example').DataTable({
3 "footerCallback": function (row, data, start, end, display) {
4 var api = this.api(), data;
5
6 // Remove the formatting to get integer data for summation
7 var intVal = function (i) {
8 return typeof i === 'string' ?
9 i.replace(/[\$,]/g, '') * 1 :
10 typeof i === 'number' ?
11 i : 0;
12 };
13
14 // Total over all pages
15 total = api
16 .column(2)
17 .data()
18 .reduce(function (a, b) {
19 return intVal(a) + intVal(b);
20 }, 0);
21
22 // Total over this page
23 pageTotal = api
24 .column(2, { page: 'current' })
25 .data()
26 .reduce(function (a, b) {
27 return intVal(a) + intVal(b);
28 }, 0);
29
30 // Update footer
31 $(api.column(7).footer()).html(
32 'Total: ' + total + '<br>' + 'Current page: ' + pageTotal
33 );
34 },
35 select: true,
36 dom: 'lfrtipB',
37 buttons: [
38 'copyHtml5',
39 'excelHtml5',
40 'csvHtml5',
41 ],
42 pagingType: 'full_numbers',
43 lengthMenu: [[10, 25, 50, 100, 200, -1], [10, 25, 50, 100, 200, "All"]]
44 });
45
46 $("#example_length").append(' <select autocomplete="off" name="tblEntries" onchange="showChosenEntries(this)"><option href="" selected="">Select a period</option><option href="ajax_view">Last month</option><option href="ajax_view">Last week</option></select>');
47
48});
49
50
51function embedData(xhttp) {
52 if (xhttp.readyState == 4 && xhttp.status == 200) {
53 var responseObject = JSON.parse(xhttp.responseText);
54 var key;
55 var tblContent = '';
56
57 for (var i = 0; i < responseObject.length; i++) {
58
59 tblContent += '<tr class="odd">';
60 var limiti = 0;
61 for (key in responseObject[i]) {
62 limiti++;
63 if (limiti == 9) {
64 break;
65 }
66 tblContent += '<td class="sorting_1">' + responseObject[i][key] + '</td>';
67 }
68 tblContent += '</tr>';
69 }
70 document.getElementById("tblHint").innerHTML = tblContent;
71 }
72}
73
74
75function showChosenEntries(select_element) {
76 if (select_element.children[select_element.selectedIndex].value != "Select a period") {
77 var xhttp = new XMLHttpRequest();
78 xhttp.onreadystatechange = function () {
79 if (this.readyState == 4 && this.status == 200) {
80 embedData(this);
81 }
82 };
83 xhttp.open("GET", select_element.children[select_element.selectedIndex].getAttribute('href'), true);
84 xhttp.send();
85 }
86}
87