gpt4 book ai didi

javascript - 筛选表中的日期从、到

转载 作者:行者123 更新时间:2023-12-03 01:29:58 25 4
gpt4 key购买 nike

我的项目中有表

我在表格中按日期进行过滤

现在它仅按日期值进行过滤,但我想过滤它的起始日期。

对于我的情况,我该如何做到这一点?

这是演示我的问题的片段

 $('#datefilterfrom').change(function () {
var date = moment(this.value).format('DD/MM/YYYY');
var filter, table, tr, td, i;
filter = date.toUpperCase();
table = document.getElementById("testTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[2];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}

}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<div class="row">
<div class="col-md-3">
<h4>Date from</h4>
<input type="date" class="form-control" id="datefilterfrom" data-date-split-input="true">
</div>
<div class="col-md-3">
<h4>Date to</h4>
<input type="date" class="form-control" id="datefilterto" data-date-split-input="true">
</div>
</div>
<table id="testTable" class="table" border="1">
<tr>
<td>nothing</td>
<td>nothing</td>
<td>18/07/2018</td>
<td>nothing</td>
</tr>
<tr>
<td>nothing</td>
<td>nothing</td>
<td>19/07/2018</td>
<td>nothing</td>
</tr>
<tr>
<td>nothing</td>
<td>nothing</td>
<td>20/07/2018</td>
<td>nothing</td>
</tr>
</table>

感谢您的帮助。

最佳答案

请检查是否正常

function filterRows() {
var from = $('#datefilterfrom').val();
var to = $('#datefilterto').val();

if (!from && !to) { // no value for from and to
return;
}

from = from || '1970-01-01'; // default from to a old date if it is not set
to = to || '2999-12-31';

var dateFrom = moment(from);
var dateTo = moment(to);

$('#testTable tr').each(function(i, tr) {
var val = $(tr).find("td:nth-child(3)").text();
var dateVal = moment(val, "DD/MM/YYYY");
var visible = (dateVal.isBetween(dateFrom, dateTo, null, [])) ? "" : "none"; // [] for inclusive
$(tr).css('display', visible);
});
}

$('#datefilterfrom').on("change", filterRows);
$('#datefilterto').on("change", filterRows);

https://jsfiddle.net/oajc94hf/38/

关于javascript - 筛选表中的日期从、到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51345745/

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