[英]Sort html table rows by first date column with jQuery
I have table with two date columns in each row start date and end date I want to sort table rows by start date with jQuery 我有每个行开始日期和结束日期两个日期列的表我想用jQuery按开始日期对表行进行排序
Below is the table html code: 下面是表格html代码:
<table class="table-bordered">
<thead>
<tr class="Headers">
<th>Number</th>
<th>Date Start</a></th>
<th>Date End</th>
</tr>
</thead>
<tbody>
<tr class="Entries" data-id="13">
<td data-field-type="string">1234</td>
<td data-field-type="date">01-04-2015</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="24">
<td data-field-type="string">1352</td>
<td data-field-type="date">04-10-2012</td>
<td data-field-type="date">23-10-2015</td>
</tr>
<tr class="Entries" data-id="8">
<td data-field-type="string">1124</td>
<td data-field-type="date">13-05-2014</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">07-11-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
</tbody>
</table>
I made a try with similar solution posted here but without success. 我尝试了这里发布的类似解决方案,但没有成功。 JSFiddle 的jsfiddle
Look at this site. 看看这个网站。 It looks like this is what you are trying to do. 看起来这就是你想要做的事情。 http://www.kryogenix.org/code/browser/sorttable/ http://www.kryogenix.org/code/browser/sorttable/
when you pass string to Date
constructor it's format is month/date/year
. 当你将字符串传递给Date
构造函数时,它的格式是month/date/year
。
since your date format is date-month-year
. 因为您的日期格式是date-month-year
。 you can reformat it using regular expression. 你可以使用正则表达式重新格式化它。
$('tr.Entries').sort(function (a, b) {
return new Date($(a).find($("[data-field-type='date']")).html().replace(/(\d{2})-(\d{2})-(\d{2})/g,'$2/$1/$3')).getTime() < new Date($(b).find($("[data-field-type='date']")).html().replace(/(\d{2})-(\d{2})-(\d{2})/g,'$2/$1/$3')).getTime()
}).appendTo('tbody');
I'd suggest: 我建议:
$('tr.Entries').each(function() {
var t = this.cells[1].textContent.split('-');
$(this).data('_ts', new Date(t[2], t[1]-1, t[0]).getTime());
}).sort(function (a, b) {
return $(a).data('_ts') < $(b).data('_ts');
}).appendTo('tbody');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.