deltreey/Dable

View on GitHub
examples/Sorting.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="x-ua-compatible" content="IE=Edge" >
<meta charset="utf-8">
<script src="../lib/dable.min.js"></script>
<!--Include the Bootstrap CDN-->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id="SortingDable"></div>
<script type="text/javascript">
    var dable = new Dable();
    var data = [
        [ 1, '12/1/12', 'Armadillo', 'First' ],
        [ 2, '12/2/2012', 'Cat', 'Fourth' ],
        [ 3, '1/1/2001', 'Porcupine', 'Second' ],
        [ 4, '2/22/02', 'Bat', 'Third' ] ];
    var columns = [ 'Numbers', 'Dates', 'Text', 'Custom' ];
    dable.SetDataAsRows(data);
    dable.SetColumnNames(columns);
    dable.columnData[3].CustomSortFunc = function(columnIndex, ascending, currentRowObjects) {
        var order = [ 'First', 'Second', 'Third', 'Fourth' ];
        currentRowObjects.sort(function(a, b) {
            var valueA = a.Row[columnIndex];
            var valueB = b.Row[columnIndex];
            return order.indexOf(valueA) - order.indexOf(valueB);
        });
        
        if (!ascending) {
            currentRowObjects.reverse();
        }
        
        return currentRowObjects
    };
    dable.style = 'bootstrap';
    dable.BuildAll("SortingDable");
</script>
</body>
</html>