Skip to content

Commit

Permalink
before-sort now called during initialization
Browse files Browse the repository at this point in the history
fixes #87
  • Loading branch information
drvic10k committed Oct 20, 2016
1 parent b3f1ff6 commit 83a0154
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Scripts/bootstrap-sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
doSort($header, $sortTable);
}
});
$this.trigger('sorted');
});
}

Expand All @@ -160,9 +159,7 @@
// element is the header of the column to sort (the clicked header)
function sortByColumn(element) {
var $this = $(element), $table = $this.data('sortTable') || $this.closest('table.sortable');
$table.trigger('before-sort');
doSort($this, $table);
$table.trigger('sorted');
}

// Look up sorting data appropriate for the specified table (jQuery element).
Expand Down Expand Up @@ -190,6 +187,8 @@

// Sorting mechanism separated
function doSort($this, $table) {
$table.trigger('before-sort');

var sortColumn = parseFloat($this.attr('data-sortcolumn')),
context = lookupSortContext($table),
bsSort = context.bsSort;
Expand Down Expand Up @@ -276,6 +275,7 @@
$table.find('td.sorted, th.sorted').removeClass('sorted');
rowsToSort.find('td:eq(' + sortColumn + ')').addClass('sorted');
$this.addClass('sorted');
$table.trigger('sorted');
}

// jQuery 1.9 removed this object
Expand Down
34 changes: 34 additions & 0 deletions Tests/eventsRaising.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Sortable Tables</title>
</head>
<body>
<table class='sortable'>
<thead>
<tr>
<th id="h1" data-defaultsort="asc">h1</th>
<th id="h2">h2</th>
</tr>
</thead>
<tbody>
<tr id="az">
<td id="a">a</td>
<td id="z">z</td>
</tr>
<tr id="by">
<td id="b">b</td>
<td id="y">y</td>
</tr>
<tr id="cx">
<td id="c">c</td>
<td id="x">x</td>
</tr>
<tr id="dw">
<td id="d">d</td>
<td id="w">w</td>
</tr>
</tbody>
</table>
</body>
</html>
49 changes: 49 additions & 0 deletions Tests/eventsRaising.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
jasmine.getFixtures().fixturesPath = 'base/Tests/';

describe('Events raising', function () {
beforeEach(function () {
jasmine.getFixtures().load('eventsRaising.html');
});

it("before-sort is raised on initialization", function () {
var table = $('table.sortable');
var wasRaised = false;
table.on('before-sort', function() {
wasRaised = true;
});
$.bootstrapSortable();
expect(wasRaised).toBe(true);
});

it("sorted is raised on initialization", function () {
var table = $('table.sortable');
var wasRaised = false;
table.on('sorted', function () {
wasRaised = true;
});
$.bootstrapSortable();
expect(wasRaised).toBe(true);
});

it("before-sort is raised on manual sorting", function () {
var table = $('table.sortable');
var wasRaised = false;
$.bootstrapSortable();
table.on('before-sort', function () {
wasRaised = true;
});
$.bootstrapSortable({ sortingHeader: $('#h2') });
expect(wasRaised).toBe(true);
});

it("sorted is raised on manual sorting", function () {
var table = $('table.sortable');
var wasRaised = false;
$.bootstrapSortable();
table.on('sorted', function () {
wasRaised = true;
});
$.bootstrapSortable({ sortingHeader: $('#h2') });
expect(wasRaised).toBe(true);
});
})

0 comments on commit 83a0154

Please sign in to comment.