Our pure JavaScript Scheduler component


Post by doonamis »

Hello! Currently I have a button than let's you select a resource (driver in our case) and duplicate it with another vehicle assigned, this works great, the thing is I also want to duplicate the timeRanges of this resource so they have the same timeRanges, the code below creates the new resource but with empty timeRanges, I don't know what I'm missing or doing wrong
addResource() {
        const scheduler = this.$refs.scheduler.schedulerEngine;
        
        if(!this.newLineModalDriver || !this.newLineModalVehicle) return;
        
        console.log('Add resource', this.newLineModalDriver, this.newLineModalVehicle);

        const driverId = this.newLineModalDriver.driverId
        //
        var driver = {
          id:	`${driverId}--${this.newLineModalVehicle.id}`,
          name:	this.newLineModalDriver.name,
          vehicleName: this.newLineModalVehicle.name,
          vehicleType: this.newLineModalVehicle.vehicleType,
          vehicleId: this.newLineModalVehicle.id,
          cls:	'b-grid-row--driver',
          urlPicture:	this.newLineModalDriver.urlPicture,
          vehicleLicensePlate: this.newLineModalVehicle.licensePlate,
          latitude: this.newLineModalDriver.latitude,
          longitude: this.newLineModalDriver.longitude
        }
        scheduler.resourceStore.add(driver)

        // Add resourceTimeRange
        var newTimeRanges = []

        // Search for this driver timeRanges and "duplicate"
        var timeRanges = scheduler.resourceTimeRangeStore.forEach(function (timeRange) {
          var timeRangeIds = timeRange.resourceId.split('--')
          //console.log(timeRangeIds[0], driverId, timeRangeIds[0] == driverId)
          if(timeRangeIds[0] == driverId) {
            var newTimeRange = Object.assign({}, timeRange);
            newTimeRange.id = Math.random().toString(36).substr(2, 9)
            newTimeRange.resourceId = driver.id
            newTimeRanges.push(newTimeRange)
            //
          }
        })

        // Add timeRanges to storage
        newTimeRanges.forEach(function(timeRange) {
          scheduler.resourceTimeRangeStore.add(timeRange)
        })
        
        this.newLineModal = false

        this.$toast.success('Driver added to scheduler')
      },
Thanks!

Post by Maxim Gorkovsky »

Hello.
I just tried this code in resourcetimeranges demo and it worked just fine:
let newResource = scheduler.resourceStore.add(scheduler.selectedRecord.copy())[0];

// timeRange always returns array
scheduler.selectedRecord.timeRanges.forEach(timeRange => {
  let copy = timeRange.copy();
  copy.resourceId = newResource.id;
  scheduler.resourceTimeRangeStore.add(copy);
});

Post by doonamis »

Hello Maxim,

Thanks! Worked perfect I don't know what I was doing wrong before but this works like charm

Post Reply