javascript - JQuery $ajax only get items with a date before today -
i have following jquery code:
var fetchresults = function () { return json.parse($.ajax({ type: 'get', url: '/search/getresults', async: false, datatype: 'json' }).responsetext); }; /search/getresults returns json-format string of data. /search/getresults accepts no criteria.
the data returned looks this:
[{"title":"sample 1 title","sample 1 description":"","headline":"sample 1 headline","body":"","date":"2016-01-17 5:30:00"},{"title":"sample 2 title","sample 2 description":"","headline":"sample 2 headline","body":"","date":"2016-01-22 7:45:17},{"title":"sample 3 title","sample 3 description":"","headline":"sample 3 headline","body":"","date":"2016-01-27 15:26:17"},{"title":"sample 3 title","sample 3 description":"","headline":"sample 4 headline","body":"","date":"2016-01-29 18:00:00"}] using example data shown above, want fetchresults contain items "date" before or equal 2016-01-27 15:26:17 (an example point in time). there function of $.ajax allow me perform filtering?
if so, how go doing this?
your example data has missing quote fix , add datafilter ajax:
var fetchresults = function() { return json.parse($.ajax({ type: 'get', url: '/search/getresults', async: false, datatype: 'json', datafilter: function(mydata) { var pdata = json.parse(mydata); var startdate = new date("2016-01-27 15:26:17");// hard coded :) var dateless = pdata.filter(function(r) { //console.log("r",r.date);// each date var d = new date(r.date); return d <= startdate }); return dateless;//filtered data } }).responsetext); }; this returns:
[{ "title": "sample 1 title", "sample 1 description": "", "headline": "sample 1 headline", "body": "", "date": "2016-01-17 5:30:00" }, { "title": "sample 2 title", "sample 2 description": "", "headline": "sample 2 headline", "body": "", "date": "2016-01-22 7:45:17" }, { "title": "sample 3 title", "sample 3 description": "", "headline": "sample 3 headline", "body": "", "date": "2016-01-27 15:26:17" }] here sample filter working outside ajax: https://jsfiddle.net/2wbdpfos/
Comments
Post a Comment