Last active
August 29, 2015 14:21
-
-
Save iampeterbanjo/c3fbb60f31d2d7c6e50f to your computer and use it in GitHub Desktop.
Number sort for dynatable.js (www.dynatable.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sort numberAges for the dynatable | |
var dynatableSettings = { | |
dataset: { | |
records: jsonTable | |
, sortTypes: { | |
'%': 'numberSort' | |
, 'Total': 'numberSort' | |
} | |
} | |
, features: { | |
paginate: false | |
, search: false | |
, recordCount: false | |
, perPageSelect: false | |
, pushState: false | |
} | |
, table: { | |
defaultColumnIdStyle: 'trimDash' | |
} | |
}; | |
function getComparison(a, b) { | |
return (a * 1) - (b * 1); | |
} | |
function getNumber(s) { | |
return /[0-9]+/.exec(s); | |
} | |
function numberSort(a, b, attr, direction) { | |
var numberA = getNumber(a[attr]) | |
, numberB = getNumber(b[attr]) | |
, comparison = getComparison(numberA, numberB); | |
return direction > 0 ? comparison : -comparison; | |
} | |
$('body').delegate('table', 'dynatable:init', function (event, dynatable) { | |
dynatable.sorts.functions['numberSort'] = numberSort; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment