padolsey-archive / jquery.fn

A collection of jQuery plugins
http://james.padolsey.com
The Unlicense
1.1k stars 717 forks source link

Is there a way to ignore if a table cell has some default value like "--"? #19

Closed vnguyen13 closed 12 years ago

vnguyen13 commented 12 years ago

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;
        });
      });
padolsey commented 12 years ago

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;
vnguyen13 commented 12 years ago

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%.

padolsey commented 12 years ago

Cool, glad it works!