javascript - 如何在没有任何依赖关系的情况下对 html 表中的日期列进行排序?
问题描述
我想按表中的最新和最旧日期对日期列进行排序。
我已经尝试过下面的代码,但它不起作用。我不知道如何对表格进行排序,因为我是这个领域的新手。
function convertDate(d) {
var p = d.split("/");
return +(p[2] + p[1] + p[0]);
}
function sortByDate() {
var tbody = document.querySelector("#example 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[7].innerHTML) - convertDate(b.cells[7].innerHTML);
});
rows.forEach(function(v) {
tbody.appendChild(v); // note that .appendChild() *moves* elements
});
}
document.querySelector("button").addEventListener("click", sortByDate);
//retrieve data from firebase
rootRef.on('child_added', function(childSnapshot) {
rootRef = childSnapshot.val();
var tr = document.createElement('tr');
tr.appendChild(createCell(rootRef.date));
table.appendChild(tr);
});
function createCell(text) {
var td = document.createElement('td');
td.appendChild(document.createTextNode(text));
return td;
}
解决方案
我不知道你从哪里得到你的日期,但假设你可以很容易地用它们制作一个数组;我认为自定义排序功能可以解决问题。当然,您可能会使用 Date 对象获得更优雅的代码,但由于只有三个排序标准,我看不出过度设计的意义。这是我想出的代码:
//test case date array
let dates = ["15-Dec-2015", "23-Feb-2029", "14-Apr-2009", "15-Apr-2009", '15-May-2009', '14-Apr-2004'];
//custom sorting
let sortedDates = dates.sort((a, b) => { //compare function
const monthArray = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; //month index
//split a and b
a = a.split('-');
b = b.split('-');
//compare years
if(Number(a[2]) > Number(b[2]))
return 1;
if(Number(a[2]) < Number(b[2]))
return -1;
//compare months
if(monthArray.indexOf(a[1]) > monthArray.indexOf(b[1]))
return 1;
if(monthArray.indexOf(a[1]) < monthArray.indexOf(b[1]))
return -1;
//compare days
if(Number(a[0]) > Number(b[0]))
return 1;
if(Number(a[0]) < Number(b[0]))
return -1;
});
console.log(sortedDates)
//returns ["14-Apr-2004", "14-Apr-2009", "15-Apr-2009", "15-May-2009", "15-Dec-2015", "23-Feb-2029"]
编辑:
不确定它是否会起作用,但我尝试使用您的数据实现我的代码:
//test case date array
let dates;
function convertDate(d) {
var p = d.split("/");
return +(p[2] + p[1] + p[0]);
}
function sortByDate() {
var tbody = document.querySelector("#example 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[7].innerHTML) - convertDate(b.cells[7].innerHTML);
});
rows.forEach(function(v) {
tbody.appendChild(v); // note that .appendChild() *moves* elements
});
}
document.querySelector("button").addEventListener("click", sortByDate);
//retrieve data from firebase
rootRef.on('child_added', function(childSnapshot) {
rootRef = childSnapshot.val();
var tr = document.createElement('tr');
tr.appendChild(createCell(rootRef.date));
dates.push(rootRef.date);
table.appendChild(tr);
});
function createCell(text) {
var td = document.createElement('td');
td.appendChild(document.createTextNode(text));
return td;
}
//custom sorting
let sortedDates = dates.sort((a, b) => { //compare function
const monthArray = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; //month index
//split a and b
a = a.split('-');
b = b.split('-');
//compare years
if(Number(a[2]) > Number(b[2]))
return 1;
if(Number(a[2]) < Number(b[2]))
return -1;
//compare months
if(monthArray.indexOf(a[1]) > monthArray.indexOf(b[1]))
return 1;
if(monthArray.indexOf(a[1]) < monthArray.indexOf(b[1]))
return -1;
//compare days
if(Number(a[0]) > Number(b[0]))
return 1;
if(Number(a[0]) < Number(b[0]))
return -1;
});
console.log(sortedDates)
推荐阅读
- java - 如何打印反斜杠?
- javascript - 外部网站如何重定向到本地主机
- database - 如何通过数据值过滤Hive数据?
- python - 使用 'spawn' 启动 redis 进程但面临 TypeError: can't pickle _thread.lock objects
- sql - 如何在 Git-Bash 的 PSQL 中断开与数据库的连接?
- visual-studio-code - 我应该为自定义语言构建语言服务器 - vscode
- javascript - 删除两个字符正则表达式之间的所有空格
- c++ - pthreads / fork / 和 execvp 的主要例子?
- spring - 无法从启动应用程序中的多个 yml 文件加载属性
- python - 在 Pandas 中将多行的数据合并为一行,并带有新的标题