We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Specify work hours for days of week

We need to specify work hours for almost twenty resources for the 180-day date range. Doing it with setWorkHours will require passing thousands of dates. Is it possible to specify work hours for certain days of week for each resource?

We want to define that for example:

Resource A, works on Monday from 10 - 5pm and on Tuesday to Friday from 8 - 4 pm


3 Replies 1 reply marked as answer

VR Vijay Ravi Syncfusion Team December 28, 2022 03:30 PM UTC

Hi Pawel Sadowski,


You can achieve your requirement using the setWorkHours method of the Schedule. You need to define the day-wise work hours for each resource and set the work hours using the setWorkHours method on the dataBound event of the Schedule as shown in the below code snippet.

Sample: https://stackblitz.com/edit/ej2-js-schedule-multiple-work-hours-resource-basi-gstus4?file=index.js 


[Index.js]

var resourceWorkHours = {

    1: { 1: { start: '10:00'end: '17:00' }, 2: { start: "08:00"end: "16:00" }, 3: { start: '00:00' , end: '00:00' }, 4: { start: '00:00' , end: '00:00' }, 5: { start: '00:00' , end: '00:00' } },

    2: { 1: { start: '00:00' , end: '00:00' }, 2: { start: '00:00' , end: '00:00' }, 3: { start: '07:00'end: '11:00' }, 4: { start: "08:00"end: "12:00" }, 5: { start: '00:00' , end: '00:00' } },

    3: { 1: { start: "06:00"end: "10:00" }, 2: { start: '00:00' , end: '00:00' }, 3: { start: '00:00' , end: '00:00' }, 4: { start: '00:00' , end: '00:00' }, 5: { start: '14:00'end: '16:00' } }

}

 

 

dataBound: () => {

        if (islayoutChanged) {

            var renderedDates = scheduleObj.activeView.getRenderDates();

            scheduleObj.resetWorkHours();

            for (var i = 0i < renderedDates.lengthi++) {

                var dayIndex = renderedDates[i].getDay();

                if (dayIndex !== 0 && dayIndex !== 6) {

                    // Setting up work hours for resource one

                    scheduleObj.setWorkHours([renderedDates[i]], resourceWorkHours[1][dayIndex].startresourceWorkHours[1][dayIndex].end0);

                    // Setting up work hours for resource two

                    scheduleObj.setWorkHours([renderedDates[i]], resourceWorkHours[2][dayIndex].startresourceWorkHours[2][dayIndex].end1);

                    // Setting up work hours for resource three

                    scheduleObj.setWorkHours([renderedDates[i]], resourceWorkHours[3][dayIndex].startresourceWorkHours[3][dayIndex].end2);

 

                }

            }

        }

    },


Regards,

Vijay Ravi



PS Pawel Sadowski replied to Vijay Ravi May 19, 2023 12:37 PM UTC

Thank you. The solution works but now I have related problem on the timeline view. Removing resource which timeline is visible in the middle of other resource timelines causes that the working hours of removed resource are applied to the resource which takes its place on the view:

Example1.jpg

 The same situation happens when resource is added to the view and takes place in order of another resource - it gets working hours of resource which previously has that place in order: 

Example2.jpg

Resources are added/removed by addResource/removeResource methods. 





SR Swathi Ravi Syncfusion Team May 24, 2023 11:12 AM UTC

Hi Pawel,


You can resolve your reported issue by using the code snippets provided below. When you delete the resources, you also remove the work hours from the array.


Sample: https://stackblitz.com/edit/ej2-js-schedule-multiple-work-hours-resource-basi-aam5v5?file=index.js


[index.js]

var resourceWorkHours = [

  {

    1: { start: '10:00'end: '17:00' },

    2: { start: '08:00'end: '16:00' },

    3: { start: '00:00'end: '00:00' },

    4: { start: '00:00'end: '00:00' },

    5: { start: '00:00'end: '00:00' },

  },

  {

    1: { start: '00:00'end: '00:00' },

    2: { start: '00:00'end: '00:00' },

    3: { start: '07:00'end: '11:00' },

    4: { start: '08:00'end: '12:00' },

    5: { start: '00:00'end: '00:00' },

  },

  {

    1: { start: '06:00'end: '10:00' },

    2: { start: '00:00'end: '00:00' },

    3: { start: '00:00'end: '00:00' },

    4: { start: '00:00'end: '00:00' },

    5: { start: '14:00'end: '16:00' },

  },

];

var islayoutChanged = false;

dataBound: () => {

    console.log('dataBound Called');

    if (islayoutChanged) {

      var renderedDates = scheduleObj.activeView.getRenderDates();

      scheduleObj.resetWorkHours();

      var resourceDataSource =

        scheduleObj.getResourceCollections()[0].dataSource;

      for (var i = 0i < renderedDates.lengthi++) {

        var dayIndex = renderedDates[i].getDay();

        if (dayIndex !== 0 && dayIndex !== 6) {

          for (var j = 1j <= resourceDataSource.lengthj++) {

            scheduleObj.setWorkHours(

              [renderedDates[i]],

              resourceWorkHours[j - 1][dayIndex].start,

              resourceWorkHours[j - 1][dayIndex].end,

              scheduleObj.getIndexFromResourceId(j) != -1 ? scheduleObj.getIndexFromResourceId(j) : scheduleObj.getIndexFromResourceId(j + 1)

            );

          }

        }

      }

    }

  },

let removedElement;

function setWorkHourss() { }

var buttonObj = new ej.buttons.Button();

buttonObj.appendTo('#remove-res');

buttonObj.element.onclick = function () {

  islayoutChanged = true;

  scheduleObj.removeResource(2'Doctors');

  removedElement = resourceWorkHours.splice(11);

};

 

var buttonObj = new ej.buttons.Button();

buttonObj.appendTo('#add-res');

buttonObj.element.onclick = function () {

  islayoutChanged = true;

  const insertionIndex = 1// Specify the desired index for insertion

  scheduleObj.addResource(scheduleObj.resources[0].dataSource[1], 'Doctors'insertionIndex);   // Add the removed resources back to the array at a specific index.

  resourceWorkHours.splice(insertionIndex0removedElement[0]);  // Add the removed element back to the array at a specific index.

};



Regards,

Swathi Ravi


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon