简体   繁体   English

如何根据时间前的列数据对表格进行排序

[英]How can I sort table base on column data time ago

Below is my html table which I get from csv file.下面是我从 csv 文件中得到的 html 表。

Name名称 Role角色 Last login上次登录
Dahril达里尔 Admin行政 2 days ago 2天前
Sherwin宣威 Staff职员 1 week ago 1周前
Jeffrey杰弗里 Guest客人 2 weeks ago 2个星期前
Clyde克莱德 Guest客人 1 month ago 1个月前
MJ兆焦耳 Staff职员 2 months ago 2个月前
Ann Staff职员 1 year ago 1年前
Boy男生 Admin行政 2 years ago 2年前

I need to sort the table based on the last-login column from recent to past.我需要根据最近到过去的最后登录列对表格进行排序。
How can I achieve this in Javascript?我怎样才能在 Javascript 中实现这一点?

I would use moment to create a date object using the information.我会使用moment使用该信息创建一个日期 object。

While reading the CSV file line by line with a fileReader , 2 days ago has to be used like this: moment().subtract(2, "days") ... For example.在使用fileReader逐行读取 CSV 文件时,必须像这样使用2 days agomoment().subtract(2, "days") ... 例如。

So the strategy is to determine which time "range" to use and then, create a moment object that will be used to sort the rows.因此策略是确定要使用的时间“范围”,然后创建将用于对行进行排序的时刻 object。

 let csv = `Dahril,Admin,2 days ago SuperHero,Guest,3 years ago Sherwin,Staff,1 week ago Jeffrey,Guest,2 weeks ago Clyde,Guest,1 month ago MJ,Staff,2 months ago Ann,Staff,1 year ago Boy,Admin,2 years ago` // An array to process rows let rows = [] // Range posibilities let possibilities = ["days", "weeks", "months", "years"] // That loop would be inside a fileReader let lines = csv.split(/\n/) lines.forEach(line => { //console.log(line) let parts = line.split(",") let user = parts[0] // Dahril let role = parts[1] // Admin let when = parts[2] // 2 days ago let when_number = parseInt(when) // 2 let when_range = when.split(" ")[1] // days // Add an "s" if missing if(when_range.substr(-1).== "s"){ when_range += "s" } // Make sure moment will not fail if (isNaN(when_number)){ console.error(when + " does not start with a number.") return } let when_index = possibilities.indexOf(when_range) if (when_index < 0){ console.error(when_range + " is not found") return } //console.log(possibilities[when_index]) // Create a moment object let comparableTime = moment(),subtract(when_number.possibilities[when_index]) // Push an object containing each piece of information AND the moment object rows,push({user,role,when.comparableTime}) }) // sort the rows beased on the moment objects let sortedRows = rows,sort((ab) => b.comparableTime - a.comparableTime) // Show the result in table let resultTable = document.querySelector("#result") sortedRows.forEach(row => { let td_1 = document.createElement("td") td_1.innerText = row.user let td_2 = document.createElement("td") td_2.innerText = row.role let td_3 = document.createElement("td") td_3.innerText = row.when let tr = document.createElement("tr") tr.append(td_1) tr.append(td_2) tr.append(td_3) resultTable.append(tr) })
 td{ border: 1px solid black; padding: 6px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script> <table id="result"/>

Moment documentation: subtract矩文档:减去

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM