Skip to content

Commit 658917d

Browse files
author
=
committed
test and docs
1 parent 63067a9 commit 658917d

File tree

3 files changed

+63
-47
lines changed

3 files changed

+63
-47
lines changed

CHANGELOG.md

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

55
## [Unreleased]
66

7+
## [dev-column-select] - 2022-05-14
8+
9+
- Added functionality to bookmark or deep link column selection
10+
- Added functionality to identify different datatable components as unique in column selection and filter arrays
11+
712
## [2.7.0] - 2022-05-07
813

914
### Added

docs/misc/multiple-tables.md

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,62 +33,25 @@ If you need the above, you should make them different components like so:
3333
<livewire:pending-users-table />
3434
```
3535

36-
## Introduction
37-
38-
By default, your table has a name of `table`, as well as an internal array called `$table` which saves its state to the query string.
39-
40-
The query string would look like this:
41-
42-
```php
43-
// Under the hood
44-
public array $queryString = [
45-
'table' => [
46-
'search' => null,
47-
'sort' => [],
48-
...
49-
],
50-
]
51-
```
52-
53-
In order to have multiple tables on the same page, you need to tell it how to save the state of each table.
54-
55-
## Setting the table name and data
56-
57-
If you have multiple tables on the same page and you want them to have independent state saved in the query string, you must set a table name and data array.
58-
59-
```php
60-
public string $tableName = 'users';
61-
public array $users = [];
62-
```
63-
64-
The data array must be the same name as the table name. This data array will remain blank, I tried to create it dynamically in the query string but Livewire doesn't support that, so you have to define it yourself. It is a workaround until Livewire supports dynamic properties for the query string.
36+
## Disabling the query string for multiple of the same component
6537

66-
Your query string will now look like this:
38+
If you must have multiple of the same component on the same page, you should disable the query string for those components so the query string state does not get replaced by one or the other:
6739

6840
```php
69-
// Under the hood
70-
public array $queryString = [
71-
'users' => [
72-
'search' => null,
73-
'sort' => [],
74-
...
75-
],
76-
// Other tables
77-
'roles' => [
78-
'search' => null,
79-
'sort' => [],
80-
...
81-
],
82-
]
41+
public function configure(): void
42+
{
43+
$this->setQueryStringDisabled();
44+
}
8345
```
8446

85-
## Disabling the query string for multiple of the same component
47+
## Disabling column selection for multiple of the same component
8648

87-
If you must have multiple of the same component on the same page, you should disable the query string for those components so the query string state does not get replaced by one or the other:
49+
You should also disable the columns selection for those components so the query string and session state does not get replaced by one or the other:
8850

8951
```php
9052
public function configure(): void
9153
{
92-
$this->setQueryStringDisabled();
54+
$this->setColumnSelectStatus(true);
55+
$this->setColumnSelectStatus(false);
9356
}
9457
```

tests/DataTableComponentTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,52 @@ public function primary_key_has_to_be_set(): void
4040
// ->call('setPrimaryKey', null)
4141
// ->call('setSearch', 'abcd');
4242
}
43+
44+
/** @test */
45+
public function fingerprint_will_always_be_the_same_for_same_datatable(): void
46+
{
47+
$this->assertSame(
48+
[
49+
$this->basicTable->dataTableFingerprint(),
50+
$this->basicTable->dataTableFingerprint(),
51+
$this->basicTable->dataTableFingerprint(),
52+
],
53+
[
54+
$this->basicTable->dataTableFingerprint(),
55+
$this->basicTable->dataTableFingerprint(),
56+
$this->basicTable->dataTableFingerprint(),
57+
]
58+
);
59+
$this->assertSame($this->basicTable->dataTableFingerprint(), $this->fingerprintingAlgo($this->basicTable::class));
60+
}
61+
62+
/** @test */
63+
public function datatable_fingerprints_will_be_different_for_each_table(): void
64+
{
65+
$mockTable = new class() extends PetsTable {
66+
};
67+
68+
$this->assertNotSame($this->basicTable->dataTableFingerprint(), $mockTable->dataTableFingerprint());
69+
}
70+
71+
/** @test */
72+
public function fingerprint_will_be_url_friendy(): void
73+
{
74+
$mocks = [];
75+
for ($i = 0; $i < 9; $i++) {
76+
$mocks[$i] = new class() extends PetsTable {
77+
};
78+
$this->assertFalse(filter_var('http://'.$mocks[$i]->dataTableFingerprint().'.dev', FILTER_VALIDATE_URL) === false);
79+
}
80+
// control
81+
$this->assertTrue(filter_var('http://[9/$].dev', FILTER_VALIDATE_URL) === false);
82+
}
83+
84+
protected function fingerPrintingAlgo($className)
85+
{
86+
$className = str_split($className);
87+
$crc32 = sprintf('%u', crc32(serialize($className)));
88+
89+
return base_convert($crc32, 10, 36);
90+
}
4391
}

0 commit comments

Comments
 (0)