jQuery按照从最小到最大的顺序显示日期

我很高兴在我的另一个问题上收到以下日期排序代码:)但我有一个快速查询。

如何从最小日期到最大日期的顺序生成最小日期和最大日期apear的结果?

jQuery的:

$(function() { $.datepicker.setDefaults({ dateFormat: "dd/mm/yy" }); //sets default datepicker's dateFormat $("#mindate, #maxdate") .datepicker() //initializes both of the date inputs as jQuery UI datepickers .on('keypress keyup change blur', function(){ filter(); }); //sets listeners to filter the table on the fly }); function filter(){ $('tr').show(); //resets table to default before executing the filter datefields = $('table.bordered tr td:nth-child(2)'); //stores a jquery oject containing all tds of the 2nd column (dates) datefields.each(function(){ //iterates through them.. var evdate = pdate($(this).html()); //evaluates each of their dates as date objects var mindate = pdate($('#mindate').val()); //evaluates the entered mindate (will return false if not valid*) if (mindate) //checks if the mindate is valid if (evdate  maxdate) $(this).parent().hide(); //hides the row if its date is after the maxdate }); } function pdate(str){ //parse date function, receives a string and returns a date if it's valid or false if it isn't if (!isValidDate(str)) return false; //returns false if the date isn't valid var parts = str.split("/"); // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]]) return new Date(parts[2], parts[1]-1, parts[0]); } //this is my custom date validating function, much better than those which you may find in the web :P function isValidDate(date) { parts = date.split('/'); if (parts.length != 3) return false; else if (parts[0] == '' || /[^0-9]/.test(parts[0]) || parts[1] == '' || /[^0-9]/.test(parts[1]) || parts[2] == '' || /[^0-9]/.test(parts[2])) return false; day = parts[0]; month = parts[1]; year = parts[2]; if (year >= 1800 && year  0 && day  0 && month  30) return false; if (month == 2) { if (year%4 == 0 && (year%100 != 0 || year%400==0)) { if (day > 29) return false; } else if (day > 28) return false; } return true; } else return false; } else return false; } 

这是一个可以工作的补充function:

 function orderAsc(td){ var evdate = pdate(td.html()); var datefields = $('table.bordered tr td:nth-child(2)'); datefields.each(function(i){ var tempdate = pdate($(this).html()); if (evdate > tempdate) td.parent().before($(this).parent()); }); } 

然后只需使用orderAsc($(this))filter()函数的.each调用它。

我建议只在你第一次加载页面时对表进行排序,当你加载新内容以便在客户端保存CPU处理时,无论如何,这里是工作小提琴的一些评论。 =]

那么你必须使用排序,你可以调用每个新的负载为你的结果

即JQuery

1-如果你想使用这个问题答案使用jQuery tablesorter对mm / yy日期进行排序有用但需要定制。

2- 使用jQuery对元素进行排序

要使用tablesorter插件,请在HTML文档的标记内包含jQuery库和tablesorter插件:

   

tablesorter适用于标准HTML表。 您必须包含THEAD和TBODY标记:

 
Last Name First Name Email Due Web Site
Smith John jsmith@gmail.com $50.00 http://www.jsmith.com
Bach Frank fbach@yahoo.com $50.00 http://www.frank.com
Doe Jason jdoe@hotmail.com $100.00 http://www.jdoe.com
Conway Tim tconway@earthlink.net $50.00 http://www.timconway.com

首先告诉tablesorter在加载文档时对表进行排序:

 $(document).ready(function() { $("#myTable").tablesorter(); } ); 

点击标题,您会看到您的表格现在可以排序! 您还可以在初始化表时传递配置选项。 这告诉tablesorter按升序对第一列和第二列进行排序。

 $(document).ready(function() { $("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} ); } ); 

注意 ! tablesorter将自动检测大多数数据类型,包括数字,日期,ip-adresses以获取更多信息,请参阅示例