我的 JSON 看起来像这样:
[
{
"date": "2015-06-19",
"operator": "ums.dam",
"type": "General Query",
"subType": "Sales",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-17",
"operator": "ums.rdl",
"type": "General Query",
"subType": "Media",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-17",
"operator": "ums.dam",
"type": "General Query",
"subType": "Other",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-16",
"operator": "ums.djf",
"type": "General Query",
"subType": "Press",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-15",
"operator": "ums.djf",
"type": "Troubleshooting",
"subType": "Service",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-15",
"operator": "ums.djf",
"type": "General Query",
"subType": "Sales",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-15",
"operator": "ums.djf",
"type": "General Query",
"subType": "Other",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-04",
"operator": "ums.jxh",
"type": "Account Maintenance",
"subType": "Add/Remove Feature",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-04",
"operator": "ums.djf",
"type": "Account Maintenance",
"subType": "Add/Remove Feature",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-04",
"operator": "ums.djf",
"type": "Troubleshooting",
"subType": "Service",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-04",
"operator": "ums.djf",
"type": "General Query",
"subType": "Other",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-01",
"operator": "ums.djf",
"type": "General Query",
"subType": "Manual Activation",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
},
{
"date": "2015-06-01",
"operator": "ums.djf",
"type": "General Query",
"subType": "Manual Activation",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempus odio purus, sit amet semper leo congue eu. Aliquam erat."
}
]
JSON 来自外部源,并且在生产中是动态的,但所有源都将具有相同的结构。
用户将有三个输入:
- 选择 JSON 源的第一个输入(出于演示目的已删除)
- 选择开始日期以开始返回 JSON 数据的第二个输入(例如2015-06-01)
- 第三个输入选择结束日期以结束返回 JSON 数据(例如2015-06-19)
这是 HTML 表单:
<div class="row">
<form id="search" role="form" style="border:0">
<div class="col-md-3">
<ul class="nav nav-stacked">
<li><strong>From Date</strong></li>
<li>
<input type="text" class="form-control" placeholder="yyyy-mm-dd" id="dateFrom" />
</li>
</ul>
</div>
<div class="col-md-3">
<ul class="nav nav-stacked">
<li><strong>To Date</strong></li>
<li>
<input type="text" class="form-control" placeholder="yyyy-mm-dd" id="dateTo" />
</li>
</ul>
</div>
<div class="col-md-3">
<ul class="nav nav-stacked">
<li> </li>
<li>
<button class="btn btn-primary" type="button" id="getJsonSrc">Search</button>
</li>
</ul>
</div>
</form>
</div>
<!-- Row -->
这是javascript:
$("#getJsonSrc").click(function() {
var $table = $("#dataTable"),
$startDate = $("#dateFrom").val(),
$endDate = $("#dateTo").val(),
$jsonSrc = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/77979/demo.json"; //for demo only - in production this will come from 1st input val()
/* This is the code that is not working */
$jsonSrc = filter(function(i, fromDate, toDate) {
return ($jsonSrc.date < $startDate && $jsonSrc.date > $endDate)
});
$table.bootstrapTable("destroy");
$table.bootstrapTable();
$table.bootstrapTable("refresh", {
url: $jsonSrc
});
});
基本上我想要的是当用户点击搜索页面获取 JSON 源 URL,但只返回输入日期之间的数据(包括输入的日期)。然后将 JSON 输出到引导数据表中。
我通常提供一个 jsFiddle 示例,但是我无法获得一个小提琴来读取外部 JSON 文件,所以希望一个 codepen 示例是可以的:)
http://codepen.io/lordtubington/pen/GJOVpy
注意:我意识到引导表https://github.com/lukaskral/bootstrap-table-filter有一个过滤器扩展,但是这个过滤器扩展在获取数据后工作。我的用户将请求包含数千条记录的文件,我不能让他们获取所有记录然后应用日期过滤器:)