简体   繁体   English

使用jQuery按第一个日期列对html表行进行排序

[英]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');

http://jsfiddle.net/rmva17gr/ http://jsfiddle.net/rmva17gr/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用HTML和Javascript排序表格-第一栏的问题 - Sort Table in HTML & Javascript - issue with first column html 中的 Email 表,但将第一列格式化为日期 - Email table in html but format first column as date 按字母顺序/按日期排序多列列表,而不使用Dropbox已完成的HTML / javascript / jQuery中的表 - Sort multi-column list alphabetically/by date without using a table in HTML/javascript/jQuery as Dropbox has done 如何使用JavaScript和jQuery覆盖HTML tbody表中每行中第一列的值? - How to overwrite the values of the first column in every rows in a HTML tbody table with JavaScript and with jQuery? 使用jquery读取HTML表的第一列值 - Read the first column values of a HTML table with jquery 按日期列对表行DESC排序,不带插件 - Sort table rows DESC by date column, without plugins JQuery表排序问题 - 跳过列禁用第一次鼠标单击 - JQuery Table Sort issue - Skipping column disables first mouse click 使用Javascript或jQuery快速排序第一列的表 - Sort a table fast by its first column with Javascript or jQuery jQuery基于自定义列值对表行进行排序 - jquery sort table rows based on the custom column value 尝试按列排序时,jQuery将行动态加载到表中不起作用 - Jquery dynamic loading of rows into a table not working when tried to sort by column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM