search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Adding values of events than counting in Group Availability

Asked by WSJU
2 years ago.

Hi, I am looking at this example https://code.daypilot.org/97538/javascript-scheduler-displaying-group-availability
and I see that it gives you a difference based on number of children.

Is there a quick way of using the same function in add up values (Event1, Event2 will integers instead of strings) and show it in the group level?

Answer posted by Dan Letecky [DayPilot]
2 years ago.

This example will calculate a sum of values stored in a specified event field ("val"):

onBeforeCellRender: (args) => {
  if (args.cell.isParent) {
    const field = "val";

    const children = dp.rows.find(args.cell.resource).children();
    const sum = children.reduce((acc, row) => {
      const events = row.events.forRange(args.cell.start, args.cell.end);
      const total = events.reduce((acc, e) => acc + e.data[field], 0);
      return acc + total;
    }, 0);

    args.cell.html = "" + sum;
  }
}

The event data object would look like this:

const events = [
  {
    id: 1,
    // ...
    val: 3
  },
  {
    id: 2,
    // ...
    val: 5
  },
];

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.