2013-02-19 73 views
1

我參加了一個領先形式這篇文章(jQuery table sort) - 在試圖做什麼Im做...jQuery的排序表

的代碼是在這裏。 http://jsfiddle.net/daqGC/

它不工作...任何想法

這裏的js代碼。 HTML可以在jsFiddle上找到。

var user_table = $('#users'); 
$('#company_header, #user_header, #email_header, #type_header') 
    .wrapInner('<span title="sort this column"/>') 
    .each(function(){ 

     var th = $(this), 
      thIndex = th.index(), 
      inverse = false; 

     th.click(function(){ 

      user_table.find('td').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; 

     }); 

    }); 

THx的

回答

1

你是你得到一個JavaScript錯誤的問題:Uncaught TypeError: Object [object Object] has no method 'sortElements'

您需要包括外部庫:https://github.com/padolsey/jQuery-Plugins/blob/master/sortElements/jquery.sortElements.js

所以,你需要做的是,下載在.js文件,並引用它的頁面after jQuery的腳本引用,例如在:

<script type="text/javascript" src="https://raw.github.com/padolsey/jQuery-Plugins/master/sortElements/jquery.sortElements.js"></script> 

看到你的jsfiddle固定在這裏:http://jsfiddle.net/daqGC/3/

+0

沒有firmiliar外部室內用林達,我只是鏈接到它jQuery的引用後?或者我下載它並將其粘貼到我的jquery文件中? – jpmyob 2013-02-19 09:30:12

+0

是的,看我的編輯。 – Tsar 2013-02-19 11:27:00

+0

THX - 現在開始工作 - 非常感謝 – jpmyob 2013-02-19 23:07:10

0

添加由數字和字符串排序,除了使用jQuery的UI可以提高接口:

$('#cuerpoTabla thead tr th.string, #cuerpoTabla thead tr th.numerico') 
    .wrapInner('<span title="Ordenar esta columna"/>') 
    .each(function() 
    { 
     var th = $(this); 
     //$('<i class="ui-icon ui-icon-caret-2-n-s"></i>').insertAfter(th); 
     th.css("cursor","pointer"); 
     th.append('<i class="ui-icon ui-icon-caret-2-n-s"></i>'); 
     var thIndex = th.index(); 
     var inverse = false; 
     th.click(function() 
     { 
      th.siblings().find("i.ui-icon").removeClass("ui-icon-caret-1-n").removeClass("ui-icon-caret-1-s").addClass("ui-icon-caret-2-n-s"); 
      th.find("i.ui-icon").removeClass("ui-icon-caret-2-n-s"); 
      if (inverse) { 
       th.find("i.ui-icon").removeClass("ui-icon-caret-1-n"); 
       th.find("i.ui-icon").addClass("ui-icon-caret-1-s"); 
      } 
      else 
      { 
       th.find("i.ui-icon").removeClass("ui-icon-caret-1-s"); 
       th.find("i.ui-icon").addClass("ui-icon-caret-1-n"); 
      } 
      $("#cuerpoTabla").find('td').filter(function() 
      { 
       return $(this).index() === thIndex; 

      }).sortElements(function(a, b) 
      { 
       if($.isNumeric($.text([a]))) 
       { 
        x = $.text([a]); 
        y = $.text([b]); 
        return (eval(x) > eval(y)) ?inverse ? -1 : 1: inverse ? 1 : -1; 
       }else 
       { 
        return $.text([a]) > $.text([b]) ?inverse ? -1 : 1: inverse ? 1 : -1; 
       } 

      }, function() 
      { 
       return this.parentNode; 
      }); 
      inverse = !inverse; 
     }); 

    });