Skip to content

Commit 985f4bd

Browse files
committed
Primary keys
- Added ability to define primary key of rows for bulk select - Added selectedKeys property that returns an array of the ids of the selected rows
1 parent 3e6277f commit 985f4bd

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
88

99
- Ability to disable pagination (https://github.com/rappasoft/laravel-livewire-tables/pull/222)
1010
- Ability to define the sorting direction names for each column. i.e. A-Z, Z-A, Yes, No, Enabled, Disabled, etc.
11+
- Added ability to define primary key of rows for bulk select
12+
- Added selectedKeys property that returns an array of the ids of the selected rows
1113

1214
### Changed
1315

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,16 @@ And then using it like this:
375375
}
376376
```
377377

378-
### Creating Bulk Actions
378+
### Bulk Actions
379379

380380
Bulk actions are not required, and the bulk actions box, as well as the left-hand checkboxes will be hidden if none are defined.
381381

382+
#### The primary key
383+
384+
Each row must have a primary key, it's `'id'` by default but you can override it with the `$primaryKey` property.
385+
386+
#### Defining Bulk Actions
387+
382388
To define your bulk actions, you add them to the **$bulkActions** array.
383389

384390
```php
@@ -404,6 +410,29 @@ public function exportSelected()
404410

405411
In the component you have access to `$this->selectedRowsQuery` which is a **Builder** instance of the selected rows.
406412

413+
You may also call `selectedKeys` which gives you an array of the primary keys in order they were selected:
414+
415+
**Note:** See above to setting the primary key if not `'id`.
416+
417+
```php
418+
public function exportSelected()
419+
{
420+
if (count($this->selectedKeys)) {
421+
// Do something with the selected rows
422+
dd($this->selectedKeys);
423+
424+
// => [
425+
// 1,
426+
// 2,
427+
// 3,
428+
// 4,
429+
// ]
430+
}
431+
432+
// Notify there is nothing to export
433+
}
434+
```
435+
407436
### Options
408437

409438
There are some class level properties you can set:
@@ -420,6 +449,7 @@ There are some class level properties you can set:
420449
| $sortDirectionNames | [] | string[] | Change the direction name of the column for the sorting pill display (i.e. A-Z, Z-A) |
421450
| $perPage | 10 | int | The default per page amount selected (must exist in list) |
422451
| $perPageAccepted | [10, 25, 50] | int[] | The values for the per page dropdown, in order |
452+
| $primaryKey | id | string | The column to pluck for bulk actions to populate the `selectedKeys` property |
423453
| $searchFilterDebounce | null | null/int | Adds a debounce of `$searchFilterDebounce` ms to the search input |
424454
| $searchFilterDefer | null | null/bool | Adds `.defer` to the search input |
425455
| $searchFilterLazy | null | null/bool | Adds `.lazy` to the search input |

src/Traits/WithBulkActions.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
trait WithBulkActions
99
{
10+
public string $primaryKey = 'id';
1011
public bool $showFilters = true;
1112
public bool $selectPage = false;
1213
public bool $selectAll = false;
@@ -40,7 +41,7 @@ public function updatedSelectPage($value): void
4041

4142
public function selectPageRows(): void
4243
{
43-
$this->selected = $this->rows->pluck('id')->map(fn ($id) => (string) $id);
44+
$this->selected = $this->rows->pluck($this->primaryKey)->map(fn ($id) => (string) $id);
4445
}
4546

4647
public function selectAll(): void
@@ -60,4 +61,9 @@ public function getSelectedRowsQueryProperty()
6061
return (clone $this->rowsQuery())
6162
->unless($this->selectAll, fn ($query) => $query->whereKey($this->selected));
6263
}
64+
65+
public function getSelectedKeysProperty()
66+
{
67+
return $this->selectedRowsQuery->pluck($this->primaryKey)->toArray();
68+
}
6369
}

0 commit comments

Comments
 (0)