Skip to content

Commit 0c7aece

Browse files
Feature/added config (#21)
1 parent eb8f643 commit 0c7aece

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Ginkelsoft DataTables is a flexible and easy-to-use package for managing tabular
4040
php artisan vendor:publish --provider="Ginkelsoft\\DataTables\\DataTableServiceProvider" --tag=views
4141
```
4242

43+
3. **Publish configuration file** (optional) for customization:
44+
45+
```bash
46+
php artisan vendor:publish --provider="Ginkelsoft\DataTables\Providers\GinkelsoftDataTableServiceProvider" --tag=config
47+
```
48+
4349
---
4450

4551
## Usage With Livewire

config/datatable.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
4+
return [
5+
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Default DataTable Settings
9+
|--------------------------------------------------------------------------
10+
|
11+
| These settings define the default behavior of the DataTable component.
12+
| You can override these settings by passing parameters to the component.
13+
|
14+
*/
15+
'per_page' => [
16+
'active' => true,
17+
'options' => [10, 25, 50, 100],
18+
'default' => 10,
19+
],
20+
21+
'sort' => [
22+
'active' => true,
23+
'column' => 'id',
24+
'direction' => 'asc',
25+
],
26+
27+
'columns' => [
28+
'hidden' => [],
29+
],
30+
31+
'filters' => [
32+
'active' => true,
33+
],
34+
35+
'row_actions' => [
36+
'view' => 'datatable::row-actions',
37+
],
38+
39+
'bulk_actions' => [
40+
'active' => true,
41+
],
42+
43+
'search' => [
44+
'active' => true,
45+
],
46+
47+
];
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<div class="hidden md:block">
22
<select wire:change="updatePerPage($event.target.value)" id="perPage"
33
class="border border-gray-300 rounded-lg px-3 py-2 text-sm w-24 focus:ring-2 focus:ring-blue-400 transition">
4-
<option value="10">10</option>
5-
<option value="25">25</option>
6-
<option value="50">50</option>
7-
<option value="100">100</option>
4+
@foreach(config('datatable.per_page.options') as $perPage)
5+
<option value="{{ $perPage }}" @if($perPage == $this->perPage) selected @endif>{{ $perPage }}</option>
6+
@endforeach
87
</select>
98
<label for="perPage" class="text-sm text-gray-600">{{ __('datatable::datatables.results_per_page') }}</label>
109
</div>

resources/views/vendor/datatables/components/table.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<table class="w-full min-w-max divide-y divide-gray-200 border border-gray-300 rounded-lg shadow-sm hidden md:table">
33
<thead class="bg-gray-50">
44
<tr>
5-
@if (count($bulkActions) > 0)
5+
@if (config('datatable.bulk_actions.active') && count($bulkActions) > 0)
66
<th class="px-4 py-3 text-left text-sm font-semibold text-gray-700 border">
77
<input type="checkbox" wire:click="toggleSelectAll"
88
class="h-5 w-5 text-blue-500 border-gray-300 rounded focus:ring-2 focus:ring-blue-400 transition">
@@ -36,7 +36,7 @@ class="h-5 w-5 text-blue-500 border-gray-300 rounded focus:ring-2 focus:ring-blu
3636
@foreach($rows as $row)
3737
<tr class="hover:bg-gray-100 transition cursor-pointer"
3838
wire:click.prevent="toggleRowSelection({{ $row->id }})">
39-
@if (count($bulkActions) > 0)
39+
@if (config('datatable.bulk_actions.active') && count($bulkActions) > 0)
4040
<td class="px-4 py-3 border">
4141
<input type="checkbox" wire:model="selectedRows"
4242
value="{{ $row->id }}"

resources/views/vendor/datatables/datatable.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,36 @@
33

44
<div class="flex justify-between items-center mb-4 gap-4">
55

6+
@if (config('datatable.search.active'))
67
<div class="flex items-center gap-2">
78
@includeIf('datatable::components.search')
89
</div>
10+
@endif
11+
912

13+
@if(config('datatable.per_page.active'))
1014
<div class="flex items-center gap-4">
1115
<div class="flex items-center gap-2">
1216
@includeIf('datatable::components.page-result')
1317
</div>
1418
</div>
19+
@endif
1520

1621
</div>
1722

1823
<div class="flex justify-between items-center mb-4">
1924

25+
@if (config('datatable.filters.active'))
2026
<div class="flex items-center gap-2">
2127
@if(!empty($filters))
2228
<div>
2329
@includeIf('datatable::filters', ['filters' => $filters])
2430
</div>
2531
@endif
2632
</div>
33+
@endif
2734

28-
@if(count($selectedRows) > 0 && count($bulkActions) > 0)
35+
@if(config('datatable.bulk_actions.active') && count($selectedRows) > 0 && count($bulkActions) > 0)
2936
<div class="flex items-center gap-2">
3037
@includeIf('datatable::components.bulk-action')
3138
</div>

src/DataTableServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DataTableServiceProvider extends ServiceProvider
1616
*/
1717
public function register(): void
1818
{
19+
$this->mergeConfigFrom(__DIR__ . '/../config/datatable.php', 'datatable');
1920
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/vendor/datatables', 'datatable');
2021
$this->loadViewsFrom(__DIR__ . '/../resources/views/vendor/datatables', 'datatable');
2122
}
@@ -30,6 +31,13 @@ public function register(): void
3031
*/
3132
public function boot(): void
3233
{
34+
$this->publishes([
35+
__DIR__ . '/../config/datatable.php' => config_path('datatable.php'),
36+
], 'config');
37+
38+
// Merge default config if not published
39+
$this->mergeConfigFrom(__DIR__ . '/../config/datatable.php', 'datatable');
40+
3341
$this->publishes([
3442
__DIR__ . '/../resources/views' => resource_path('views/vendor/datatables'),
3543
], 'views');

src/Livewire/DataTableComponent.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,26 @@ public function mount(
9292
string $rowActionView = 'datatable::row-actions',
9393
array $hiddenColumns = [],
9494
array $bulkActions = [],
95-
array $filters = []
95+
array $filters = [],
96+
string $sortColumn = '',
97+
string $sortDirection = '',
98+
string $perPage = '',
9699
): void
97100
{
98101
$this->model = $model;
99102
$this->columns = $columns;
100103
$this->rowAction = array_map(fn($action) => $action instanceof Action ? $action->toArray() : $action, $rowActions);
101-
$this->rowActionView = $rowActionView;
102-
$this->hiddenColumns = $hiddenColumns;
104+
$this->rowActionView = $rowActionView ?: config('datatable.row_actions.view');
105+
$this->hiddenColumns = $hiddenColumns ?: config('datatable.columns.hidden');
103106
$this->bulkActions = $bulkActions;
104107
$this->filters = collect($filters)
105108
->map(fn($filter) => FilterFactory::make($filter)->toArray())
106109
->collapse()
107110
->toArray();
111+
112+
$this->sortColumn = $sortColumn ?: config('datatable.sort.column');
113+
$this->sortDirection = $sortDirection ?: config('datatable.sort.direction');
114+
$this->perPage = $perPage ?: config('datatable.per_page.default');
108115
}
109116

110117
/**

0 commit comments

Comments
 (0)