{"id":3014,"date":"2018-11-01T20:18:48","date_gmt":"2018-11-01T20:18:48","guid":{"rendered":"https:\/\/blog.paranoidprofessor.com\/?p=3014"},"modified":"2018-11-20T08:03:01","modified_gmt":"2018-11-20T08:03:01","slug":"more-fun-with-datatables","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2018\/11\/01\/more-fun-with-datatables\/","title":{"rendered":"more fun with datatables"},"content":{"rendered":"<p>I actually don&#8217;t get a chance to do much GUI development but I did get a short stint on a project that used datatables.\u00a0 It is actually fascinating just how much of a facelift this does give your standard HTML.\u00a0 I did a small example of this in my blog entry <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2018\/09\/02\/software-development-the-easy-way\/\" target=\"_blank\" rel=\"noopener\">software development the easy way<\/a>.<\/p>\n<p>Between normal javascript and datatables it is possible to actually make web applications that have a feel of your standard windows heavy client application.<\/p>\n<p>The only example I have to share today is the ability to do a bit more with datatables.\u00a0 In this case the need was to add totals of the contents of the table as a summary line.\u00a0 This isn&#8217;t so difficult if you already know what the total is but if you do not there is a small bit of code that will do the calculation for you.<\/p>\n<pre>         function intVal ( i ) {\r\n\t\treturn typeof i === 'string' ?\r\n\t\ti.replace(\/[\\$,]\/g, '')*1 :\r\n\t\ttypeof i === 'number' ?\r\n         \t\ti : 0;\r\n            };\r\n\r\n\t$(document).ready(function() {\r\n\t   $('#example').DataTable( {\r\n           \"footerCallback\": function ( row, data, start, end, display ) {\r\n            var api = this.api(), data;\r\n\r\n            var ageTotal = api\r\n         \t.column( 3 )\r\n\t\t.data()\r\n       \t\t.reduce( function (a, b) {\r\n             return intVal(a) + intVal(b);\r\n             }, 0 );\r\n         \r\n             ageTotal = Number(ageTotal).toFixed(2);\r\n             ageTotal = ageTotal.toLocaleString();\r\n         \r\n             $( api.column( 3 ).footer() ).html(Number(ageTotal).toLocaleString());\r\n\t   },\r\n<\/pre>\n<p>If you add this code, actually not much different than many other examples on the internet to your web site then all of a sudden you will see your footer change with the new value(s).<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3015\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2018\/09\/datatables-w-footer.png\" alt=\"\" width=\"984\" height=\"402\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2018\/09\/datatables-w-footer.png 984w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2018\/09\/datatables-w-footer-300x123.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2018\/09\/datatables-w-footer-768x314.png 768w\" sizes=\"(max-width: 984px) 100vw, 984px\" \/><\/p>\n<p>The code is not all that difficult to follow.\u00a0 You can use this particular function to put any values that you wish into the table footer.\u00a0 In my case it was a simple sum, but if you needed some fancy algorithm then this would be the place to put it.<\/p>\n<p>Download source for this example<\/p>\n<div class=\"\"><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/downloads\/datatable-footer-sample.html.gz\" target=\"_self\" class=\"emd_dl_blue\">Download files<\/a><\/div>        <style type=\"text\/css\">\r\n    .emd_dl_blue {\r\n        -moz-box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        -webkit-box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #79bbff), color-stop(1, #378de5) );\r\n        background:-moz-linear-gradient( center top, #79bbff 5%, #378de5 100% );\r\n        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#79bbff', endColorstr='#378de5');\r\n        background-color:#79bbff;\r\n        -webkit-border-top-left-radius:0px;\r\n        -moz-border-radius-topleft:0px;\r\n        border-top-left-radius:0px;\r\n        -webkit-border-top-right-radius:0px;\r\n        -moz-border-radius-topright:0px;\r\n        border-top-right-radius:0px;\r\n        -webkit-border-bottom-right-radius:0px;\r\n        -moz-border-radius-bottomright:0px;\r\n        border-bottom-right-radius:0px;\r\n        -webkit-border-bottom-left-radius:0px;\r\n        -moz-border-radius-bottomleft:0px;\r\n        border-bottom-left-radius:0px;\r\n        text-indent:0;\r\n        border:1px solid #84bbf3;\r\n        display:inline-block;\r\n        color:#ffffff !important;\r\n        font-family:Georgia;\r\n        font-size:15px;\r\n        font-weight:bold;\r\n        font-style:normal;\r\n        height:41px;\r\n        line-height:41px;\r\n        width:153px;\r\n        text-decoration:none;\r\n        text-align:center;\r\n        text-shadow:1px 1px 0px #528ecc;\r\n    }\r\n    .emd_dl_blue:hover {\r\n        background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #378de5), color-stop(1, #79bbff) );\r\n        background:-moz-linear-gradient( center top, #378de5 5%, #79bbff 100% );\r\n        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#378de5', endColorstr='#79bbff');\r\n        background-color:#378de5;\r\n    }.emd_dl_blue:active {\r\n        position:relative;\r\n        top:1px;\r\n    }\r\n    <\/style>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I actually don&#8217;t get a chance to do much GUI development but I did get a short stint on a project that used datatables.\u00a0 It is actually fascinating just how much of a facelift this does give your standard HTML.\u00a0 &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2018\/11\/01\/more-fun-with-datatables\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/3014"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=3014"}],"version-history":[{"count":3,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/3014\/revisions"}],"predecessor-version":[{"id":3018,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/3014\/revisions\/3018"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=3014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=3014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=3014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}