[英]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.