Is there a way to ignore if a table cell has some default value like "--"?
vnguyen13 opened this issue · comments
Vu Nguyen commented
First, thanks for creating this. It's really great. I was wondering if there was a way to ignore values like "--" in a table I'm sorting. Right now the table cells list grades in percentages (99%, 80%, 60%, etc). But for the students that haven't taken the quizzes yet, it displays "--". The sorter uses it as being "higher" than 100%. I want -- to act as if it were a 0.
Right now I have:
$(document).ready(function(){
var table = $('#course_map_table');
$('.sortable_header')
.wrapInner('<span title="Sort this Column"/>')
.each(function(){
var th = $(this), thIndex = th.index(), inverse = false;
th.click(function(){
table.find('td').not('.unsortable').filter(function(){
return $(this).index() === thIndex;
}).sortElements(function(a, b){
return $.text([a]) > $.text([b]) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}, function(){
// parentNode is the element we want to move
return this.parentNode;
});
inverse = !inverse;
});
});
James Padolsey commented
In place of this:
return $.text([a]) > $.text([b]) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
Try this:
var textA = $.text([a]);
var textB = $.text([b]);
if (textA == '--') textA = 100;
if (textB == '--') textB = 100;
return textA > textB ?
inverse ? -1 : 1
: inverse ? 1 : -1;
Vu Nguyen commented
Thanks so much for putting me in the right direction!
I got it to work with a really small change to yours:
var textA = $.text([a]);
var textB = $.text([b]);
if (textA.indexOf("--") != -1) textA = "";
if (textB.indexOf("--") != -1) textB = "";
return textA > textB ?
inverse ? -1 : 1
: inverse ? 1 : -1;
I realized having nothing worked better than using 100%.
James Padolsey commented
Cool, glad it works!