Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pagination component and integrate into Table component #523

Merged

Conversation

iurigustavo
Copy link
Contributor

A new Pagination component has been created and added to the MaryServiceProvider. This component has been integrated into the Table component to manage table pagination. The addition of Pagination provides greater control and customization options for page number displays. This update also reduces complexity within the Table component by offloading pagination responsibilities.

Enable pagination outside table and add perPage parameter:

class ListTasks extends Component
{
    use Toast;
    use WithPagination;
    use ResetsPaginationWhenPropsChanges;

    #[Url]
    public string $name = '';

    #[Url]
    public array $sortBy = ['column' => 'id', 'direction' => 'asc'];

    #[Url]
    public int $perPage = 10;

    public function render()
    {
        return view('livewire.tasks.list-tasks')->with(
            [
                'tasks'   => $this->tasks(),
                'headers' => $this->headers(),
            ]
        );
    }

    public function tasks(): LengthAwarePaginator
    {
        return Task::query()
            ->when($this->name, fn(Builder $q) => $q->whereAny(['title', 'code', 'description'], 'like', "%$this->name%"))
            ->orderBy(...array_values($this->sortBy))
            ->paginate($this->perPage);
    }

    public function headers(): array
    {
        return [
            ['key' => 'id', 'label' => '#', 'class' => 'hidden lg:table-cell'],
            ['key' => 'code', 'label' => 'Code', 'sortBy' => 'code'],
            ['key' => 'title', 'label' => 'Title'],
            ['key' => 'status', 'label' => 'Status'],
            ['key' => 'expires_at', 'label' => 'Expires at', 'sortBy' => 'expires_at'],
        ];
    }

}
     @foreach($tasks as $task)
            <x-card title="{{ strtoupper($task->title) }}" subtitle="{{ $task->code }}" shadow separator>
                {{ $task->description }}
            </x-card>
            <x-pagination :rows="$tasks" wire:model.live="perPage"/>
     @endforeach
      <x-table :headers="$headers" :rows="$tasks" with-pagination per-page="perPage" :sort-by="$sortBy"/>
image

A new Pagination component has been created and added to the MaryServiceProvider. This component has been integrated into the Table component to manage table pagination. The addition of Pagination provides greater control and customization options for page number displays. This update also reduces complexity within the Table component by offloading pagination responsibilities.
@robsontenorio
Copy link
Owner

robsontenorio commented Jul 10, 2024

Great PR ❤️

The default value of $perPage was changed from an empty string to null in the Table component to avoid potential issues. Also, the condition in the isShowable method in the Pagination component was updated to check for a non-empty model name instead of a non-null one for better error handling.
@robsontenorio
Copy link
Owner

robsontenorio commented Jul 24, 2024

Hey @iurigustavo

It throws a warning when using default pagination without perPage configuration. Can you handle it? This configuration should be optional.

<x-table :headers="$headers" :rows="$users" with-pagination />

image

Ensure that the pagination component only binds the live model if the perPage parameter is set. This avoids potential issues when the perPage value is missing.
@iurigustavo
Copy link
Contributor Author

Both ways don't work:

<x-mary-pagination :rows="$rows" :per-page-values="$perPageValues" {{ $perPage ? "wire:model.live=$perPage" : '' }} />
<x-mary-pagination :rows="$rows" :per-page-values="$perPageValues" @if ($perPage) wire:model.live="{{ $perPage }}" @endif />

So I had to get a conditional (if) to work

@robsontenorio
Copy link
Owner

Thanks!

image image

@robsontenorio robsontenorio merged commit 078bb0f into robsontenorio:main Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants