AskSmartAskSmart Posts: 22Questions: 6Answers: 0


Here is the template of my application:

When you first load the page, the default behavior is to load the data from the last month (with respect to the calendar).
The problem is that the data might be from previous years, and it implies no data is shown at the default load.

For that, I want to change this default behavior, to show in default the data from the last 30 days, but with respect to the data itself. That is, 30 days, with respect to the maximum date that exists in the data.

My difficulty is with doing that - I know that for that I have to change these 2 lines:

            dateMin = startdate;
            dateMax = enddate;

To something like that:

            dateMax = fetchMaxDateFromData();
            dateMin = dateMax - 30;

But I didn't figure out how can I implement this fetchMaxDateFromData(), because currently what happens is that aData is iterating over all the records' dates one by one.

Can someone please help me with doing that?

Thank you!

This question has an accepted answers - jump to answer


  • allanallan Posts: 62,424Questions: 1Answers: 10,244 Site admin

    How is the data being loaded into your actual app - is it HTML based like that example, or are you using Ajax? How is the Javascript generated?

    I think what I would do myself is to query the database as the page is being built to get the MAX of the date value, then use that as your starting point. Insert it into the JS.

    But the best way will depend a lot on your application and how it operates.


  • AskSmartAskSmart Posts: 22Questions: 6Answers: 0
    edited May 2022

    Hey Allan, thank you for trying to help.

    The data itself is loaded via the Django templating engine, directly via the HTML file. That is, once the page is loaded, all the records (rows of the table) of the SQL query are embedded in the HTML this way:

    But you can see here that the JS (which is exactly like mine) operates on this loaded data.

  • allanallan Posts: 62,424Questions: 1Answers: 10,244 Site admin
    Answer ✓

    Excellent. In that case, can you query the database to get the max date, and put that into your Javascript as a Django template variable?

    var maxDate = {% maxDate %};

    Or something like that (sorry - I haven't used Django before, so apologies if I've got the syntax wrong)?


Sign In or Register to comment.