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 bulk actions function #467

Merged
merged 4 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: run-tests

on: [push, pull_request]


jobs:
test:
runs-on: ${{ matrix.os }}
Expand Down
8 changes: 4 additions & 4 deletions resources/views/bootstrap-4/includes/bulk-actions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
</button>

<div class="dropdown-menu dropdown-menu-right w-100" aria-labelledby="bulkActions">
@foreach($bulkActions as $action => $title)
@foreach($this->bulkActions as $action => $title)
<a
href="#"
wire:click.prevent="{{ $action }}"
wire:key="bulk-action-{{ $action }}"
class="dropdown-item"
wire:click.prevent="{{ $action }}"
wire:key="bulk-action-{{ $action }}"
class="dropdown-item"
>
{{ $title }}
</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if ($bulkActionsEnabled && count($bulkActions) && (($selectPage && $rows->total() > $rows->count()) || count($selected)))
@if ($bulkActionsEnabled && count($this->bulkActions) && (($selectPage && $rows->total() > $rows->count()) || count($selected)))
<x-livewire-tables::bs4.table.row wire:key="row-message">
<x-livewire-tables::bs4.table.cell colspan="{{ $colspan }}">
@if (count($selected) && !$selectAll && !$selectPage)
Expand Down
6 changes: 3 additions & 3 deletions resources/views/bootstrap-4/includes/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<x-livewire-tables::bs4.table.heading />
@endif

@if ($bulkActionsEnabled && count($bulkActions))
@if ($bulkActionsEnabled && count($this->bulkActions))
<x-livewire-tables::bs4.table.heading>
<input
wire:model="selectPage"
Expand Down Expand Up @@ -37,7 +37,7 @@
<x-slot name="body">
@php
$colspan = count($columns);
if ($bulkActionsEnabled && count($bulkActions)) $colspan++;
if ($bulkActionsEnabled && count($this->bulkActions)) $colspan++;
if ($reordering) $colspan++;
@endphp

Expand All @@ -62,7 +62,7 @@
</x-livewire-tables::bs4.table.cell>
@endif

@if ($bulkActionsEnabled && count($bulkActions))
@if ($bulkActionsEnabled && count($this->bulkActions))
<x-livewire-tables::bs4.table.cell>
<input
wire:model="selected"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</button>

<div class="dropdown-menu dropdown-menu-end w-100" aria-labelledby="bulkActions">
@foreach($bulkActions as $action => $title)
@foreach($this->bulkActions as $action => $title)
<a
href="#"
wire:click.prevent="{{ $action }}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if ($bulkActionsEnabled && count($bulkActions) && (($selectPage && $rows->total() > $rows->count()) || count($selected)))
@if ($bulkActionsEnabled && count($this->bulkActions) && (($selectPage && $rows->total() > $rows->count()) || count($selected)))
<x-livewire-tables::bs5.table.row wire:key="row-message">
<x-livewire-tables::bs5.table.cell colspan="{{ $colspan }}">
@if (count($selected) && !$selectAll && !$selectPage)
Expand Down
6 changes: 3 additions & 3 deletions resources/views/bootstrap-5/includes/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<x-livewire-tables::bs5.table.heading />
@endif

@if ($bulkActionsEnabled && count($bulkActions))
@if ($bulkActionsEnabled && count($this->bulkActions))
<x-livewire-tables::bs5.table.heading>
<input
wire:model="selectPage"
Expand Down Expand Up @@ -38,7 +38,7 @@ class="form-check-input"
<x-slot name="body">
@php
$colspan = count($columns);
if ($bulkActionsEnabled && count($bulkActions)) $colspan++;
if ($bulkActionsEnabled && count($this->bulkActions)) $colspan++;
if ($reordering) $colspan++;
@endphp

Expand All @@ -63,7 +63,7 @@ class="form-check-input"
</x-livewire-tables::bs5.table.cell>
@endif

@if ($bulkActionsEnabled && count($bulkActions))
@if ($bulkActionsEnabled && count($this->bulkActions))
<x-livewire-tables::bs5.table.cell class="align-middle">
<input
wire:model="selected"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/tailwind/includes/bulk-actions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class="origin-top-right absolute right-0 mt-2 w-full md:w-48 rounded-md shadow-l
>
<div class="rounded-md bg-white shadow-xs dark:bg-gray-700 dark:text-white">
<div class="py-1" role="menu" aria-orientation="vertical" aria-labelledby="options-menu">
@foreach($bulkActions as $action => $title)
@foreach($this->bulkActions as $action => $title)
<button
wire:click="{{ $action }}"
wire:key="bulk-action-{{ $action }}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if ($bulkActionsEnabled && count($bulkActions) && (($selectPage && $rows->total() > $rows->count()) || count($selected)))
@if ($bulkActionsEnabled && count($this->bulkActions) && (($selectPage && $rows->total() > $rows->count()) || count($selected)))
<x-livewire-tables::table.row wire:key="row-message" class="bg-indigo-50 dark:bg-gray-900 dark:text-white">
<x-livewire-tables::table.cell :colspan="$colspan">
@if (count($selected) && !$selectAll && !$selectPage)
Expand Down
6 changes: 3 additions & 3 deletions resources/views/tailwind/includes/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<x-livewire-tables::table.heading />
@endif

@if ($bulkActionsEnabled && count($bulkActions))
@if ($bulkActionsEnabled && count($this->bulkActions))
<x-livewire-tables::table.heading>
<div class="inline-flex rounded-md shadow-sm">
<input
Expand Down Expand Up @@ -40,7 +40,7 @@ class="rounded border-gray-300 text-indigo-600 shadow-sm transition duration-150
<x-slot name="body">
@php
$colspan = count($columns);
if ($bulkActionsEnabled && count($bulkActions)) $colspan++;
if ($bulkActionsEnabled && count($this->bulkActions)) $colspan++;
if ($reordering) $colspan++;
@endphp

Expand Down Expand Up @@ -71,7 +71,7 @@ class="rounded border-gray-300 text-indigo-600 shadow-sm transition duration-150
</x-livewire-tables::table.cell>
@endif

@if ($bulkActionsEnabled && count($bulkActions))
@if ($bulkActionsEnabled && count($this->bulkActions))
<x-livewire-tables::table.cell>
<div class="inline-flex rounded-md shadow-sm">
<input
Expand Down
18 changes: 16 additions & 2 deletions src/Traits/WithBulkActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait WithBulkActions
public bool $selectPage = false;
public bool $selectAll = false;
public $selected = [];
public array $bulkActions = [];

public bool $hideBulkActionsOnEmpty = false;

public function renderingWithBulkActions(): void
Expand Down Expand Up @@ -91,12 +91,26 @@ public function getSelectedKeysProperty(): array
return $this->selectedKeys();
}

public function getBulkActionsProperty(): array
{
return $this->bulkActions();
}

public function bulkActions(): array
{
if (property_exists($this, 'bulkActions')) {
return $this->bulkActions;
}

return [];
}

public function getShowBulkActionsDropdownProperty(): bool
{
$showBulkActions = false;

if ($this->bulkActionsEnabled) {
if (count($this->bulkActions)) {
if (count($this->bulkActions())) {
$showBulkActions = true;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/DataTableComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,11 @@ public function search_filter_alt_query_relation()
$this->tableAltQuery->filters['search'] = 'Cat';
$this->assertEquals(2, $this->tableAltQuery->rows->total());
}

/** @test */
public function bulk_actions_defined_with_function()
{
$this->table->selected[] = 1;
$this->assertEquals(1, $this->table->count());
}
}
10 changes: 10 additions & 0 deletions tests/Http/Livewire/PetsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

class PetsTable extends DataTableComponent
{
public function bulkActions(): array
{
return [ 'count' => 'Count selected'];
}

/**
* @return Builder
*/
Expand Down Expand Up @@ -36,4 +41,9 @@ public function columns(): array
->searchable(),
];
}

public function count(): int
{
return $this->selectedRowsQuery()->count();
}
}