0

I have a table which works as expected, show all the data that falls between start and end dates(please look on comments for more info). only issue I am having right now is when I click next button on a table, the table header moves dates to next week dates and shows the data for that week but also it shows the data from last week as well. what I needed was if it goes to next week date it should show data only for that week (no rows/data from last week).

enter image description here

//this is only an eg:

function test() {
  //this is the table header start date(in pic)6/4/17
  var test1 = document.getElementById("testing").innerHTML;

  //this is the table header close date(in pic)6/10/17
  var test2 = document.getElementById("testin").innerHTML;

  //this is the function if dates is between start and end its displays the data in a tabel
  function dateInRange(dbdates, start, end) {
    if (end >= dbdates && start <= dbdates) {
      return true;
    } else {
      return false;
    }
  }


  var columns = ['orange']
  var table = document.getElementById('tablet');

  for (var i = 0; i < (testvalues.length - 1); i = i + 5) { //looping all values from db
    if (dateInRange(testvalues[i + 2], test1, test2)) {

      var row = table.insertRow(-1);

      var cell1 = row.insertCell(-1);
      var cell2 = row.insertCell(-1);

      //more logic 

      //this is just an ex:
      //this is next button on table
      $("next").click(
        test();
      }
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
david
  • 107
  • 1
  • 11

3 Answers3

1

You need something like the following at the start of your dateInRange function:

function dateInRange(dbdates, start, end) {
    dbdates=Date(dbdates);
    start=Date(start);
    end=Date(end);
    //rest of function
}

This will allow you to do the Date comparison you are attempting. You could convert the values to Dates before passing them to the function but this keeps your code cleaner.

In regards to removing/replacing all the existing rows except for the header, a good solution is here: Delete all rows in an HTML table

0

In order to compare dates, you need to tell Javascript your string is a date and not just a regular string.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

sidewinder
  • 682
  • 4
  • 9
0

You should remove the data of table before for loop , try it:

$("#tablet tbody tr").remove();
sergioBertolazzo
  • 585
  • 2
  • 11
  • 27