diff --git a/.DS_Store b/.DS_Store
deleted file mode 100644
index 446532e5..00000000
Binary files a/.DS_Store and /dev/null differ
diff --git a/.gitignore b/.gitignore
index 847d15b6..a9c0e146 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,6 @@ composer.lock
docs
vendor
coverage
-.vscode/
\ No newline at end of file
+.DS_Store
+.vscode/
+.idea/
diff --git a/.phpunit.result.cache b/.phpunit.result.cache
new file mode 100644
index 00000000..03884ce7
--- /dev/null
+++ b/.phpunit.result.cache
@@ -0,0 +1 @@
+{"version":1,"defects":{"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_column":3,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_raw":3,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_width_property_from_column":3},"times":{"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_generate_an_array_of_columns_from_a_model":0.127,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #0":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #1":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #2":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #3":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #4":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #5":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #6":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #7":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_exclude_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_include_columns":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_rename_columns":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_table_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_scope":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_delete_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #0":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #1":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #2":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #3":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #4":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_column":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_raw":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_width_property_from_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_mount_using_the_class":0.054,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_set_a_default_sort":0.019,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_show_and_hide_a_column":0.039,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_order_results":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_text":0.026,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_boolean":0.026,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_strings_as_a_boolean":0.029,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_selects":0.025,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_numbers":0.059,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_base_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_one_relation_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_a_has_one_column":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_column_with_specific_aggregate":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_has_many_relation_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_relation_columns":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_belongs_to_relation_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_many_relation_columns":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a__where_query_for_belongs_to_many_relation_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mount_from_the_default_template_with_a_model":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_header_can_be_hidden_with_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_pagination_bar_can_be_hidden_with_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_per_page_with_a_property":0.023,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_include_columns_from_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_exclude_columns_from_a_property":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_hide_columns_from_a_property":0.021,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_date_format_from_a_property":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_time_format_from_a_property":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_sort_from_a_property":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::component_is_created_by_make_command":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::dot_nested_component_is_created_by_make_command":0.019,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::forward_slash_nested_component_is_created_by_make_command":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::multiword_component_is_created_by_make_command":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::pascal_case_component_is_automatically_converted_by_make_command":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command_on_nested_component":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::new_component_model_name_matches_option":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::a_component_is_not_created_with_a_reserved_class_name":0.013}}
\ No newline at end of file
diff --git a/.styleci.yml b/.styleci.yml
index f4d3cbc6..98874aab 100644
--- a/.styleci.yml
+++ b/.styleci.yml
@@ -1,4 +1,8 @@
preset: laravel
+enabled:
+ - concat_with_spaces
+
disabled:
+ - concat_without_spaces
- single_class_element_per_statement
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f65bc51a..a5e189b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
All notable changes to `livewire-datatables` will be documented in this file
-## 1.0.0 - 201X-XX-XX
+## 1.0.0 - 201X-XX-XX ( to be released in the future... )
- initial release
+
+## 0.9.0 ( 2022-03-22 )
+
+- Breaking Change: 'unsortable' has been renamed to 'sortable', which is more intuitive. Please adjust your overwritten views, if any (thyseus).
+
diff --git a/README.md b/README.md
index 4c664b6f..71b03b6f 100644
--- a/README.md
+++ b/README.md
@@ -3,21 +3,31 @@
[](https://packagist.org/packages/mediconesystems/livewire-datatables)
[](https://packagist.org/packages/mediconesystems/livewire-datatables)
+⚠️ Project Status: Unmaintained
+
+Thank you to everyone who has used, contributed to, or supported this project. Unfortunately, I’m no longer able to actively maintain it.
+I wanted to be transparent so that you can make informed decisions about using or forking the code.
+
+If you rely on this project and would like to take over maintenance or ownership, feel free to open an issue to discuss it. Otherwise, please treat this repository as archived and read-only.
+
+Thanks again for your understanding and for being part of the project.
+
### Features
- Use a model or query builder to supply data
- Mutate and format columns using preset or custom callbacks
- Sort data using column or computed column
- Filter using booleans, times, dates, selects or free text
+- Create complex combined filters using the [complex query builder](#complex-query-builder)
- Show / hide columns
-
-## [Live Demo App](https://livewire-datatables.com)
+- Column groups
+- Mass Action (Bulk) Support
## [Demo App Repo](https://github.com/MedicOneSystems/demo-livewire-datatables)

## Requirements
-- [Laravel 7](https://laravel.com/docs/7.x)
+- [Laravel 7, 8 or 9](https://laravel.com/docs/9.x)
- [Livewire](https://laravel-livewire.com/)
- [Tailwind](https://tailwindcss.com/)
- [Alpine JS](https://github.com/alpinejs/alpine)
@@ -29,10 +39,15 @@ You can install the package via composer:
```bash
composer require mediconesystems/livewire-datatables
```
+If you use laravel 9 first execute
+```bash
+composer require psr/simple-cache:^1.0 maatwebsite/excel
+```
+
### Optional
You don't need to, but if you like you can publish the config file and blade template assets:
```bash
-php artisan vendor:publish
+php artisan vendor:publish --provider="Mediconesystems\LivewireDatatables\LivewireDatatablesServiceProvider"
```
This will enable you to modify the blade views and apply your own styling, the datatables views will be published to resources/livewire/datatables. The config file contains the default time and date formats used throughout
> - This can be useful if you're using Purge CSS on your project, to make sure all the livewire-datatables classes get included
@@ -51,7 +66,7 @@ somewhere in your CSS
```html
...
-
+
...
```
@@ -61,51 +76,54 @@ somewhere in your CSS
```html
```
+*Attention*: Please note that having multiple datatables on the same page _or_ more than one datatable of the same
+type on different pages needs to have a unique `name` attribute assigned to each one so they do not conflict with each
+other as in the example above.
+
### Props
-| Property | Arguments | Result | Example |
-|----|----|----|----|
-|**model**|*String* full model name|Define the base model for the table| ```model="App\Post"```|
-|**include**|*String\|Array* of column definitions|specify columns to be shown in table, label can be specified by using \| delimter | ```include="name, email, dob|Birth Date, role"```|
-|**exclude**|*String\|Array* of column definitions|columns are excluded from table| ```:exclude="['created_at', 'updated_at']"```|
-|**hide**|*String\|Array* of column definitions|columns are present, but start hidden|```hidden="email_verified_at"```|
-|**dates**|*String\|Array* of column definitions [ and optional format in \| delimited string]|column values are formatted as per the default date format, or format can be included in string with \| separator | ```:dates="['dob|lS F y', 'created_at']"```|
-|**times**|*String\|Array* of column definitions [optional format in \| delimited string]|column values are formatted as per the default time format, or format can be included in string with \| separator | ```'bedtime|g:i A'```|
-|**searchable**|*String\|Array* of column names | Defines columns to be included in global search | ```searchable="name, email"```|
-|**sort**|*String* of column definition [and optional 'asc' or 'desc' (default: 'desc') in \| delimited string]|Specifies the column and direction for initial table sort. Default is column 0 descending | ```sort="name|asc"```|
-|**hide-header**|*Boolean* default: *false*|The top row of the table including the column titles is removed if this is ```true```| |
-|**hide-pagination**|*Boolean* default: *false*|Pagination controls are removed if this is ```true```| |
-|**per-page**|*Integer* default: 10|Number of rows per page| ```per-page="20"``` |
-|**exportable**|*Boolean* default: *false*|Allows tabel to bbe exported| `````` |
-|**hideable**| _String_ | gives ability to show/hide columns, accepts strings 'inline', 'buttons', or 'select'| `````` |
-|**beforeTableSlot**| _String_ |blade view to be included immediately before the table in the component, which can therefore access public properties| |
-|**afterTableSlot**| _String_ |blade view to be included immediately after the table in the component, which can therefore access public properties| [demo](https://livewire-datatables.com/complex) |
+| Property | Arguments | Result | Example |
+|----|----|------------------------------------------------------------------------------------------------------------------------------------------------|----|
+|**model**|*String* full model name| Define the base model for the table | ```model="App\Post"```|
+|**include**|*String\| Array* of column definitions |specify columns to be shown in table, label can be specified by using \| delimter | ```include="name, email, dob\|Birth Date, role"```|
+|**exclude**|*String\| Array* of column definitions |columns are excluded from table| ```:exclude="['created_at', 'updated_at']"```|
+|**hide**|*String\| Array* of column definitions |columns are present, but start hidden|```hidden="email_verified_at"```|
+|**dates**|*String\| Array* of column definitions [ and optional format in \ | delimited string]|column values are formatted as per the default date format, or format can be included in string with \| separator | ```:dates="['dob\|lS F y', 'created_at']"```|
+|**times**|*String\| Array* of column definitions [optional format in \ | delimited string]|column values are formatted as per the default time format, or format can be included in string with \| separator | ```'bedtime\|g:i A'```|
+|**searchable**|*String\| Array* of column names | Defines columns to be included in global search | ```searchable="name, email"```|
+|**sort**|*String* of column definition [and optional 'asc' or 'desc' (default: 'desc') in \| delimited string] |Specifies the column and direction for initial table sort. Default is column 0 descending | ```sort="name\|asc"```|
+|**hide-header**|*Boolean* default: *false*| The top row of the table including the column titles is removed if this is ```true``` | |
+|**hide-pagination**|*Boolean* default: *false*| Pagination controls are removed if this is ```true``` | |
+|**per-page**|*Integer* default: 10| Number of rows per page | ```per-page="20"``` |
+|**exportable**|*Boolean* default: *false*| Allows table to be exported | `````` |
+|**hideable**| _String_ | gives ability to show/hide columns, accepts strings 'inline', 'buttons', or 'select' | `````` |
+|**buttonsSlot**| _String_ | blade view to be included immediately after the buttons at the top of the table in the component, which can therefore access public properties | |
+|**beforeTableSlot**| _String_ | blade view to be included immediately before the table in the component, which can therefore access public properties | |
+|**afterTableSlot**| _String_ | blade view to be included immediately after the table in the component, which can therefore access public properties | [demo](https://livewire-datatables.com/complex) |
---
## Component Syntax
### Create a livewire component that extends ```Mediconesystems\LivewireDatatables\LivewireDatatable```
-> ```php artisan livewire:datatable foo``` --> 'app/Http/Livewire/Foo.php'
+> ```php artisan make:livewire-datatable foo``` --> 'app/Http/Livewire/Foo.php'
-> ```php artisan livewire:datatable tables.bar``` --> 'app/Http/Livewire/Tables/Bar.php'
+> ```php artisan make:livewire-datatable tables.bar``` --> 'app/Http/Livewire/Tables/Bar.php'
### Provide a datasource by declaring public property ```$model``` **OR** public method ```builder()``` that returns an instance of ```Illuminate\Database\Eloquent\Builder```
-> ```php artisan livewire:datatable users-table --model=user``` --> 'app/Http/Livewire/UsersTable.php' with ```public $model = User::class```
-
+> ```php artisan make:livewire-datatable users-table --model=user``` --> 'app/Http/Livewire/UsersTable.php' with ```public $model = User::class```
### Declare a public method ```columns``` that returns an array containing one or more ```Mediconesystems\LivewireDatatables\Column```
-
## Columns
Columns can be built using any of the static methods below, and then their attributes assigned using fluent method chains.
There are additional specific types of Column; ```NumberColumn```, ```DateColumn```, ```TimeColumn```, using the correct one for your datatype will enable type-specific formatting and filtering:
-
| Class | Description |
|---|---|
|Column|Generic string-based column. Filter will be a text input|
@@ -113,12 +131,12 @@ There are additional specific types of Column; ```NumberColumn```, ```DateColumn
|BooleanColumn| Values will be automatically formatted to a yes/no icon, filters will be yes/no|
|DateColumn| Values will be automatically formatted to the default date format. Filters will be a date range|
|TimeColumn| Values will be automatically formatted to the default time format. Filters will be a time range|
+|LabelColumn| Fixed header string ("label") with fixed content string in every row. No SQL is executed at all|
___
```php
class ComplexDemoTable extends LivewireDatatable
{
-
public function builder()
{
return User::query();
@@ -138,18 +156,33 @@ class ComplexDemoTable extends LivewireDatatable
Column::name('name')
->defaultSort('asc')
+ ->group('group1')
->searchable()
+ ->hideable()
->filterable(),
Column::name('planet.name')
->label('Planet')
+ ->group('group1')
->searchable()
+ ->hideable()
->filterable($this->planets),
+ // Column that counts every line from 1 upwards, independent of content
+ Column::index($this);
+
DateColumn::name('dob')
->label('DOB')
+ ->group('group2')
->filterable()
- ->hide()
+ ->hide(),
+
+ (new LabelColumn())
+ ->label('My custom heading')
+ ->content('This fixed string appears in every row'),
+
+ NumberColumn::name('dollars_spent')
+ ->enableSummary(),
];
}
}
@@ -165,20 +198,32 @@ class ComplexDemoTable extends LivewireDatatable
|_static_ **delete**|[*String* $primaryKey default: 'id']|Adds a column with a delete button, which will call ```$this->model::destroy($primaryKey)```|```Column::delete()```|
|_static_ **checkbox**|[*String* $column default: 'id']|Adds a column with a checkbox. The component public property ```$selected``` will contain an array of the named column from checked rows, |```Column::checkbox()```|
|**label**|*String* $name|Changes the display name of a column|```Column::name('id')->label('ID)```|
+|**group**|*String* $group|Assign the column to a group. Allows to toggle the visibility of all columns of a group at once|```Column::name('id')->group('my-group')```|
|**format**|[*String* $format]|Formats the column value according to type. Dates/times will use the default format or the argument |```Column::name('email_verified_at')->filterable(),```|
|**hide**| |Marks column to start as hidden|```Column::name('id')->hidden()```|
|**sortBy**|*String\|Expression* $column|Changes the query by which the column is sorted|```Column::name('dob')->sortBy('DATE_FORMAT(users.dob, "%m%d%Y")'),```|
|**truncate**|[*Integer* $length (default: 16)]Truncates column to $length and provides full-text in a tooltip. Uses ```view('livewire-datatables::tooltip)```|```Column::name('biography)->truncate(30)```|
|**linkTo**|*String* $model, [*Integer* $pad]|Replaces the value with a link to ```"/$model/$value"```. Useful for ID columns. Optional zero-padding. Uses ```view('livewire-datatables::link)```|```Column::name('id')->linkTo('user')```|
+|**link**|*String* $href, [*String* $slot]|Let the content of the column render as a link. You may use {{ }} syntax to fill the url with any attributes of the current row. Uses ```view('livewire-datatables::link)```|```Column::name('first_name')->link('/users/{{slug}}/edit', 'edit {{first_name}} {{last_name}}')```|
|**round**|[*Integer* $precision (default: 0)]|Rounds value to given precision|```Column::name('age')->round()```|
|**defaultSort**|[*String* $direction (default: 'desc')]|Marks the column as the default search column|```Column::name('name')->defaultSort('asc')```|
|**searchable**| |Includes the column in the global search|```Column::name('name')->searchable()```|
-|**filterable**|[*Array* $options], [*String* $filterScope]|Adds a filter to the column, according to Column type. If an array of options is passed it wil be used to populate a select input. If the column is a scope column then the name of the filter scope muyst also be passed|```Column::name('allegiance')->filterable(['Rebellion', 'Empire'])```|
+|**hideable**| |The user is able to toggle the visibility of this column|```Column::name('name')->hideable()```|
+|**filterable**|[*Array* $options], [*String* $filterScope]|Adds a filter to the column, according to Column type. If an array of options is passed it wil be used to populate a select input. If the column is a scope column then the name of the filter scope must also be passed|```Column::name('allegiance')->filterable(['Rebellion', 'Empire'])```|
+|**unwrap**| | Prevents the content of the column from being wrapped in multiple lines |```Column::name('oneliner')->unwrap()```|
+|**filterOn**|*String/Array* $statement|Allows you to specify a column name or sql statement upon which to perform the filter (must use SQL syntax, not Eloquent eg. ```'users.name'``` instead of ```'user.name'```). Useful if using a callback to modify the displayed values. Can pass a single string or array of strings which will be combined with ```OR```|```Column::callback(['name', 'allegiance'], function ($name, $allegiance) { return "$name is allied to $allegiance"; })->filterable(['Rebellion', 'Empire'])->filterOn('users.allegiance')```|
|**view**|*String* $viewName| Passes the column value, whole row of values, and any additional parameters to a view template | _(see below)_|
|**editable**| | Marks the column as editable | _(see below)_|
-|**alignCenter**| | Center-aligns column header and contents |```Column::delete()->alignCenter()```|
-|**alignCenter**| | Right-aligns column header and contents |```Column::delete()->alignRight()```|
+|**alignCenter**| | Center-aligns column header _and_ contents |```Column::delete()->alignCenter()```|
+|**alignRight**| | Right-aligns column header _and_ contents |```Column::delete()->alignRight()```|
+|**contentAlignCenter**| | Center-aligns column contents |```Column::delete()->contentAlignCenter()```|
+|**contentAlignRight**| | Right-aligns column contents |```Column::delete()->contentAlignRight()```|
+|**headerAlignCenter**| | Center-aligns column header |```Column::delete()->headerAlignCenter()```|
+|**headerAlignRight**| | Right-aligns column header |```Column::delete()->headerAlignRight()```|
|**editable**| | Marks the column as editable | _(see below)_|
+|**exportCallback**| Closure $callback | Reformats the result when exporting | _(see below)_ |
+|**excludeFromExport**| | Excludes the column from export |```Column::name('email')->excludeFromExport()```|
+|**unsortable**| | Prevents the column being sortable |```Column::name('email')->unsortable()```|
___
### Listener
@@ -197,6 +242,122 @@ NumberColumn::name('students.age:min')->label('Student Min'),
NumberColumn::name('students.age:max')->label('Student Max'),
```
+### Column Groups
+
+When you have a very big table with a lot of columns, it is possible to create 'column groups' that allows the user to toggle the visibility of a whole group at once. Use `->group('NAME')` at any column to achieve this.
+You can human readable labels and translations of your groups via the `groupLabels` property of your table:
+
+```php
+class GroupDemoTable extends LivewireDatatable
+{
+ public $groupLabels = [
+ 'group1' => 'app.translation_for_group_1',
+ 'group2' => 'app.translation_for_group_2'
+ ];
+
+ public function columns()
+ {
+ return [
+ Column::name('planets.name')
+ ->group('group1')
+ ->label('Planet'),
+
+ Column::name('planets.name')
+ ->group('group2')
+ ->label('Planet'),
+```
+
+### Summary row
+If you need to summarize all cells of a specific column, you can use `enableSummary()`:
+
+```php
+public function columns()
+{
+ return [
+ Column::name('dollars_spent')
+ ->label('Expenses in Dollar')
+ ->enableSummary(),
+
+ Column::name('euro_spent')
+ ->label('Expenses in Euro')
+ ->enableSummary(),
+```
+
+### Mass (Bulk) Action
+
+If you want to be able to act upon several records at once, you can use the `buildActions()` method in your Table:
+
+```php
+public function buildActions()
+ {
+ return [
+
+ Action::value('edit')->label('Edit Selected')->group('Default Options')->callback(function ($mode, $items) {
+ // $items contains an array with the primary keys of the selected items
+ }),
+
+ Action::value('update')->label('Update Selected')->group('Default Options')->callback(function ($mode, $items) {
+ // $items contains an array with the primary keys of the selected items
+ }),
+
+ Action::groupBy('Export Options', function () {
+ return [
+ Action::value('csv')->label('Export CSV')->export('SalesOrders.csv'),
+ Action::value('html')->label('Export HTML')->export('SalesOrders.html'),
+ Action::value('xlsx')->label('Export XLSX')->export('SalesOrders.xlsx')->styles($this->exportStyles)->widths($this->exportWidths)
+ ];
+ }),
+ ];
+ }
+```
+
+### Mass Action Style
+
+If you only have small style adjustments to the Bulk Action Dropdown you can adjust some settings here:
+
+```php
+public function getExportStylesProperty()
+ {
+ return [
+ '1' => ['font' => ['bold' => true]],
+ 'B2' => ['font' => ['italic' => true]],
+ 'C' => ['font' => ['size' => 16]],
+ ];
+ }
+
+ public function getExportWidthsProperty()
+ {
+ return [
+ 'A' => 55,
+ 'B' => 45,
+ ];
+ }
+```
+
+### Pin Records
+
+If you want to give your users the ability to pin specific records to be able to, for example, compare
+them with each other, you can use the CanPinRecords trait. Ensure to have at least one Checkbox Column
+so the user can select records:
+
+```php
+use Mediconesystems\LivewireDatatables\Traits\CanPinRecords;
+
+class RecordTable extends LivewireDatatable
+{
+ use CanPinRecords;
+
+ public $model = Record::class;
+
+ public function columns()
+ {
+ return [
+ Column::checkbox(),
+
+ // ...
+
+```
+
### Custom column names
It is still possible to take full control over your table, you can define a ```builder``` method using whatever query you like, using your own joins, groups whatever, and then name your columns using your normal SQL syntax:
@@ -226,17 +387,16 @@ public function columns()
```
-
### Callbacks
Callbacks give you the freedom to perform any mutations you like on the data before displaying in the table.
- The callbacks are performed on the paginated results of the database query, so shouldn't use a ton of memory
- Callbacks will receive the chosen columns as their arguments.
- Callbacks can be defined inline as below, or as public methods on the Datatable class, referenced by passing the name as a string as the second argument to the callback method.
-
+- If you want to format the result differently for export, use ```->exportCallback(Closure $callback)```.
```php
class CallbackDemoTable extends LivewireDatatable
{
- public model = User::class
+ public $model = User::class
public function columns()
{
@@ -250,6 +410,8 @@ class CallbackDemoTable extends LivewireDatatable
return $age > 18
? '' . $age . ''
: $age;
+ })->exportCallback(function ($dob, $signupDate), {
+ return $age = $signupDate->diffInYears($dob);
}),
...
@@ -257,6 +419,34 @@ class CallbackDemoTable extends LivewireDatatable
}
```
+### Default Filters
+
+If you want to have a default filter applied to your table, you can use the `defaultFilters` property. The `defaultFilter` should be an Array of column names and the default filter value to use for. When a persisted filter (`$this->persistFilters` is true and session values are available) is available, it will override the default filters.
+
+In the example below, the table will by default be filtered by rows where the _deleted_at_ column is false. If the user has a persisted filter for the _deleted_at_ column, the default filter will be ignored.
+
+```php
+class CallbackDemoTable extends LivewireDatatable
+{
+ public $defaultFilters = [
+ 'deleted_at' => '0',
+ ];
+
+ public function builder()
+ {
+ return User::query()->withTrashed();
+ }
+
+ public function columns()
+ {
+ return [
+ Column::name('id'),
+ BooleanColumn::name('deleted_at')->filterable(),
+ ];
+ }
+}
+```
+
### Views
You can specify that a column's output is piped directly into a separate blade view template.
- Template is specified using ususal laravel view helper syntax
@@ -264,7 +454,7 @@ You can specify that a column's output is piped directly into a separate blade v
```php
class CallbackDemoTable extends LivewireDatatable
{
- public model = User::class
+ public $model = User::class
public function columns()
{
@@ -291,7 +481,6 @@ This uses the ```view()``` method above to pass the data into an Alpine/Livewire
```php
class EditableTable extends LivewireDatatable
{
-
public $model = User::class;
public function columns()
@@ -310,9 +499,125 @@ class EditableTable extends LivewireDatatable
}
```
-## Styling
+# Complex Query Builder
+Just add ```$complex = true``` to your Datatable Class and all filterable columns will be available in the complex query builder.
+
+**Features**
+- Combine rules and groups of rules using AND/OR logic
+- Drag and drop rules around the interface
+
+
+---
+**Persisting Queries** (Requires AlpineJS v3 with $persist plugin)
+- Add ```$persistComplexQuery = true``` to your class and queries will be stored in browser localstorage.
+- By default the localstorage key will be the class name. You can provide your own by setting the public property ```$persistKey``` or overriding ```getPersistKeyProperty()``` on the Datatable Class
+- eg: for user-specific persistence:
+
+```php
+public function getPersistKeyProperty()
+{
+ return Auth::id() . '-' . parent::getPersistKeyProperty();
+}
+```
+---
+**Saving Queries**
+
+If you want to permanently save queries you must provide 3 methods for adding, deleting and retrieving your saved queries using whatever logic you like:
+
+- ```public function saveQuery(String $name, Array $rules)```
+- ```public function deleteQuery(Int $id)```
+- ```public function getSavedQueries()```
+
+* In your save and delete methods, be sure to emit an ```updateSavedQueries``` livewire event and pass a fresh array of results (see example below)
+
+### Example:
+This example shows saving queries using a conventional Laravel ComplexQuery model, that belongsTo a User
+
+```php
+/* Migration */
+
+class CreateComplexQueriesTable extends Migration
+{
+ public function up()
+ {
+ Schema::create('complex_queries', function (Blueprint $table) {
+ $table->id();
+ $table->unsignedInteger('user_id');
+ $table->string('table');
+ $table->json('rules');
+ $table->string('name');
+ $table->timestamps();
+ });
+ }
+}
+
+
+/* Model */
+
+class ComplexQuery extends BaseModel
+{
+ protected $casts = ['rules' => 'array'];
+
+ public function user()
+ {
+ return $this->belongsTo(User::class);
+ }
+}
+
+/* Datatable Class */
+
+class TableWithSaving extends LivewireDatatable
+{
+ ...
+
+ public function saveQuery($name, $rules)
+ {
+ Auth::user()->complex_queries()->create([
+ 'table' => $this->name,
+ 'name' => $name,
+ 'rules' => $rules
+ ]);
+
+ $this->emit('updateSavedQueries', $this->getSavedQueries());
+ }
+
+ public function deleteQuery($id)
+ {
+ ComplexQuery::destroy($id);
+
+ $this->emit('updateSavedQueries', $this->getSavedQueries());
+ }
+
+ public function getSavedQueries()
+ {
+ return Auth::user()->complex_queries()->where('table', $this->name)->get();
+ }
+
+ ...
+}
+```
+
+
+# Styling
I know it's not cool to provide a package with tons of opionated markup and styling. Most other packages seem to have gone down the route of passing optional classes around as arguments or config variables. My take is that because this is just blade with tailwind, you can publish the templates and do whatever you like to them - it should be obvious where the Livewire and Alpine moving parts are.
+There are methods for applying styles to rows and cells. ```rowClasses``` receives the ```$row``` and the [laravel loop variable](https://laravel.com/docs/8.x/blade#the-loop-variable) as parameters. ```cellClasses``` receives the ```$row``` and ```$column```
+
+For example:
+```php
+public function rowClasses($row, $loop)
+{
+ return 'divide-x divide-gray-100 text-sm text-gray-900 ' . ($this->rowIsSelected($row) ? 'bg-yellow-100' : ($row->{'car.model'} === 'Ferrari' ? 'bg-red-500' : ($loop->even ? 'bg-gray-100' : 'bg-gray-50')));
+}
+
+public function cellClasses($row, $column)
+{
+ return 'text-sm ' . ($this->rowIsSelected($row) ? ' text-gray-900' : ($row->{'car.model'} === 'Ferrari' ? ' text-white' : ' text-gray-900'));
+}
+```
+
+You can change the default CSS classes applied by the ```rowClasses``` and the ```cellClasses``` methods by changing ```default_classes``` in the ```livewire-datatables.php``` config file.
+
You could also override the render method in your table's class to provide different templates for different tables.
diff --git a/composer.json b/composer.json
index 8c66971e..c8e58e81 100644
--- a/composer.json
+++ b/composer.json
@@ -1,9 +1,10 @@
{
"name": "mediconesystems/livewire-datatables",
- "description": "",
+ "description": "Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS",
"keywords": [
- "mediconesystems",
- "livewire-datatables"
+ "php",
+ "laravel",
+ "livewire"
],
"homepage": "https://github.com/mediconesystems/livewire-datatables",
"license": "MIT",
@@ -13,18 +14,29 @@
"name": "Mark Salmon",
"email": "mark.salmon@mediconesystems.com",
"role": "Developer"
+ },
+ {
+ "name": "Shane Burrell",
+ "email": "shane@shaneburrell.com",
+ "role": "Developer"
+ },
+ {
+ "name": "Herbert Maschke",
+ "email": "thyseus@pm.me",
+ "role": "Developer"
}
],
"require": {
- "php": "^7.2.5",
- "illuminate/support": "^7.0|^8.0",
- "livewire/livewire": "^1.2|^2.0",
- "maatwebsite/excel": "^3.1"
+ "php": "^8.0",
+ "illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0|^12.0",
+ "livewire/livewire": "^2.4.4|^3.4",
+ "maatwebsite/excel": "^3.1",
+ "reedware/laravel-relation-joins": "^2.4|^3.0|^4.0|^5.0"
},
"require-dev": {
"laravel/legacy-factories": "^1.0.4",
- "orchestra/testbench": "^4.0|5.0|6.0",
- "phpunit/phpunit": "^8.0|9.0"
+ "orchestra/testbench": "^4.0|5.0|6.0|^7.0|^8.0|^9.0",
+ "phpunit/phpunit": "^8.0|9.0|^9.5.10|^10.5"
},
"autoload": {
"psr-4": {
@@ -39,7 +51,6 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
-
},
"config": {
"sort-packages": true
diff --git a/config/livewire-datatables.php b/config/livewire-datatables.php
index a60e8a9c..d67a0bd2 100644
--- a/config/livewire-datatables.php
+++ b/config/livewire-datatables.php
@@ -1,6 +1,105 @@
'H:i',
'default_date_format' => 'd/m/Y',
+ 'default_datetime_format' => 'd/m/Y H:i',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Default Carbon Formats
+ |--------------------------------------------------------------------------
+ | The default formats that are used for TimeColumn & DateColumn.
+ | You can use the formatting characters from the PHP DateTime class.
+ | More info: https://www.php.net/manual/en/datetime.format.php
+ |
+ */
+
+ 'default_time_start' => '0000-00-00',
+ 'default_time_end' => '9999-12-31',
+
+ // Defaults that work with smalldatetime in SQL Server
+ // 'default_time_start' => '1900-01-01',
+ // 'default_time_end' => '2079-06-06',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Surpress Search Highlights
+ |--------------------------------------------------------------------------
+ | When enabled, matching text won't be highlighted in the search results
+ | while searching.
+ |
+ */
+
+ 'suppress_search_highlights' => false,
+
+ /*
+ |--------------------------------------------------------------------------
+ | Per Page Options
+ |--------------------------------------------------------------------------
+ | Sets the options to choose from in the `Per Page`dropdown.
+ |
+ */
+
+ 'per_page_options' => [10, 25, 50, 100],
+
+ /*
+ |--------------------------------------------------------------------------
+ | Default Per Page
+ |--------------------------------------------------------------------------
+ | Sets the default amount of rows to display per page.
+ |
+ */
+
+ 'default_per_page' => 10,
+
+ /*
+ |--------------------------------------------------------------------------
+ | Model Namespace
+ |--------------------------------------------------------------------------
+ | Sets the default namespace to be used when generating a new Datatables
+ | component.
+ |
+ */
+
+ 'model_namespace' => 'App',
+
+ /*
+ |--------------------------------------------------------------------------
+ | Default Sortable
+ |--------------------------------------------------------------------------
+ | Should a column of a datatable be sortable by default ?
+ |
+ */
+
+ 'default_sortable' => true,
+
+ /*
+ |--------------------------------------------------------------------------
+ | Default CSS classes
+ |--------------------------------------------------------------------------
+ |
+ | Sets the default classes that will be applied to each row and class
+ | if the rowClasses() and cellClasses() functions are not overrided.
+ |
+ */
+
+ 'default_classes' => [
+ 'row' => [
+ 'even' => 'divide-x divide-gray-100 text-sm text-gray-900 bg-gray-100',
+ 'odd' => 'divide-x divide-gray-100 text-sm text-gray-900 bg-gray-50',
+ 'selected' => 'divide-x divide-gray-100 text-sm text-gray-900 bg-yellow-100',
+ ],
+ 'cell' => 'whitespace-no-wrap text-sm text-gray-900 px-6 py-2',
+ ],
];
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 22fe879d..edc89d6d 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,14 +1,5 @@
-
+tests
diff --git a/resources/.DS_Store b/resources/.DS_Store
deleted file mode 100644
index 64304b44..00000000
Binary files a/resources/.DS_Store and /dev/null differ
diff --git a/resources/views/icons/copy.blade.php b/resources/views/icons/copy.blade.php
new file mode 100644
index 00000000..9db8dff8
--- /dev/null
+++ b/resources/views/icons/copy.blade.php
@@ -0,0 +1,3 @@
+
diff --git a/resources/views/livewire/datatables/checkbox.blade.php b/resources/views/livewire/datatables/checkbox.blade.php
index ba58dcab..078c4773 100644
--- a/resources/views/livewire/datatables/checkbox.blade.php
+++ b/resources/views/livewire/datatables/checkbox.blade.php
@@ -1,3 +1,9 @@