Skip to content

Commit

Permalink
Merge pull request #426 from pi-hole/fix/QuerySorting
Browse files Browse the repository at this point in the history
Fix sorting algorithm on Query Log page
  • Loading branch information
DL6ER authored Mar 3, 2017
2 parents 7c5e1ae + cc5be61 commit 078cbb1
Show file tree
Hide file tree
Showing 4 changed files with 630 additions and 3 deletions.
2 changes: 2 additions & 0 deletions queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,5 @@
?>

<script src="scripts/pi-hole/js/queries.js"></script>
<script src="scripts/vendor/moment.min.js"></script>
<script src="scripts/vendor/datetime-moment.js"></script>
6 changes: 3 additions & 3 deletions scripts/pi-hole/js/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ $(document).ready(function() {
APIstring += "&until="+GETDict["until"];
}

$.fn.dataTable.moment("YYY-MM-DD z HH:MM:SS");

tableApi = $("#all-queries").DataTable( {
"rowCallback": function( row, data, index ){
status = data[4];
Expand All @@ -135,7 +137,7 @@ $(document).ready(function() {
"processing": true,
"order" : [[0, "desc"]],
"columns": [
{ "width" : "20%", "type": "date" },
{ "width" : "20%" },
{ "width" : "10%" },
{ "width" : "40%" },
{ "width" : "10%" },
Expand Down Expand Up @@ -176,5 +178,3 @@ $(document).ready(function() {
tableApi.column(2).search("^"+escapeRegex(GETDict["domain"])+"$",true,false);
}
} );


74 changes: 74 additions & 0 deletions scripts/vendor/datetime-moment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* This plug-in for DataTables represents the ultimate option in extensibility
* for sorting date / time strings correctly. It uses
* [Moment.js](http://momentjs.com) to create automatic type detection and
* sorting plug-ins for DataTables based on a given format. This way, DataTables
* will automatically detect your temporal information and sort it correctly.
*
* For usage instructions, please see the DataTables blog
* post that [introduces it](//datatables.net/blog/2014-12-18).
*
* @name Ultimate Date / Time sorting
* @summary Sort date and time in any format using Moment.js
* @author [Allan Jardine](//datatables.net)
* @depends DataTables 1.10+, Moment.js 1.7+
*
* @example
* $.fn.dataTable.moment( 'HH:mm MMM D, YY' );
* $.fn.dataTable.moment( 'dddd, MMMM Do, YYYY' );
*
* $('#example').DataTable();
*/

(function (factory) {
if (typeof define === "function" && define.amd) {
define(["jquery", "moment", "datatables.net"], factory);
} else {
factory(jQuery, moment);
}
}(function ($, moment) {

$.fn.dataTable.moment = function ( format, locale ) {
var types = $.fn.dataTable.ext.type;

// Add type detection
types.detect.unshift( function ( d ) {
if ( d ) {
// Strip HTML tags and newline characters if possible
if ( d.replace ) {
d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
}

// Strip out surrounding white space
d = $.trim( d );
}

// Null and empty values are acceptable
if ( d === '' || d === null ) {
return 'moment-'+format;
}

return moment( d, format, locale, true ).isValid() ?
'moment-'+format :
null;
} );

// Add sorting method - use an integer for the sorting
types.order[ 'moment-'+format+'-pre' ] = function ( d ) {
if ( d ) {
// Strip HTML tags and newline characters if possible
if ( d.replace ) {
d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
}

// Strip out surrounding white space
d = $.trim( d );
}

return d === '' || d === null ?
-Infinity :
parseInt( moment( d, format, locale, true ).format( 'x' ), 10 );
};
};

}));
Loading

0 comments on commit 078cbb1

Please sign in to comment.