根据startdate过滤JSON并结束

时间:2015-06-26 00:37:15

标签: javascript jquery json twitter-bootstrap bootstrap-table

我的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来自外部资源,并且在生产中将是动态的,但所有来源都具有相同的结构。


用户将有三个输入:

  1. 选择JSON源的第一个输入(为演示目的而删除)
  2. 选择开始日期以开始返回JSON数据的第二个输入(例如 2015-06-01
  3. 选择结束日期以结束返回JSON数据的第三个输入(例如 2015-06-19

  4. 以下是表单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>&nbsp;</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有一个过滤器扩展,但是这个过滤器扩展在获取数据之后有效。我的用户将请求包含数千条记录的文件,我无法获取所有记录,然后应用日期过滤器:)

2 个答案:

答案 0 :(得分:2)

我修改了代码:

http://codepen.io/anon/pen/pJdMxv

我所做的更改:我通过jquery加载了json,为开始日期,结束日期和obj日期创建了日期对象,然后创建了一个新的Array并将所有匹配的日期推送到数组中。

$("#getJsonSrc").click(function() {
  var $table = $("#dataTable"),
    $startDate = new Date($("#dateFrom").val()),
    $endDate = new Date($("#dateTo").val()),
    $jsonSrc = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/77979/demo.json";

  $.getJSON($jsonSrc).success(function(data) {
    my_array = new Array();

    for (var i = 0; i < data.length; i++) {
      var this_date = new Date(data[i].date);
      if ((this_date >= $startDate) && (this_date <= $endDate)) {
        my_array.push(data[i]);
      }
    }
    console.log(my_array);

    $table.bootstrapTable("destroy");
    $table.bootstrapTable();
    $table.bootstrapTable("refresh", {
        url: my_array
    });
  });


});

答案 1 :(得分:0)

原来我不需要JSON.stringify(my_array),我需要使用load选项而不是data。以下是工作代码:

$("#getJsonSrc").click(function() {
    $("#dataTable thead").show();
    var $table = $("#dataTable"),
        $startDate = new Date($("#dateFrom").val()),
        $endDate = new Date($("#dateTo").val()),
        $jsonSrc = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/77979/demo.json";

    var my_array;
    $.getJSON($jsonSrc).success(function(data) {
        my_array = [];

        for (var i = 0; i < data.length; i++) {
            var this_date = new Date(data[i].date);
            if ((this_date >= $startDate) && (this_date <= $endDate)) {
                my_array.push(data[i]);

            }
        }
        $table.bootstrapTable("load", my_array);
    });
});
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.js"></script>
<div class="container">
  <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" value="2015-06-17" />


          </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" value="2015-06-19" />

          </li>
        </ul>
      </div>

      <div class="col-md-3">
        <ul class="nav nav-stacked">
          <li>&nbsp;</li>
          <li>

            <button class="btn btn-primary" type="button" id="getJsonSrc">Search</button>

          </li>
        </ul>
      </div>
    </form>
  </div>
  <!-- Row -->

  <div class="row">
    <div class="col-md-12">
      <table id="dataTable" data-toggle="table" data-pagination="true" data-page-size="5" data-page-list="5, 10, All" data-search="true" data-search-align="left" data-striped="true" data-show-refresh="true" data-sort-name="date" data-sort-order="desc">
        <thead>
          <tr>
            <th class="col-xs-1" data-field="date">Date</th>
            <th class="col-xs-1" data-field="operator">Operator</th>
            <th class="col-xs-2" data-field="type">Type</th>
            <th class="col-xs-2" data-field="subType">Sub Type</th>
            <th class="col-xs-5" data-field="message">Message</th>
          </tr>
        </thead>
      </table>
    </div>
  </div>
</div>