GuavaCZ / calendar

MIT License
140 stars 13 forks source link

[Question]: getResources of current selected date #42

Closed aeq-dev closed 1 month ago

aeq-dev commented 1 month ago

What happened?

Hello, I have a User as Resourceable model, each user has many tasks. I'm using$calendarView = 'resourceTimeGridDay', I want to display only users having tasks on selected date. How could I achieve that ? Thanks

How to reproduce the bug

It's a question :)

Package Version

1.8

PHP Version

8.2

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

No response

lukas-frey commented 1 month ago

Hi, it's an ugly hack, but this is what I did in a project where I needed to do the same:

public function getEvents(array $fetchInfo = []): Collection|array
    {
        // Store the current "view" (start / end dates)
        $this->currentView = [
            'start' => data_get($fetchInfo, 'startStr'),
            'end' => data_get($fetchInfo, 'endStr'),
        ];

        // Trigger a resource refresh
        $this->refreshResources();

        // Returns your events as usual
        // ...
    }

And in the getResources method you can now read the start/end date from $this->currentView to query your resources accordingly.

the downside is that the resources are loaded twice each time.

lukas-frey commented 1 month ago

Actually, there's a much simpler option since a few days now.

I didn't test it yet, but you should be able to do this:

public function getOptions () {
    return [
       'filterResourcesWithEvents' => true,
    ];
}

It'll only show resources which have events assigned for the current view.

aeq-dev commented 1 month ago

Thank you so much ! the first option helped me, I'll try the second later :)