Skip to content

Commit

Permalink
Add CountColumn, simpler adding of WithCounts, With (#1761)
Browse files Browse the repository at this point in the history
* Initial Commit

* Adjust CountColumn

* Add ExtraWiths

* Add AggregateColumn

* Add SumColumn

* Update Docs - Add Column Types Section

* Add exceptions for empty data source, add standard tests

* Ensure pcov runs on push to master/development/develop

* Update to use codecov v4

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Jul 10, 2024
1 parent 51f4c94 commit 37691f9
Show file tree
Hide file tree
Showing 52 changed files with 1,260 additions and 3,153 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/run-tests-pcov-pull.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: run-tests-pcov-pull

on:
push:
branches:
- 'develop'
- 'development'
- 'master'
pull_request:
branches:
- 'develop'
Expand All @@ -18,7 +23,7 @@ jobs:
laravel: [10]
stability: [prefer-dist]

name: PCOV-PULL - ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}
name: PCOV - ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}
env:
extensionKey: phpextensions-${{ matrix.os }}-P${{ matrix.php }}-withpcov
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pcov,pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
Expand Down Expand Up @@ -86,7 +91,7 @@ jobs:
run: php ./vendor/bin/paratest --cache-directory=".phpunit.cache/code-coverage" --strict-coverage --coverage-clover ./coverage.xml --processes=4

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ phpunit.xml.dist.dev
.history/*
.env
phpunit.xml.bak
phpstan.txt
phpstan.txt
coverage.xml
./tmp/**
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-livewire-tables` will be documented in this file

## UNRELEASED
### New Features
- Add new columns (ArrayColumn, AvgColumn, CountColumn, SumColumn) by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1761

## [v3.2.8] - 2024-07-03
### Bug Fixes
- Fix hide bulk actions when empty not reflecting in frontend by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1747
Expand Down
2,894 changes: 0 additions & 2,894 deletions coverage.xml

This file was deleted.

2 changes: 1 addition & 1 deletion docs/bulk-actions/_index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
title: Bulk Actions
weight: 9
weight: 10
---
4 changes: 4 additions & 0 deletions docs/column-types/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Column Types
weight: 5
---
22 changes: 22 additions & 0 deletions docs/column-types/array_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: Array Columns (beta)
weight: 1
---

Array columns provide an easy way to work with and display an array of data from a field.

```
ArrayColumn::make('notes', 'name')
->data(fn($value, $row) => ($row->notes))
->outputFormat(fn($index, $value) => "<a href='".$value->id."'>".$value->name."</a>")
->separator('<br />')
->sortable(),
```

### Empty Value
You may define the default/empty value using the "emptyValue" method

```
ArrayColumn::make('notes', 'name')
->emptyValue('Unknown'),
```
14 changes: 14 additions & 0 deletions docs/column-types/avg_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Avg Columns (beta)
weight: 2
---

Avg columns provide an easy way to display the "Average" of a field on a relation.

```
AvgColumn::make('Average Related User Age')
->setDataSource('users','age')
->sortable(),
```

The "sortable()" callback can accept a callback, or you can use the default behaviour, which calculates the correct field to sort on.
81 changes: 81 additions & 0 deletions docs/column-types/boolean_columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: Boolean Columns
weight: 3
---

Boolean columns are good if you have a column type that is a true/false, or 0/1 value.

For example:

```php
BooleanColumn::make('Active')
```

Would yield:

![Boolean Column](https://imgur.com/LAk6gHY.png)

### Using your own view

If you don't want to use the default view and icons you can set your own:

```php
BooleanColumn::make('Active')
->setView('my.active.view')
```

You will have access to `$component`, `$status`, and `$successValue`.

To help you better understand, this is the Tailwind implementation of BooleanColumn:

```html
@if ($status)
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-5 w-5 @if ($successValue === true) text-green-500 @else text-red-500 @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
@else
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-5 w-5 @if ($successValue === false) text-green-500 @else text-red-500 @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
@endif
```

### Setting the truthy value

If you want the false value to be the green option, you can set:

```php
BooleanColumn::make('Active')
->setSuccessValue(false); // Makes false the 'successful' option
```

That would swap the colors of the icons in the image above.

### Setting the status value

By default, the `$status` is set to:

```php
(bool)$value === true
```

You can override this functionality:

```php
BooleanColumn::make('Active')
// Note: Parameter `$row` available as of v2.4
->setCallback(function(string $value, $row) {
// Figure out what makes $value true
}),
```

### Different types of boolean display

By default, the BooleanColumn displays icons.

If you would like the BooleanColumn to display a plain Yes/No, you can set:

```php
BooleanColumn::make('Active')
->yesNo()
```
34 changes: 34 additions & 0 deletions docs/column-types/button_group_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Button Group Columns
weight: 4
---

Button group columns let you provide an array of LinkColumns to display in a single cell.

```php
ButtonGroupColumn::make('Actions')
->attributes(function($row) {
return [
'class' => 'space-x-2',
];
})
->buttons([
LinkColumn::make('View') // make() has no effect in this case but needs to be set anyway
->title(fn($row) => 'View ' . $row->name)
->location(fn($row) => route('user.show', $row))
->attributes(function($row) {
return [
'class' => 'underline text-blue-500 hover:no-underline',
];
}),
LinkColumn::make('Edit')
->title(fn($row) => 'Edit ' . $row->name)
->location(fn($row) => route('user.edit', $row))
->attributes(function($row) {
return [
'target' => '_blank',
'class' => 'underline text-blue-500 hover:no-underline',
];
}),
]),
```
41 changes: 41 additions & 0 deletions docs/column-types/color_columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Color Columns
weight: 5
---

Color columns provide an easy way to a Color in a Column

You may pass either pass a CSS-compliant colour as a field
```php
ColorColumn::make('Favourite Colour', 'favourite_color'),
```

Or you may use a Callback
```php
ColorColumn::make('Favourite Colour')
->color(
function ($row) {
if ($row->success_rate < 40)
{
return '#ff0000';
}
else if ($row->success_rate > 90)
{
return '#008000';
}
else return '#ffa500';

}
),
```

You may also specify attributes to use on the div displaying the color, to adjust the size or appearance, this receives the full row. By default, this will replace the standard classes, to retain them, set "default" to true. To then over-ride, you should prefix your classes with "!" to signify importance.
```php
ColorColumn::make('Favourite Colour')
->attributes(function ($row) {
return [
'class' => '!rounded-lg self-center',
'default' => true,
];
}),
```
28 changes: 28 additions & 0 deletions docs/column-types/component_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Component Columns
weight: 6
---

Component columns let you specify a component name and attributes and provides the column value to the slot.

```php
// Before
Column::make("Email", "email")
->format(function ($value) {
return view('components.alert')
->with('attributes', new ComponentAttributeBag([
'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger',
'dismissible' => true,
]))
->with('slot', $value);
}),

// After
ComponentColumn::make('E-mail', 'email')
->component('email')
->attributes(fn ($value, $row, Column $column) => [
'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger',
'dismissible' => true,
]),
```

14 changes: 14 additions & 0 deletions docs/column-types/count_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Count Columns (beta)
weight: 7
---

Count columns provide an easy way to display the "Count" of a relation.

```
CountColumn::make('Related Users')
->setDataSource('users')
->sortable(),
```

The "sortable()" callback can accept a callback, or you can use the default behaviour, which calculates the correct field to sort on.
28 changes: 28 additions & 0 deletions docs/column-types/date_columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Date Columns
weight: 8
---

Date columns provide an easy way to display dates in a given format, without having to use repetitive format() methods or partial views.

You may pass either a DateTime object, in which you can define an "outputFormat"
```php
DateColumn::make('Updated At', 'updated_at')
->outputFormat('Y-m-d H:i:s),
```

Or you may pass a string, in which case you can define an "inputFormat" in addition to the outputFormat:
```php
DateColumn::make('Last Charged', 'last_charged_at')
->inputFormat('Y-m-d H:i:s')
->outputFormat('Y-m-d'),
```

You may also set an "emptyValue" to use when there is no value from the database:
```php
DateColumn::make('Last Charged', 'last_charged_at')
->inputFormat('Y-m-d H:i:s')
->outputFormat('Y-m-d')
->emptyValue('Not Found'),
```

26 changes: 26 additions & 0 deletions docs/column-types/image_columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Image Columns
weight: 9
---

Image columns provide a way to display images in your table without having to use `format()` or partial views:

```php
ImageColumn::make('Avatar')
->location(
fn($row) => storage_path('app/public/avatars/' . $row->id . '.jpg')
),
```

You may also pass an array of attributes to apply to the image tag:

```php
ImageColumn::make('Avatar')
->location(
fn($row) => storage_path('app/public/avatars/' . $row->id . '.jpg')
)
->attributes(fn($row) => [
'class' => 'rounded-full',
'alt' => $row->name . ' Avatar',
]),
```
24 changes: 24 additions & 0 deletions docs/column-types/link_columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Link Columns
weight: 10
---

Link columns provide a way to display HTML links in your table without having to use `format()` or partial views:

```php
LinkColumn::make('Action')
->title(fn($row) => 'Edit')
->location(fn($row) => route('admin.users.edit', $row)),
```

You may also pass an array of attributes to apply to the `a` tag:

```php
LinkColumn::make('Action')
->title(fn($row) => 'Edit')
->location(fn($row) => route('admin.users.edit', $row))
->attributes(fn($row) => [
'class' => 'rounded-full',
'alt' => $row->name . ' Avatar',
]),
```
5 changes: 5 additions & 0 deletions docs/column-types/livewire_component_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Livewire Component (beta)
weight: 11
---

Loading

0 comments on commit 37691f9

Please sign in to comment.