Fixing some examples

This commit is contained in:
David
2025-05-03 01:57:26 -04:00
parent 9f12bc28d0
commit d64e9c00be
13 changed files with 132 additions and 29 deletions

17
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"C:\\xampp\\htdocs\\generic_table_examples": "${workspaceFolder}",
}
}
]
}

View File

@@ -3,6 +3,7 @@
namespace App\Livewire\Examples; namespace App\Livewire\Examples;
use App\Tables\DepartmentTable; use App\Tables\DepartmentTable;
use App\Tables\SubDepartmentsTable;
use App\Tables\TableWithDragDropOrdering; use App\Tables\TableWithDragDropOrdering;
use Livewire\Component; use Livewire\Component;
@@ -12,9 +13,15 @@ class WithTwoTablesComponent extends Component
{ {
return view('livewire.examples.with-two-tables-component', [ return view('livewire.examples.with-two-tables-component', [
'products' => TableWithDragDropOrdering::class, 'products' => TableWithDragDropOrdering::class,
'departments' => DepartmentTable::class 'departments' => DepartmentTable::class,
'subDepartments' => SubDepartmentsTable::class
]) ])
->extends('components.layouts.app') ->extends('components.layouts.app')
->section('content'); ->section('content');
} }
public function showModal()
{
$this->dispatch('showModal');
}
} }

View File

@@ -9,6 +9,6 @@ class Department extends Model
{ {
public function group() : BelongsTo public function group() : BelongsTo
{ {
return $this->belongsTo(DepartmentGroup::class, 'id'); return $this->belongsTo(DepartmentGroup::class, 'department_group_id');
} }
} }

View File

@@ -2,7 +2,7 @@
namespace App\Tables; namespace App\Tables;
use App\Models\Product; use App\Models\Department;
use Mmt\GenericTable\Components\Column; use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Components\ColumnCollection; use Mmt\GenericTable\Components\ColumnCollection;
use Mmt\GenericTable\Interfaces\IDragDropReordering; use Mmt\GenericTable\Interfaces\IDragDropReordering;
@@ -24,6 +24,6 @@ class DepartmentTable implements IGenericTable, IDragDropReordering
$columns->add(new Column("Status")); $columns->add(new Column("Status"));
$columns->add(new Column("Order")->sortable()->defaultSortDesc()); $columns->add(new Column("Order")->sortable()->defaultSortDesc());
$this->tableSettings = new GenericTableSettings(new Product(), $columns); $this->tableSettings = new GenericTableSettings(new Department(), $columns);
} }
} }

View File

@@ -55,4 +55,9 @@ class IconColumn implements IColumn, IColumnRenderer
$this->setIconCallback = $callback; $this->setIconCallback = $callback;
return $this; return $this;
} }
public function isRelationship(): bool
{
return false;
}
} }

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Tables;
use App\Models\SubDepartment;
use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Components\ColumnCollection;
use Mmt\GenericTable\Interfaces\IDragDropReordering;
use Mmt\GenericTable\Interfaces\IGenericTable;
use Mmt\GenericTable\Support\GenericTableSettings;
class SubDepartmentsTable implements IGenericTable
{
public GenericTableSettings $tableSettings;
public function __construct()
{
$columns = new ColumnCollection();
$columns->add(new Column("Id", 'id as uid'));
$columns->add(new Column("Sub Department Name", 'name'));
$columns->add(new Column("Status"));
$columns->add(new Column("Department Name", 'department.name'));
$this->tableSettings = new GenericTableSettings(new SubDepartment(), $columns);
}
}

View File

@@ -12,7 +12,7 @@ class TableWithActionColumn implements IGenericTable, IActionColumn
{ {
use WithExportableProductColumns; use WithExportableProductColumns;
public int $actionColumnIndex = 0; public int $actionColumnIndex = 2;
public function actionView(IRowData $row): \Illuminate\View\View public function actionView(IRowData $row): \Illuminate\View\View
{ {

View File

@@ -26,6 +26,7 @@ class TableWithBindedRoutes implements IGenericTable, IDragDropReordering
new Column('Price'), new Column('Price'),
new Column('Stock'), new Column('Stock'),
new Column('SubDepartment', 'subDepartment.name'), new Column('SubDepartment', 'subDepartment.name'),
new Column('Order')->hide()
); );
$this->tableSettings = new GenericTableSettings( $this->tableSettings = new GenericTableSettings(

View File

@@ -3,9 +3,17 @@
namespace App\Tables; namespace App\Tables;
use App\Tables\Traits\WithColumnFormatter; use App\Tables\Traits\WithColumnFormatter;
use Mmt\GenericTable\Interfaces\IExportable;
use Mmt\GenericTable\Interfaces\IGenericTable; use Mmt\GenericTable\Interfaces\IGenericTable;
class TableWithColumnFormatter implements IGenericTable class TableWithColumnFormatter implements IGenericTable, IExportable
{ {
use WithColumnFormatter; use WithColumnFormatter;
public function onExport(\Mmt\GenericTable\Support\ExportEventArgs $args): \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
{
$args->useStripTags = true;
$args->query->take(5);
return $args->export();
}
} }

View File

@@ -44,7 +44,8 @@ class TableWithFilters implements IGenericTable, IEvent, IDateRangeFilter, IColu
new Column('Id')->hookFormatter(fn(IRowData $row) => '#'.$row->get('id')), new Column('Id')->hookFormatter(fn(IRowData $row) => '#'.$row->get('id')),
new Column('Order')->sortable()->defaultSort(), new Column('Order')->sortable()->defaultSort(),
new Column('Name'), new Column('Name'),
new Column('Price') new Column('Price'),
new Column('Status')->hide()
); );
$this->filters = new FilterCollection( $this->filters = new FilterCollection(

View File

@@ -5,38 +5,37 @@ namespace App\Tables;
use App\Models\Product; use App\Models\Product;
use Mmt\GenericTable\Components\Column; use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Components\ColumnCollection; use Mmt\GenericTable\Components\ColumnCollection;
use Mmt\GenericTable\Enums\DatabaseEventQueryState; use Mmt\GenericTable\Interfaces\IExportable;
use Mmt\GenericTable\Interfaces\IEvent;
use Mmt\GenericTable\Interfaces\IGenericTable; use Mmt\GenericTable\Interfaces\IGenericTable;
use Mmt\GenericTable\Support\DatabaseEvent;
use Mmt\GenericTable\Support\GenericTableSettings; use Mmt\GenericTable\Support\GenericTableSettings;
class TableWithRelationships implements IGenericTable, IEvent class TableWithRelationships implements IGenericTable, IExportable
{ {
public GenericTableSettings $tableSettings; public GenericTableSettings $tableSettings;
public function __construct() public function __construct()
{ {
$columns = new ColumnCollection()->add( $columns = ColumnCollection::make(
new Column('Name'), new Column('Product Name', 'name')
new Column('Department'), ->searchable()
new Column('SubDepartment'), ->sortable(),
new Column('Department Name', 'subDepartment.department.name')
->searchable()
->sortable(),
new Column('SubDepartment Name', 'subDepartment.name')
->searchable()
->exportable()
->sortable(),
); );
$this->tableSettings = new GenericTableSettings(new Product(), $columns, true); $this->tableSettings = new GenericTableSettings(new Product(), $columns);
} }
public function dispatchCallback(\Mmt\GenericTable\Support\EventArgs $arguments): void
public function onExport(\Mmt\GenericTable\Support\ExportEventArgs $args): \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
{ {
if($arguments instanceof DatabaseEvent) { $args->query->take(5);
if($arguments->queryState == DatabaseEventQueryState::INITIALIZING) { return $args->export();
$arguments->builder->join('sub_departments', 'sub_departments.id', 'products.sub_department_id')
->join('departments', 'departments.id', 'sub_departments.department_id')
->select(
'products.name as name',
'departments.name as department',
'sub_departments.name as sub_department'
);
}
}
} }
} }

View File

@@ -21,6 +21,8 @@
</div> </div>
</div> </div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
@yield('js')
</body> </body>
</html> </html>

View File

@@ -1,5 +1,27 @@
<div> <div>
<div class="row"> <div class="modal fade" tabindex="-1" id = "subDepartments" wire:ignore.self>
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Sub Departamentos</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
@generic_table($subDepartments)
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<div class="row g-3">
<div class="col-12">
<button class="btn btn-primary" wire:click = "showModal">See sub departments</button>
</div>
<div class="col-7"> <div class="col-7">
<div class="card"> <div class="card">
<div class="card-body p-0"> <div class="card-body p-0">
@@ -16,4 +38,19 @@
</div> </div>
</div> </div>
</div> </div>
{{-- @script --}}
{{-- @endscript --}}
</div> </div>
@section('js')
<script>
document.addEventListener('livewire:initialized', () => {
@this.on('showModal', () => {
const myModal = new bootstrap.Modal('#subDepartments');
myModal.show();
})
})
</script>
@endsection