2014-07-10 335 views
0

這是我的HTML代碼。tablesorter()dd-MMM-yyyy格式的jQuery函數

此代碼不適用於日期格式,而是按字符串格式進行排序。

我還提到了jquery tablesorter sort date dd mmm yyyy之類的一篇文章。

但是對於這種格式它不起作用。

我使用像

^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$ 

仍然無法正常使用正則表達式。請幫我

<html> 
<head><title> 

</title><link href="themes/blue/style.css" rel="stylesheet" /><link  href="themes/green/style.css" rel="stylesheet" /> 
<script src="jQuery/jquery-1.9.0.min.js" type="text/javascript"></script> 

<script src="jQuery/jquery.tablesorter.js" type="text/javascript"></script> 
<script src="jQuery/TableSorter.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#GrdViewEmployee").tablesorter({ 
      sortList: [[0, 0], [2, 1]], 
      widgets:'zebra' 
     }); 
    }); 
</script> 
</head> 
<body class="tablesorterBlue"> 
<form method="post" action="WebForm1.aspx" id="form1"> 
<div class="aspNetHidden"> 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="fauQ7GKDqTon5mt7NHJnOCzVzfTVHWHj+gG3j+CA8mTf4JJVPho0PBzFatn/hz/0xu7X0+jfUVQHlCgPQh5CNnoHCyzZOnTqgr7nSstUfCj5JlrZkhV7468h3Vx1e7Er" /> 
</div> 

    <div> 
<table cellspacing="0" rules="all" class="tablesorterBlue" border="1" id="GrdViewEmployee" style="border-collapse:collapse;"> 
    <thead> 
     <tr> 
      <th scope="col">empId</th><th scope="col">empName</th><th scope="col">empEMail</th><th scope="col">empPhone</th> 
     </tr> 
    </thead><tbody> 
     <tr> 
      <td>1</td><td>rameshwar</td><td>[email protected]</td><td>2-Aug-2013</td> 
     </tr><tr> 
      <td>2</td><td>shrivatsav</td><td>[email protected]</td><td>27-Aug-2013</td> 
     </tr><tr> 
      <td>3</td><td>ganga</td><td>[email protected]</td><td>14-Jan-2013</td> 
     </tr><tr> 
      <td>4</td><td>krishna</td><td>[email protected]</td><td>13-Jan-2014</td> 
     </tr><tr> 
      <td>5</td><td>mahesh</td><td>[email protected]</td><td>30-Nov-2013</td> 
     </tr><tr> 
      <td>6</td><td>Shridhar</td><td>[email protected]</td><td>1-Dec-2013</td> 
     </tr> 
    </tbody><tfoot> 

    </tfoot> 
</table> 
</div> 
</form> 
</body> 
</html> 
+0

我不確定你運行的是什麼版本,但是這可能有幫助:http://mottie.github.io/ tablesorter/docs/example-option-date-format.html – entropic

+0

請參閱[this](http://drumcoder.co.uk/blog/2010/mar/15/jquery-tablesorter-and-dates/)博客,希望它幫助:) – Aravind

回答

2

您可以添加自己的解析器:

monthDict = { 
    'Jan': '01','Feb': '02','Mar': '03', 
    'Apr': '04','May': '05','Jun': '06', 
    'Jul': '07','Aug': '08','Sep': '09', 
    'Oct': '10','Nov': '11','Dec': '12' 
} 
$.tablesorter.addParser({ 
    id: 'mydate', 
    is: function(s) {return false}, 
    format: function(s) { 
     var date = s.split('-'); 
     var date[0] = (date[0].length == 1) ? '0' + date[0] : date[0]; 
     return date[2] + monthDict[date[1]] + date[0]; 
    }, 
    type: 'numeric' 
}); 
$("#GrdViewEmployee").tablesorter({ 
    sortList: [[0, 0], [2, 1]], 
    widgets: 'zebra', 
    headers: {3: {sorter:'mydate'}} 
}); 

working jsfiddle

+0

嗯..仍然沒有工作。其實這一次排序本身不允許...! –

+0

問題是你的日子有時只有一個數字。我編輯了代碼來說明這一點,並添加了一個jsfiddle,以便您可以看到它的工作。 – Banana

+1

+1不錯的工作香蕉! – Mottie

0

謝謝香蕉。我對你的代碼做了一些修改,現在它的工作正常:)

var monthDict = { 
'Jan': '01', 
'Feb': '02', 
'Mar': '03', 
'Apr': '04', 
'May': '05', 
'Jun': '06', 
'Jul': '07', 
'Aug': '08', 
'Sep': '09', 
'Oct': '10', 
'Nov': '11', 
'Dec': '12' 
} 
$.tablesorter.addParser({ 

    id: 'mydate', 
    is: function(s, table, cell) {return false;}, 
format: function(s, table, cell, cellIndex) { 

var dat = s.split('-'); 
dat[0]= (/^\d{2}$/.test(dat[0]))? dat[0]: '0'+dat[0];//If 'dd' format is single digit, append 0 to it. 
return dat[2]+monthDict[dat[1]]+dat[0];//Sort in 'yyyymmdd' format as priority of sorting 

}, 

    type: 'numeric' 
});