Skip to content
This repository was archived by the owner on Jun 27, 2024. It is now read-only.

Commit 8fae281

Browse files
committed
WIP
1 parent cea54ab commit 8fae281

File tree

6 files changed

+56
-26
lines changed

6 files changed

+56
-26
lines changed

app/app/Http/UserTableView.php

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function __invoke($resource = false, $paginateMethod)
3131
])->table(function (InertiaTable $table) {
3232
$table
3333
->withGlobalSearch()
34+
->defaultSort('name')
3435
->column(key: 'name', searchable: true, sortable: true, canBeHidden: false)
3536
->column(key: 'email', searchable: true, sortable: true)
3637
->column(key: 'language_code', label: 'Language')

app/tests/Browser/PaginationTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ public function it_generates_a_simple_paginator($url, $method)
103103
->orderBy('name')
104104
->get();
105105

106-
$pageTen = $method === 'simple' ? '?page=10' : '?cursor=eyJuYW1lIjoiUHJvZi4gVGhlbyBTY2hpbW1lbCIsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0';
106+
$cursor = '?cursor=' . str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(json_encode([
107+
'name' => $users->get(89)->name,
108+
'_pointsToNextItems' => true,
109+
])));
110+
111+
$pageTen = $method === 'simple' ? '?page=10' : $cursor;
107112

108113
$browser
109114
->visit($url)

js/HeaderCell.vue

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<template>
22
<th
33
v-show="!cell.hidden"
4+
class="no-padding"
45
:class="{ 'cursor-pointer': cell.sortable }"
56
>
67
<component
78
:is="cell.sortable ? 'button' : 'div'"
9+
class="py-3 px-6 w-full"
810
@click.prevent="onClick"
911
>
1012
<span class="flex flex-row items-center">

js/Table.vue

+13-16
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ watch(queryBuilderData, () => {
448448
//
449449
450450
function sortBy(column) {
451-
queryBuilderData.value.sort = queryBuilderData.value.sort == column ? `-${column}` : column;
451+
if(queryBuilderData.value.sort == column) {
452+
queryBuilderData.value.sort = `-${column}`;
453+
} else {
454+
queryBuilderData.value.sort = column;
455+
}
456+
452457
queryBuilderData.value.cursor = null;
453458
queryBuilderData.value.page = 1;
454459
}
@@ -460,20 +465,9 @@ function show(key) {
460465
}
461466
462467
function header(key) {
463-
let columnSort = false;
464-
465-
const sort = queryBuilderProps.value.sort;
466-
467-
if (sort == key) {
468-
columnSort = "asc";
469-
} else if (sort == `-${key}`) {
470-
columnSort = "desc";
471-
}
472-
473468
const intKey = findDataKey("columns", key);
474469
const columnData = clone(queryBuilderProps.value.columns[intKey]);
475470
476-
columnData.sorted = columnSort;
477471
columnData.onSort = sortBy;
478472
479473
return columnData;
@@ -485,16 +479,19 @@ table :deep(th) {
485479
font-weight: 500;
486480
font-size: 0.75rem;
487481
line-height: 1rem;
488-
padding-top: 0.75rem;
489-
padding-bottom: 0.75rem;
490-
padding-left: 1.5rem;
491-
padding-right: 1.5rem;
492482
text-align: left;
493483
--tw-text-opacity: 1;
494484
color: rgba(107, 114, 128, var(--tw-text-opacity));
495485
letter-spacing: 0.05em;
496486
}
497487
488+
table :deep(th:not(.no-padding)) {
489+
padding-top: 0.75rem;
490+
padding-bottom: 0.75rem;
491+
padding-left: 1.5rem;
492+
padding-right: 1.5rem;
493+
}
494+
498495
table :deep(td) {
499496
font-size: 0.875rem;
500497
line-height: 1.25rem;

php/Column.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public function __construct(
1111
public string $label,
1212
public bool $canBeHidden,
1313
public bool $hidden,
14-
public bool $sortable
14+
public bool $sortable,
15+
public bool|string $sorted
1516
) {
1617
}
1718

@@ -23,6 +24,7 @@ public function toArray()
2324
'can_be_hidden' => $this->canBeHidden,
2425
'hidden' => $this->hidden,
2526
'sortable' => $this->sortable,
27+
'sorted' => $this->sorted,
2628
];
2729
}
2830
}

php/InertiaTable.php

+31-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class InertiaTable
1515
private Collection $columns;
1616
private Collection $searchInputs;
1717
private Collection $filters;
18+
private string $defaultSort = '';
1819

1920
public function __construct(Request $request)
2021
{
@@ -37,6 +38,19 @@ public function name(string $name): self
3738
return $this;
3839
}
3940

41+
/**
42+
* Default sort for this table.
43+
*
44+
* @param string $defaultSort
45+
* @return self
46+
*/
47+
public function defaultSort(string $defaultSort): self
48+
{
49+
$this->defaultSort = $defaultSort;
50+
51+
return $this;
52+
}
53+
4054
/**
4155
* Collects all properties and sets the default
4256
* values from the request query.
@@ -64,8 +78,8 @@ protected function getQueryBuilderProps(): array
6478
'globalSearch' => $this->searchInputs->firstWhere('key', 'global'),
6579

6680
'cursor' => $this->request->query('cursor'),
67-
'sort' => $this->request->query('sort'),
68-
'page' => Paginator::resolveCurrentPage(),
81+
'sort' => $this->request->query('sort', $this->defaultSort) ?: null,
82+
'page' => Paginator::resolveCurrentPage(),
6983
];
7084
}
7185

@@ -78,12 +92,20 @@ protected function transformColumns(): Collection
7892
{
7993
$columns = $this->request->query('columns', []);
8094

81-
if (empty($columns)) {
82-
return $this->columns;
83-
}
95+
$sort = $this->request->query('sort', $this->defaultSort);
96+
97+
return $this->columns->map(function (Column $column) use ($columns, $sort) {
98+
$key = $column->key;
8499

85-
return $this->columns->map(function (Column $column) use ($columns) {
86-
$column->hidden = ! in_array($column->key, $columns);
100+
if (! empty($columns)) {
101+
$column->hidden = ! in_array($key, $columns);
102+
}
103+
104+
if ($sort === $key) {
105+
$column->sorted = 'asc';
106+
} elseif ($sort === "-{$key}") {
107+
$column->sorted = 'desc';
108+
}
87109

88110
return $column;
89111
});
@@ -147,7 +169,8 @@ public function column(string $key = null, string $label = null, bool $canBeHidd
147169
label: $label ?: Str::headline($key),
148170
canBeHidden: $canBeHidden,
149171
hidden: $hidden,
150-
sortable: $sortable
172+
sortable: $sortable,
173+
sorted: false
151174
));
152175

153176
if ($searchable) {

0 commit comments

Comments
 (0)