2012-05-13 11 views
1

最近私はhelp getting the correct Date format on my jQuery/tablesorter tableでした。クライアントはテーブルに企業を掲載していて、カラムの1つを日付別にソートする必要があり、そのうちのいくつかはポートフォリオ内の「アクティブな」企業であり、他は出庫したもので、日付フォーマットを使用するものです。私が持っている問題は、これらがすべて同じテーブルにあり、日付の列が日付順にソートされているため、「アクティブ」としてリストされている企業はリストの先頭または末尾にグループ化されますが、注文。日付列にテキスト文字列を追加し、Tablesorterでソート順を調整する方法

ので、企業は、現在「アクティブ」>「アクティブ」>「アクティブ」> 2006年1月> 2010年3月> 2012年2月、などのように

をソートしている私は追加する方法があるかどうかを確認したいと思いますこれをパーサに入力すると、 'アクティブ'文字列が現在の日付に変換され、会社は次のようにソートされます。 'アクティブ'> 'アクティブ'> 'アクティブ'> 2012年3月> 2010年1月>

これは可能ですか?何か案は?私は、以前の新しい問題である新しい日付のフォーマットを取得するだけだったので、これについて新しいスレッドを開始することにしました。私は以下のHTMLとコードを投稿しています。前もって感謝します。

<table id="myTable" class="tablesorter stripeMe sample" width="100%" cellpadding="0" cellspacing="0"> 
<thead> 
<th width="30%" align="left">COMPANY</th><th width="35%">DESCRIPTION</th><th width="17%" align="left">INDUSTRY</th><th width="18%" align="left">EXIT DATE</th></tr></thead> 
<tbody> 
<tr><td width="30%"> Cartera Commerce, Inc. </td> 
<td width="35%">Provides technology-enabled marketing and loyalty solutions 
</td><td width="17%"> Financials </td> <td width="18%">Active</td> 
</tr><tr><td width="30%"> Critical Information Network, LLC </td> 
<td width="35%">Operates library of industrial professional training and certification materials 
</td><td width="17%"> Education </td> <td width="18%">Active</td> 
</tr><tr><td width="30%"> Cynergydata </td> 
<td width="35%">Provides merchant payment processing services and related software products 
</td><td width="17%"> Merchant Processing </td> <td width="18%">Active</td> 
</tr><tr><td width="30%"> </td> 
<td width="35%">Operates post-secondary schools 
</td><td width="17%"> Education </td> <td width="18%">Active</td> 
</tr><tr><td width="30%"> CorVu Corporation</td> 
<td width="35%">Develops and distributes performance management software products and related services 
</td><td width="17%"> Information Technology </td> <td width="18%">May 2007</td> 
</tr><tr><td width="30%"> Fischer Imaging Corporation </td> 
<td width="35%">Manufactures and services specialty digital imaging systems and other medical devices 
</td><td width="17%"> Healthcare </td> <td width="18%">Sep 2005</td> 
</tr><tr><td width="30%"> Global Transportation Services, Inc. </td> 
<td width="35%">Provides air and ocean transportation and logistics services 
</td><td width="17%"> Transportation  </td> <td width="18%">Mar 2010</td> 
</tr><tr><td width="30%"> HMP/Rita </td> 
<td width="35%">Operates as a specialty medical device company that manufactures and markets vascular and spinal access systems 
</td><td width="17%"> Healthcare </td> <td width="18%">Dec 2006</td> 
</tr> 

</tbody> 
</table> 

と現在のjQueryのパーサ:それは実際には簡単な修正です

$.tablesorter.addParser({ 
id: 'monthYear', 
is: function(s) { 
    return false; 
}, 
format: function(s) { 
    var date = s.match(/^(\w{3})[ ](\d{4})$/); 
    var m = date ? date[1] + ' 1 ' || '' : ''; 
    var y = date && date[2] ? date[2] || '' : ''; 
    return new Date(m + y).getTime() || ''; 
}, 
type: 'Numeric' 
}); 

$('#myTable').tablesorter({ 

headers: { 
    3: { 
     sorter: 'monthYear' 
    } 
} 

}); 

答えて

0

var today = new Date().getTime(); 

$.tablesorter.addParser({ 
    id: 'monthYear', 
    is: function(s) { 
     return false; 
    }, 
    format: function(s) { 
     // remove extra spacing 
     s = $.trim(s.replace(/\s+/g, ' ')); 
     // process date 
     var date = s.match(/^(\w{3})[ ](\d{4})$/), 
      m = date ? date[1] + ' 1 ' || '' : '', 
      y = date && date[2] ? date[2] || '' : ''; 
     return /active/i.test(s) ? today : new Date(m + y).getTime() || ''; 
    }, 
    type: 'Numeric' 
}); 

表のセルで「アクティブ」という語は、大文字と小文字を区別しません。ここにはdemo of it workingがあります。将来の日付、この場合の2012年7月は、「アクティブ」より上にソートされることに注意してください。

+0

絶対に華麗です!本当にありがとう、それは魅力のように働いた - 私はあなたに必然的に気をつけたくないので新しい質問を投稿しましたが、あなたは本当に私のために来ました - ありがとう! – seanx

+0

これは面倒ではありません! :) – Mottie

関連する問題