gpt4 book ai didi

javascript - 使用 Javascript 对带有日期范围列的 HTML 表进行排序

转载 作者:行者123 更新时间:2023-11-30 20:11:30 25 4
gpt4 key购买 nike

例如,如果一列在第一行包含日期 9/17/2018 - 9/21/22018,在第二行包含日期 9/30/2018 - 10/12/2018 等,需要帮助从最早开始对此类列进行排序日期范围。我现在的 table :

<table border="1" cellpadding="1" cellspacing="0" class="sortable" 
id="myTable2" style="width:100%;border:1px solid #000000;">
<thead>

<th style="text-align: center;" width="200"><b>Pay Week</b></th>
<th style="text-align: center;" width="200"><b>Date of Intermittent Leave
Event</b> </th>
<th style="text-align: center;" width="400"><b>Pay Instruction</b></th>

</thead>
<tbody>
<tr>
<td style="text-align: center;" width="200">8/5/2018 - 8/11/2018</td>
<td style="text-align: center;" width="200">8/8/2018</td>
<td style="text-align: center;" width="400">3.0 hour(s) unpaid/ PTO
(FMLA)</td>
</tr>
<tr>
<td style="text-align: center;" width="200">8/12/2018 - 8/18/2018</td>
<td style="text-align: center;" width="200">8/18/2018</td>
<td style="text-align: center;" width="400">4.0 hour(s) unpaid/ PTO
(FMLA)</td>
</tr>
<tr>
<td style="text-align: center;" width="200">9/9/2018 - 9/15/2018</td>
<td style="text-align: center;" width="200">9/12/2018</td>
<td style="text-align: center;" width="400">7.0 hour(s) unpaid/ PTO
(FMLA)</td>
</tr>
<tr>
<td style="text-align: center;" width="200">9/9/2018 - 9/15/2018</td>
<td style="text-align: center;" width="200">9/13/2018</td>
<td style="text-align: center;" width="400">7.0 hour(s) unpaid/ PTO
(FMLA)</td>
</tr>
<tr>
<td style="text-align: center;" width="200">9/9/2018 - 9/15/2018</td>
<td style="text-align: center;" width="200">9/14/2018</td>
<td style="text-align: center;" width="400">7.0 hour(s) unpaid/ PTO
(FMLA)</td>
</tr>
<tr>
<td style="text-align: center;" width="200">9/3/2017 - 9/9/2017</td>
<td style="text-align: center;" width="200">N/A - Continuous</td>
<td contenteditable="true" style="text-align: center;" width="400"></td>
</tr>
<tr>
<td style="text-align: center;" width="200">9/10/2017 - 9/16/2017</td>
<td style="text-align: center;" width="200">N/A - Continuous</td>
<td contenteditable="true" style="text-align: center;" width="400"></td>
</tr>
<tr>
<td style="text-align: center;" width="200">9/17/2017 - 9/23/2017</td>
<td style="text-align: center;" width="200">N/A - Continuous</td>
<td contenteditable="true" style="text-align: center;" width="400"></td>
</tr>
<tr>
<td style="text-align: center;" width="200">9/24/2017 - 9/30/2017</td>
<td style="text-align: center;" width="200">N/A - Continuous</td>
<td contenteditable="true" style="text-align: center;" width="400"></td>
</tr>
<tr>
<td style="text-align: center;" width="200">10/1/2017 - 10/7/2017</td>
<td style="text-align: center;" width="200">N/A - Continuous</td>
<td contenteditable="true" style="text-align: center;" width="400"></td>
</tr>
<tr>
<td style="text-align: center;" width="200">10/8/2017 - 10/14/2017</td>
<td style="text-align: center;" width="200">N/A - Continuous</td>
<td contenteditable="true" style="text-align: center;" width="400"></td>
</tr>
<tr>
<td style="text-align: center;" width="200">10/15/2017 - 10/21/2017</td>
<td style="text-align: center;" width="200">N/A - Continuous</td>
<td contenteditable="true" style="text-align: center;" width="400"></td>
</tr>
</tbody>
</table>
<button>Sort Pay Week</button>

<script>
function convertDate(d) {
var str = d.split("-", 1);
var p = str.split("/");
return +(p[2] + p[0] + p[1]);
}

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);

</script>

第一列排序后,第二列的值乱七八糟,如何只对包含日期的行进行排序而不对 NA-Continuous 单元格进行排序?

最佳答案

我为你做的:

function convertDate(d) {
var str = d.split("-", 1)[0].trim();
var p = str.split("/");
return new Date(p[2] + "-"+p[0]+"-" + p[1]);
}

function sortByDate() {
var tbody = document.querySelector("#myTable2 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);

代码笔:https://codepen.io/agamj474/pen/KxGEXV .

关于javascript - 使用 Javascript 对带有日期范围列的 HTML 表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52372584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com