Search code examples
javascriptdhtmlxdhtmlx-scheduler

Event handler for header clicks in DHTMLX Scheduler


We use DHTMLX Scheduler and we would like to attach a listener for clicks on header in week view, unit view, etc. We didn't find a way to do this.

screenshot

We also need date in our listener with date for which click was performed.

How can we do this with DHTMLX Scheduler?


Solution

  • DHTMLX Scheduler does not provide an event for clicks on header, so in order to do this, you need to change templates for your views first:

    var toStr = scheduler.date.date_to_str(scheduler.config.day_date);
    scheduler.templates.week_scale_date = (dt) => "<span data-scale-date='" + dt.valueOf() + "'>" + toStr(dt) + "</span>";
    scheduler.templates.unit_scale_text = (key, label) => "<span data-scale-date='0' data-scale-unitId='" + key + "'>" + label + "</span>";
    // ...
    

    After templates are changed, you can attach an event listener to #scheduler_here and filter for clicks on header:

    document.getElementById("scheduler_here").addEventListener("click", (evt) => {
        var event = evt || window.event;
        var target = event.target || event.srcElement;
    
        if (target && target.nodeName === "SPAN" && target.parentElement && target.parentElement.classList && target.parentElement.classList.contains("dhx_scale_bar")) {
            var dateStr = target.getAttribute("data-scale-date");
            if (dateStr) {
                var dateValue = parseInt(dateStr, 10);
                var date = dateValue === 0 ? scheduler.getState().date : new Date(dateValue);
                var unitId = target.getAttribute("data-scale-unitId") ? target.getAttribute("data-scale-unitId") : {{your unit id in unit view}};
                // ...
            }
    
            evt.stopPropagation();
            evt.preventDefault();
        }
    }, false);