Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…bles into main
  • Loading branch information
coolsam726 committed Oct 18, 2021
2 parents 95611fc + 698fb4e commit d878d6c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: run-tests

on:
push:
branches: [master]
branches: [main]
pull_request:
branches: [master]
branches: [main]

jobs:
test:
Expand Down
58 changes: 51 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# Very short description of the package
# Laravel + PrimeVue Datatables

[![Latest Version on Packagist](https://img.shields.io/packagist/v/savannabits/primevue-datatables.svg?style=flat-square)](https://packagist.org/packages/savannabits/primevue-datatables)
[![Total Downloads](https://img.shields.io/packagist/dt/savannabits/primevue-datatables.svg?style=flat-square)](https://packagist.org/packages/savannabits/primevue-datatables)
![GitHub Actions](https://github.com/savannabits/primevue-datatables/actions/workflows/main.yml/badge.svg)

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
This is a simple, clean and fluent serve-side implementation of [PrimeVue Datatables](https://primefaces.org/primevue/showcase/#/datatable) in [Laravel](https://laravel.com/).

## Features
- Global Search including searching in relationships up to a depth of 3, e.g `author.user.name`
- Per-Column filtering out of the box
- Column Sorting with direction toggling
- Pagination with a dynamic `no. or records per page` setting
- Fully compatible with PrimeVue Datatable

## Installation

Expand All @@ -16,8 +23,49 @@ composer require savannabits/primevue-datatables

## Usage

### Server Side
It is as simple as having this in your `index()` function of your controller:
```php
// Usage description here
public function index(Request $request): JsonResponse
{
$list = PrimevueDatatables::of(Book::query())->make();
return response()->json([
'success' => true,
'payload' => $list,
]);
}
```
#### Required Query Parameters
The server-side implementation uses two parameters from your laravel request object to perform filtering, sorting and pagination:
You have to pass the following parameters as query params from the client:
1. Searchable Columns **(Passed as `searchable_columns`)** - Used to specify the columns that will be used to perform the global datatable search
2. Dt Params **(Passed as `dt_params`)** - This is the main Datatable event object as received from PrimeVue. See [Lazy Datatable](https://primefaces.org/primevue/showcase/#/datatable/lazy) documentation for more details
### Client Side:
Go through [PrimeVue's Lazy Datatable](https://primefaces.org/primevue/showcase/#/datatable/lazy) documentation for details on frontend implementation.

Here is an example of your `loadLazyData()` implementation:

```ts
const loadLazyData = async () => {
loading.value = true;

try {
const res = await axios.get('/api/books',{
params: {
dt_params: JSON.stringify(lazyParams.value),
searchable_columns: JSON.stringify(['title','author.name','price']),
},
});

records.value = res.data.payload.data;
totalRecords.value = res.data.payload.total;
loading.value = false;
} catch (e) {
records.value = [];
totalRecords.value = 0;
loading.value = false;
}
};
```

### Testing
Expand Down Expand Up @@ -46,7 +94,3 @@ If you discover any security related issues, please email maosa.sam@gmail.com in
## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

## Laravel Package Boilerplate

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).
10 changes: 7 additions & 3 deletions src/PrimevueDatatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ class PrimevueDatatables
private array $filters;
private array $_dtParams;

public function __construct(Builder $query)
public function __construct()
{
$this->query = $query;
$this->_dtParams = json_decode(request()->get('dt_params', "[]"), true);
$this->_searchableColumns
= json_decode(request()->get('searchable_columns',"[]"), true);
Expand All @@ -44,9 +43,14 @@ public function searchableColumns(array $searchable_columns): static
return $this;
}

public function query(Builder $query): static {
$this->query = $query;
return $this;
}
public static function of(Builder $query): static
{
return new self($query);
$instance = new self($query);
return $instance->query($query);
}

public function make(): \Illuminate\Contracts\Pagination\LengthAwarePaginator
Expand Down

0 comments on commit d878d6c

Please sign in to comment.