Use Buttons to Search a Date Within a Range

Hey Tadabase community! I’ve been working on a method to assist with some specific search functionality involving isolating records with a date range based on a specific date provided. I wanted to share this method with the community as well so anyone can utilize it!

The idea behind this method is to create a user experience where a user can click a date button on a page and then will be able to see details for records on that date. The challenge is that we are working with a date range (saved as two date fields). So, using this custom setup, you can effectively find whether a given date is included in a record’s date range and you can also create the experience of performing the search at the click of a button.

Please view this video for the full end-to-end setup.

Here are the resources that go along with the video.

Javascript for the pipe call (@4:50)

Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}
var retVal = "", current = new Date('{start}'), end = new Date('{end}');
while (current <= end) {
  retVal+=current.toLocaleDateString('en-CA', {day: '2-digit',month: '2-digit',year: 'numeric',})+" ";
  current = current.addDays(1);
 }
retVal;

HTML source code for page buttons (@7:30)

Download HTML here

Javascript for the page (@9:03)

TB.render('component_24', function(data) {
    TB.hideComponent('component_12');
    var links = document.getElementsByClassName("date-link"), days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
    for(var x=2;x<links.length;x++){
        var date = new Date();
        date.setDate(date.getDate() + x); 
        console.log(date);
        links[x].innerHTML = days[date.getDay()] + " " + date.getDate();
     }
    $(".date-link").click(function(){
        var date2 = new Date();
        date2.setDate(date2.getDate() + parseFloat(this.dataset.value)); 
        window.location = "#!/default?date="+ date2.toLocaleDateString('en-CA', {day: '2-digit',month: '2-digit',year: 'numeric',});
        location.reload();
    });
});
4 Likes