[英]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 ![]() |
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 ![]() |
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 ago
: moment().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"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.