Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Ginkelsoft DataTables is a flexible and easy-to-use package for managing tabular
php artisan vendor:publish --provider="Ginkelsoft\\DataTables\\DataTableServiceProvider" --tag=views
```

3. **Publish configuration file** (optional) for customization:

```bash
php artisan vendor:publish --provider="Ginkelsoft\DataTables\Providers\GinkelsoftDataTableServiceProvider" --tag=config
```

---

## Usage With Livewire
Expand Down
47 changes: 47 additions & 0 deletions config/datatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php


return [

/*
|--------------------------------------------------------------------------
| Default DataTable Settings
|--------------------------------------------------------------------------
|
| These settings define the default behavior of the DataTable component.
| You can override these settings by passing parameters to the component.
|
*/
'per_page' => [
'active' => true,
'options' => [10, 25, 50, 100],
'default' => 10,
],

'sort' => [
'active' => true,
'column' => 'id',
'direction' => 'asc',
],

'columns' => [
'hidden' => [],
],

'filters' => [
'active' => true,
],

'row_actions' => [
'view' => 'datatable::row-actions',
],

'bulk_actions' => [
'active' => true,
],

'search' => [
'active' => true,
],

];
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<div class="hidden md:block">
<select wire:change="updatePerPage($event.target.value)" id="perPage"
class="border border-gray-300 rounded-lg px-3 py-2 text-sm w-24 focus:ring-2 focus:ring-blue-400 transition">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
@foreach(config('datatable.per_page.options') as $perPage)
<option value="{{ $perPage }}" @if($perPage == $this->perPage) selected @endif>{{ $perPage }}</option>
@endforeach
</select>
<label for="perPage" class="text-sm text-gray-600">{{ __('datatable::datatables.results_per_page') }}</label>
</div>
4 changes: 2 additions & 2 deletions resources/views/vendor/datatables/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<table class="w-full min-w-max divide-y divide-gray-200 border border-gray-300 rounded-lg shadow-sm hidden md:table">
<thead class="bg-gray-50">
<tr>
@if (count($bulkActions) > 0)
@if (config('datatable.bulk_actions.active') && count($bulkActions) > 0)
<th class="px-4 py-3 text-left text-sm font-semibold text-gray-700 border">
<input type="checkbox" wire:click="toggleSelectAll"
class="h-5 w-5 text-blue-500 border-gray-300 rounded focus:ring-2 focus:ring-blue-400 transition">
Expand Down Expand Up @@ -36,7 +36,7 @@ class="h-5 w-5 text-blue-500 border-gray-300 rounded focus:ring-2 focus:ring-blu
@foreach($rows as $row)
<tr class="hover:bg-gray-100 transition cursor-pointer"
wire:click.prevent="toggleRowSelection({{ $row->id }})">
@if (count($bulkActions) > 0)
@if (config('datatable.bulk_actions.active') && count($bulkActions) > 0)
<td class="px-4 py-3 border">
<input type="checkbox" wire:model="selectedRows"
value="{{ $row->id }}"
Expand Down
9 changes: 8 additions & 1 deletion resources/views/vendor/datatables/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@

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

@if (config('datatable.search.active'))
<div class="flex items-center gap-2">
@includeIf('datatable::components.search')
</div>
@endif


@if(config('datatable.per_page.active'))
<div class="flex items-center gap-4">
<div class="flex items-center gap-2">
@includeIf('datatable::components.page-result')
</div>
</div>
@endif

</div>

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

@if (config('datatable.filters.active'))
<div class="flex items-center gap-2">
@if(!empty($filters))
<div>
@includeIf('datatable::filters', ['filters' => $filters])
</div>
@endif
</div>
@endif

@if(count($selectedRows) > 0 && count($bulkActions) > 0)
@if(config('datatable.bulk_actions.active') && count($selectedRows) > 0 && count($bulkActions) > 0)
<div class="flex items-center gap-2">
@includeIf('datatable::components.bulk-action')
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/DataTableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DataTableServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/datatable.php', 'datatable');
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/vendor/datatables', 'datatable');
$this->loadViewsFrom(__DIR__ . '/../resources/views/vendor/datatables', 'datatable');
}
Expand All @@ -30,6 +31,13 @@ public function register(): void
*/
public function boot(): void
{
$this->publishes([
__DIR__ . '/../config/datatable.php' => config_path('datatable.php'),
], 'config');

// Merge default config if not published
$this->mergeConfigFrom(__DIR__ . '/../config/datatable.php', 'datatable');

$this->publishes([
__DIR__ . '/../resources/views' => resource_path('views/vendor/datatables'),
], 'views');
Expand Down
13 changes: 10 additions & 3 deletions src/Livewire/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,26 @@ public function mount(
string $rowActionView = 'datatable::row-actions',
array $hiddenColumns = [],
array $bulkActions = [],
array $filters = []
array $filters = [],
string $sortColumn = '',
string $sortDirection = '',
string $perPage = '',
): void
{
$this->model = $model;
$this->columns = $columns;
$this->rowAction = array_map(fn($action) => $action instanceof Action ? $action->toArray() : $action, $rowActions);
$this->rowActionView = $rowActionView;
$this->hiddenColumns = $hiddenColumns;
$this->rowActionView = $rowActionView ?: config('datatable.row_actions.view');
$this->hiddenColumns = $hiddenColumns ?: config('datatable.columns.hidden');
$this->bulkActions = $bulkActions;
$this->filters = collect($filters)
->map(fn($filter) => FilterFactory::make($filter)->toArray())
->collapse()
->toArray();

$this->sortColumn = $sortColumn ?: config('datatable.sort.column');
$this->sortDirection = $sortDirection ?: config('datatable.sort.direction');
$this->perPage = $perPage ?: config('datatable.per_page.default');
}

/**
Expand Down