javascript - 如何修复表中的数据范围过滤器?
问题描述
我正在使用脚本过滤开始/结束日期之间的日期,但它不会在我的表中执行任何操作。
<script>
$(document).ready(function () {
$(function () {
var start = moment("2019-10-01 00:00:00");
var end = moment("2019-10-31 23:59:59");
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
ranges: {
}
}, cb);
cb(start, end);
});
$('#reportrange').on('apply.daterangepicker', function (ev, picker) {
var start = picker.startDate.format('YYYY-MM-DD');
var end = picker.endDate.format('YYYY-MM-DD');
console.log("-----------------------------");
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var min = new Date(start);
var max = new Date(end);
var startDate = new Date(data[1]);
console.log(startDate + " <= " + max + " --- " + (startDate <= max));
if (min == null && max == null) {
return true;
}
if (min == null && startDate <= max) {
return true;
}
if (max == null && startDate >= min) {
return true;
}
if (startDate <= max && startDate >= min) {
return true;
}
return false;
}
);
table.draw();
$.fn.dataTable.ext.search.pop();
});
});
</script>
这是表格,我希望它可以过滤带有开始和结束日期的日期,但它根本没有做任何事情
<div class="card-body">
<div id="tablas">
<div id="var receive">
<div class="table-striped">
<table class="table table-bordered" width="100%" cellspacing="0">
<thead align="center">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>Date</th>
</tr>
</thead>
<tbody id="mydataTable">
<tr>
<td align="center">1</td>
<td>987654 UserName</td>
<td align="center">Coins</td>
<td align="right">$1,000.00</td>
<td align="center">07/16/19</td>
<td align="center">
<form action="~/Index"> <button type="submit" class="btn btn-info">Select</button></form>
</td>
</tr>
<tr>
<td align="center">2</td>
<td>123456 UserName</td>
<td align="center">Coins</td>
<td align="right">$1,000.00</td>
<td align="center">10/16/19</td>
<td align="center">
<form action="~/Index"> <button type="submit" class="btn btn-info">Select</button></form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
我从这个小提琴中获取了这个例子,在网上寻找答案,因为我的表是在旅途中渲染的,而且没有 sql 连接,所有的在线例子都不能帮助我这样做:(
Reportrange 就在桌子上方,它是一个按钮
<div class="col-lg-3 col-md-3 form-group">
<label>Buscar por fecha:</label>
<div class="input-group">
<div id="reportrange" class="btn btn-info btn-lg">
<span class="input-group-append">
</span> <b class="caret"></b>
</div>
</div>
</div>
解决方案
编辑:
现在这实际上是您问题的答案:我在查看https://www.daterangepicker.com/和 moment.js 文档后重建了您的逻辑。
行的过滤可以在 daterangepicker 的回调函数中完成,如下所示。仅当表格行的日期位于日期范围选择器的所选开始日期和结束日期之间时,才会显示表格行。如果您真的想在选择后查看所选范围,您还应该将 daterangepicker 字段设为<input type="text">
字段。
$('#reportrange').daterangepicker({startOfWeek: 'monday'},function(start,end){
$('#mydataTable tr').each((i,tr)=>{ // go through each tr of table body:
var dt=moment($('td:eq(4)',tr).text(),'MM/DD/YY'); // parse the date in column 5 using moment.js
$(tr).toggle( start<dt && dt<end ); // if dt is between start and end then show, else hide
})
});
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<input type="text" id="reportrange">
<div class="card-body">
<div id="tablas">
<div id="var receive">
<div class="table-striped">
<table class="table table-bordered" width="100%" cellspacing="0">
<thead align="center">
<tr><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>Date</th></tr>
</thead>
<tbody id="mydataTable">
<tr>
<td align="center">1</td>
<td>987654 UserName</td>
<td align="center">Coins</td>
<td align="right">$1,000.00</td>
<td align="center">07/16/19</td>
<td align="center">
<form action="~/Index"> <button type="submit" class="btn btn-info">Select</button></form>
</td>
</tr><tr>
<td align="center">2</td>
<td>123456 UserName</td>
<td align="center">Coins</td>
<td align="right">$1,000.00</td>
<td align="center">10/16/19</td>
<td align="center">
<form action="~/Index"> <button type="submit" class="btn btn-info">Select</button></form>
</td>
</tr><tr>
<td align="center">3</td>
<td>007 somebody else</td>
<td align="center">Coins</td>
<td align="right">$100.00</td>
<td align="center">10/20/19</td>
<td align="center">
<form action="~/Index"> <button type="submit" class="btn btn-info">Select</button></form>
</td>
</tr><tr>
<td align="center">4</td>
<td>789101 UserName</td>
<td align="center">Coins</td>
<td align="right">$3,210.00</td>
<td align="center">11/07/19</td>
<td align="center">
<form action="~/Index"> <button type="submit" class="btn btn-info">Select</button></form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
推荐阅读
- javascript - ES8 和 ES9 中的 jshint 指的是什么?
- r - 错误 1:从 JavaScript 动态生成 Web 抓取时下标越界
- openssl - 在 OpenSSL 命令行工具中为自定义扩展创建自己的 ASN.1 模块
- jquery - 将导航栏品牌和链接与 Bootstrap 4 中的页面内容对齐
- node.js - 如何通过 nodejs express 应用程序调用 API 获取 png 图像
- javascript - Android 版 Chrome 中文本输入的奇怪行为
- java - 如果我在嵌入式 tomcat 上运行我的 Rest API 并且 IDE 已关闭,它从哪里运行?
- laravel - Laravel-echo-server 码头化问题
- azure-api-management - 如何在不使用 API Gateway 上的测试控制台的情况下跟踪 Api 代理请求?
- php - 如何将 wordpress 从 xampp windows 迁移到 ubuntu