Have WorkDayStart be a function, or customize slotTemplate in MVC

0 Answers 79 Views
Scheduler Templates
Paul
Top achievements
Rank 1
Iron
Iron
Veteran
Paul asked on 23 Sep 2022, 05:33 PM | edited on 23 Sep 2022, 08:53 PM

I have two Kendo Schedulers. In the first one the WorkDayStart and WorkDayEnd should be custom to the day(s) being viewed. They are not static at one single time. If I am viewing one single day, I am sure I could inject some Javascript to find these and update them, then rerender the scheduler. But when viewing a week that would not help. Really they need to be a function. Is this possible?

The second scheduler is grouped by a resource of an Employee. So in this case it is more complicated. I have this one locked down to viewing only a single day view (or other views that don't include slots that may be closed like Agenda). But the columns here are dependent on the employee's availability for the day selected. So again a function is needed.

Alternatively, digging in the kendo.all.js, I see a property called "slotTemplate" but this doesn't seem to be exposed in MVC. Is there a workaround using this?

[EDIT]: I finally found the SlotTemplate as part of the views in MVC. Will play around here and possibly I can solve this on my own.

[EDIT 2]: I did get SlotTemplate working, but the real solution is to fix the WorkDayStart and WorkDayEnd. Let's say this is a program for a store. Bob logs in. Bob sees his own schedule on one tab, and the store schedule on another tab. Bob works different hours per day. The store is open different hours per day. It's not always the same. When viewing a week we need to be expanded to the most open day. When viewing a single day we should match that day.

Here's a SlotTemplate tagged in CSHTML:

views.DayView(v => v.Selected(true)).SlotTemplateId("shopSlotTemplate");
Now, I've actually got three schedules. One shows Bob's schedule or whomever is logged in. This one contains an employee ID to identify Bob. Another one shows the entire store, but it is grouped by the employee ID. Still one more shows the entire store but does not group by employee ID and instead puts everything together. These in respective order are here:
<script id="mySlotTemplate" type="text/x-kendo-template">
   #= getSlotTemplate(data, @loggedInEmployeeId) #
</script>

<script id="shopSlotTemplate" type="text/x-kendo-template">
   #= getSlotTemplate(data) #
</script>

<script id="acSlotTemplate" type="text/x-kendo-template">
   #= getSlotTemplate(data, 0) #
</script>
Next, I added the getSlotTemplate function as follows:

 


function getSlotTemplate(data, employeeId) {

   // Get employee ID if not passed in. If still not found then this scheduler is not grouped this way.
   if (employeeId === undefined) {
      employeeId = data.resources().EmployeeId;
   }

   let isClosed = isShopClosed(shopSchedule.Shop, data.date);

   if ((!isClosed) && (employeeId > 0) && (shopSchedule["Employee" + employeeId] !== undefined)) {
      isClosed = isShopClosed(shopSchedule["Employee" + employeeId], data.date);
   }

   return "<div class=\"" + (isClosed ? "k-nonwork-hour" : "") + "\">&nbsp;</div>";
}
isShopClosed is a function that looks inside a JSON object I've loaded. It contains employee schedules and store schedules. Half days off, full days off, holidays, etc. are all a part of this. It returns a simple boolean. First I'm checking is the store closed, and next I am checking is the employee not working. A DIV is returned with the appropriate Kendo class.

 

This required new CSS since the TD element has margins.

div.k-scheduler-content > table > tbody > tr > td > div {
   margin: -4px -0.5em;
   height: calc(100% + 8px);
   width: calc(100% + 1em);
}

The end result is that the grid for the scheduler appears how I want it.

But working hours are still inaccurate. They are based on a single date and don't change as I move between days.

Yanislav
Telerik team
commented on 28 Sep 2022, 12:29 PM

Hello Paul,

If I understand correctly, you are trying to change dynamically the WorkDayStart and WorkDayEnd properties based on the selected date, view, and the current user information that you get from a JSON.
If so, what I can recommend you is to hook up forthe Navigate event of the Scheduler, and by using its setOptions method you can change the WorkDayStart and WorkDayEnd properties in a different way based on the result from the JSON, the date, and the current view. 
Here is an example: https://netcorerepl.telerik.com/mGaXwCPQ15optq8q11

As you can see when navigating to the next day, the "WorkDayStart" is different. Also, you can get the View and the Date through the event object, which might be helpful in defining the condition.

I hope this helps.

Paul
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 29 Sep 2022, 03:30 PM

Yes! This will work. Thank you!

No answers yet. Maybe you can help?

Tags
Scheduler Templates
Asked by
Paul
Top achievements
Rank 1
Iron
Iron
Veteran
Share this question
or