Hello Lucas,
By default, when the displayed date range is not set, Gantt adds 1 cell to the most left and right dates of the task dataset.
The displayed date range can be set with the gantt.config.start_date
and gantt.config.end_date
parameters. And if you change them, you need to call the render
method to repaint the changes.
Here is an example:
You can get the minimal and maximal dates of the loaded dataset with the getSubtaskDates
method:
https://docs.dhtmlx.com/gantt/api__gantt_getsubtaskdates.html
To get the minimal and maximal dates for the displayed date range, you can use the getState
method:
https://docs.dhtmlx.com/gantt/api__gantt_getstate.html
With that method, it doesn’t matter if you manually set the displayed date range or Gantt calculated it dynamically from the task dates.
Gantt doesn’t have a public event that fires when the displayed date range is changed (dynamically or manually). But you can check the dates in the onDataRender
event handler:
https://docs.dhtmlx.com/gantt/api__gantt_ondatarender_event.html
Here is an example of how it can be implemented:
https://snippet.dhtmlx.com/0y6zlauz
Also, the displayed date range is not the same as viewport dates. The latter is used by Gantt for the smart rendering feature to not render HTML elements outside the viewport dates (visible area in the browser). There is no public method to get the viewport dates, but you can use the Gantt API to get them.
You can get the left viewport position from the scroll position of the timeline. For that, you need to use the getScrollState
method:
https://docs.dhtmlx.com/gantt/api__gantt_getscrollstate.html
To get the right viewport position, you need to add the width of the timeline container to the scroll position.
After that, you need to use the dateFromPos
method to convert the position in the timeline to the date:
https://docs.dhtmlx.com/gantt/api__gantt_datefrompos.html
You can see how it is used in the following snippet where the data is exported only within the viewport dates:
However, if you want to load tasks between the specified dates, it may not be the best way to do that. A child task will not be displayed if its parent task is not loaded. So, if you load only child tasks without parent tasks, you will see an empty chart.
Instead of that, you can load the whole dataset and filter tasks with the onBeforeTaskDisplay
event handler:
https://docs.dhtmlx.com/gantt/desktop__filtering.html
https://docs.dhtmlx.com/gantt/api__gantt_onbeforetaskdisplay_event.html
But if your use case is not related to Gantt, it may not be relevant.