简体   繁体   English

仅使用javaScript按日期值desc按列对HTML中的表进行排序

[英]Sort table in HTML by column with date values desc using only javaScript

Is it possible to make sorting function using only javaScript, without any other library for sorting? 是否可以仅使用javaScript而不使用其他任何库来进行排序?

Let's say I have one table, and it's first column that has date values in this format: MM/dd/yyyy. 假设我有一个表,它是第一列,其日期值采用以下格式:MM / dd / yyyy。 Table has two more columns, like this: 该表还有另外两列,如下所示:

<table id="results" width="360" border="1">
    <thead>
      <tr>
        <th scope="col" width="120">Date Created</th>
        <th scope="col" width="120">Name</th>
        <th scope="col" width="120">Tests</th>
      </tr>
      <tr>
        <td>07/08/2015</td>
        <td>Test Name</td>
        <td>Raven</td>
      </tr>
      <tr>
        <td>05/04/2015</td>
        <td>Test Name 4</td>
        <td>Raven 3</td>
      </tr>
      <tr/>
        <td>01/08/2017</td>
        <td>Test Name 2</td>
        <td>PCT</td>
      </tr>
    </thead>
</table>

Would it be possible to lets say add one button, and on click event sort rows by values in Date column? 可以说添加一个按钮,并在单击事件时按“日期”列中的值对行进行排序吗?

I have to accomplish this using only plain javaScript and HTML, so no jQuery unfortunately :( 我必须仅使用简单的javaScript和HTML来完成此操作,因此不幸的是没有jQuery :(

Here's a little something I whipped up to give you some ideas. 这是我想给您的一些想法。 Obviously you could extend this to sort by other data types. 显然,您可以将其扩展为按其他数据类型排序。

I've "cheated" on the date comparisons by just changing the string format date directly to an eight-digit number in the form 20140312 from "12/03/2014" - note that I've assumed the date input format is dd/mm/yyyy , so if for some reason you're actually using mm/dd/yyyy you'll have to tweak the convertDate() function. 我只是通过直接将字符串格式的日期更改为"12/03/2014" 20140312 "12/03/2014"中的20140312格式的八位数字来“欺骗”日期比较-请注意,我假设日期输入格式为dd/mm/yyyy ,因此,如果出于某种原因您实际上正在使用mm/dd/yyyy ,则必须调整convertDate()函数。

Also I've introduced a <tbody> into your table so that I can just sort the data rows and completely ignore the header row. 另外,我在表中引入了<tbody>以便可以对数据行进行排序,而完全忽略标题行。

 function convertDate(d) { var p = d.split("/"); return +(p[2]+p[1]+p[0]); } function sortByDate() { var tbody = document.querySelector("#results tbody"); // get trs as array for ease of use var rows = [].slice.call(tbody.querySelectorAll("tr")); rows.sort(function(a,b) { return convertDate(a.cells[0].innerHTML) - convertDate(b.cells[0].innerHTML); }); rows.forEach(function(v) { tbody.appendChild(v); // note that .appendChild() *moves* elements }); } document.querySelector("button").addEventListener("click", sortByDate); 
 <table id="results" width="360" border="1"> <thead> <tr> <th scope="col" width="120">Date Created</th> <th scope="col" width="120">Name</th> <th scope="col" width="120">Tests</th> </tr> </thead> <tbody> <tr> <td>04/04/2015</td> <td>Test Name 2</td> <td>1</td> </tr> <tr> <td>09/08/2017</td> <td>Test Name 5</td> <td>2</td> </tr> <tr> <td>07/08/2015</td> <td>Test Name 4</td> <td>3</td> </tr> <tr> <td>05/04/2015</td> <td>Test Name 3</td> <td>4</td> </tr> <tr> <td>12/08/2017</td> <td>Test Name 6</td> <td>5</td> </tr> <tr> <td>21/03/2014</td> <td>Test Name 1</td> <td>6</td> </tr> </tbody> </table> <button>Sort by date</button> 

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

相关问题 使用 Javascript 对带有日期范围列的 HTML 表进行排序 - Sort HTML table with date range column using Javascript 按日期列对表行DESC排序,不带插件 - Sort table rows DESC by date column, without plugins 如何根据日期值对 html 表列值进行排序 - How to sort an html table column values based on Date values 按字母顺序/按日期排序多列列表,而不使用Dropbox已完成的HTML / javascript / jQuery中的表 - Sort multi-column list alphabetically/by date without using a table in HTML/javascript/jQuery as Dropbox has done AngularJS或Javascript:仅使用索引对数组(ASC和DESC)进行排序 - AngularJS or Javascript: Sort array(ASC & DESC) using index only 如何使用javascript根据表中的月份对日期列进行排序? - How to sort the date column according to month in a table using javascript? 按日期对 desc 进行排序,如果匹配则在 javascript 数组中进行风险排序 - Sort desc on date and if tie then on risk in javascript array 使用javascript在html表中的日期时间和字母顺序排序 - date time and alphabet wise sort in html table using javascript 如何使用JavaScript对数字HTML表格列进行排序 - How to Sort a numeric HTML table column at defult using javascript 如何使用基于列索引的javascript对html表进行排序 - How to sort a html table using javascript based on column index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM