Skip to content

Commit

Permalink
#55 (#57)
Browse files Browse the repository at this point in the history
* Fixed doc js snippet given in [destroyConfirmationHtmlAttributes](https://github.com/Okipa/laravel-table#table-destroyConfirmationHtmlAttributes).
  • Loading branch information
Okipa authored Sep 7, 2020
1 parent d68e062 commit eb4cca2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
24 changes: 15 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [3.1.2](https://github.com/Okipa/laravel-table/compare/3.1.1...3.1.2)

2020-08-24

* Fixed doc js snippet given in [destroyConfirmationHtmlAttributes](./README.md#table-destroyConfirmationHtmlAttributes).

## [3.1.1](https://github.com/Okipa/laravel-table/compare/3.1.0...3.1.1)

2020-08-24
Expand Down Expand Up @@ -119,15 +125,15 @@

* The model is now directly passed to the route during the table `show`, `edit` and `destroy` routes generation instead of its id.
```php
// assuming your declared your edit route like this:
// Assuming your declared your edit route like this:
(new Table)->model(User::class)->routes([
// ...
'edit' => ['name'=> 'user.edit', 'params' => ['foo' => 'bar']],
//...
]);
// the route will be generated like this during the table instantiation:
// The route will be generated like this during the table instantiation:
route('user.edit', [$user, 'foo' => 'bar']);
// instead of this way
// Instead of this way:
route('user.edit', [$user->id, 'foo' => 'bar']);
```

Expand All @@ -137,15 +143,15 @@ route('user.edit', [$user->id, 'foo' => 'bar']);

* Fixed params order when generating the table routes. The table model id was not positioned at first when declaring other parameters.
```php
// with a route declared like this:
// With a route declared like this:
Route::get('user/edit/{user}/{foo}', 'UsersController@edit')->name('user.edit');
// and a table routes declaration like this:
// And a table routes declaration like this:
(new Table)->model(User::class)->routes([
// ...
'edit' => ['name'=> 'user.edit', 'params' => ['bar']],
//...
]);
// the route is now correctly generated and gives: /user/edit/1/bar instead of /user/edit/bar/1
// The route is now correctly generated and gives: /user/edit/1/bar instead of /user/edit/bar/1
```

## [1.2.1](https://github.com/Okipa/laravel-table/compare/1.2.0...1.2.1)
Expand All @@ -154,15 +160,15 @@ Route::get('user/edit/{user}/{foo}', 'UsersController@edit')->name('user.edit');

* Fixed the `show`, `edit` and `destroy` route generation, since Laravel 6 does handle differently the key given in the `route()` helper:
```php
// assuming your declared your edit route like this:
// Assuming your declared your edit route like this:
(new Table)->model(User::class)->routes([
// ...
'edit' => ['name'=> 'user.edit', 'params' => ['foo' => 'bar']],
//...
]);
// the route will be generated like this during the table instantiation:
// The route will be generated like this during the table instantiation:
route('user.edit', [$user->id, 'foo' => 'bar']);
// instead of this way
// Instead of this way
route('user.edit', ['id' => $user->id, 'foo' => 'bar']);
```

Expand Down
71 changes: 36 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,21 @@ class NewsTable extends AbstractTable
'destroy' => ['name' => 'news.destroy'],
'show' => ['name' => 'news.show'],
])
->rowsNumber(50) // or set `null` to display all the items contained in database
->rowsNumber(50) // Or set `null` to display all the items contained in database
->activateRowsNumberDefinition(false)
->query(function (Builder $query) {
// some examples of what you can do
// Some examples of what you can do
$query->select('news.*');
// add a constraint
// Add a constraint
$query->where('category_id', $this->categoryId);
// get value stored in a json field
// Get value stored in a json field
$query->addSelect('news.json_field->>json_attribute as json_attribute');
// get a formatted value from a pivot table
// Get a formatted value from a pivot table
$query->selectRaw('count(comments.id) as comments_count');
$query->leftJoin('news_commment', 'news_commment.news_id', '=', 'news.id');
$query->leftJoin('comments', 'comments.id', '=', 'news_commment.comment_id');
$query->groupBy('comments.id');
// alias a value to make it available from the column model
// Alias a value to make it available from the column model
$query->addSelect('users.name as author');
$query->join('users', 'users.id', '=', 'news.author_id');
})
Expand All @@ -266,7 +266,7 @@ class NewsTable extends AbstractTable
fn(News $news) => $news->id === 3,
['highlighted', 'bg-success']
)
// append all request params to the paginator
// Append all request params to the paginator
->appendData($this->request->all());
}

Expand Down Expand Up @@ -416,46 +416,46 @@ class UsersTable extends AbstractTable
* Routes have to be defined with the following structure:

```php
// example
// Example
[
'index' => [
// required
// Required
'name' => 'users.index',
// optional
// Optional
'params' => [
// set route params (or not)
// Set route params (or not)
]
]
// you will have to respect the same structure for any declared route.
// You will have to respect the same structure for any declared route.
];
```

* As the current model instance is always provided as a param to the `show`, `edit` and `destroy` routes, you do not have to pass it to the params.
* You also should declare your routes carefully to avoid errors. See the examples bellow:

```php
// assuming your declared your route with implicit binding:
// Assuming your declared your route with implicit binding:
Route::get('parent/{$parent}/user/edit/{$user}/child/{$child}', 'UsersController@edit')->name('user.edit');
// you will have to declare your params with keys as following:
// You will have to declare your params with keys as following:
(new Table)->model(User::class)->routes([
// ...
'edit' => ['name'=> 'user.edit', 'params' => ['parent' => $parent, 'child' => $child]],
// ...
]);
// because the route will be generated with the table related model as first param (the params order differs from the declaration):
// Because the route will be generated with the table related model as first param (the params order differs from the declaration):
route('user.edit', [$user, 'parent' => $parent, 'child' => $child]);
```

```php
// now imagine your route is declared with the table related model as first param like this:
// Now imagine your route is declared with the table related model as first param like this:
Route::get('/user/edit/{$user}/child/{$child}/{otherParam}', 'UsersController@edit')->name('user.edit');
// in this case only, you will be able to declare your routes without keys:
// In this case only, you will be able to declare your routes without keys:
(new Table)->model(User::class)->routes([
// ...
'edit' => ['name'=> 'user.edit', 'params' => [$child, 'otherParam']],
// ...
]);
// because the route params are given in the same order as the route declaration:
// Because the route params are given in the same order as the route declaration:
route('user.edit', [$user, $child, 'otherParam']);
```

Expand Down Expand Up @@ -486,7 +486,7 @@ class UsersTable extends AbstractTable

```php
(new Table)->rowsNumber(50);
// or
// Or
(new Table)->rowsNumber(null);
```

Expand Down Expand Up @@ -666,15 +666,16 @@ class UsersTable extends AbstractTable
**Javascript snippet example:**

```javascript
// example of javascript snippet to ask a confirmation before executing the destroy action.
// this js snippet uses the `data-confirm` attribute value provided in the use case example above
const destroyButton = $('form.destroy button[type="submit"]');
destroyButton.click((e) => {
e.preventDefault();
const $this = $(e.target);
const message = $this.data('confirm');
const form = $this.closest('form');
if(confirm(message)) {
// Example of javascript snippet to ask a confirmation before executing the destroy action
// This js snippet uses the `data-confirm` attribute value provided in the use case example above
const destroyButtons = $('table form.destroy-action button[data-confirm]');
destroyButtons.click((event) => {
event.preventDefault();
const $this = $(event.target);
const $destroyButton = $this.is('button') ? $this : $this.closest('button');
const message = $destroyButton.data('confirm');
const form = $destroyButton.closest('form');
if (message && confirm(message)) {
form.submit();
}
});
Expand Down Expand Up @@ -1006,7 +1007,7 @@ $table->column()->title('E-mail');
```php
$table->column('email')->sortable();

// alternative
// Alternative
$table->column('email')->sortable(true, 'desc');
```

Expand All @@ -1024,18 +1025,18 @@ $table->column('email')->sortable(true, 'desc');
**Use case example:**

```php
// example 1
// Example 1
$table->column('email')->searchable();

// example 2
// Example 2
$table = (new Table)->model(User::class)->query(function(Builder $query) {
$query->select('users.*');
$query->addSelect('companies.name as company');
$query->join('companies', 'companies.owner_id', '=', 'users.id');
});
$table->column('company')->searchable('companies', ['name']);

// example 3
// Example 3
$table = (new Table)->model(User::class)->query(function(Builder $query) {
$query->select('users.*');
$query->addSelect(\DB::raw('CONCAT(companies.name, " ", companies.activity) as company'));
Expand Down Expand Up @@ -1089,13 +1090,13 @@ $table->column('email')->button(['btn', 'btn-sm', 'btn-primary']);
**Use case example:**

```php
// example 1
// Example 1
$table->column('url')->link();

// example 2
// Example 2
$table->column()->link(route('news.index'));

// example 3
// Example 3
$table->column()->link(function(News $news) {
return route('news.show', $news);
});
Expand Down
2 changes: 1 addition & 1 deletion docs/upgrade-guides/from-v1-to-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Instead of this way in previous versions:

```php
$table = (new Table);
// ... any table configuration
// ... Any table configuration
$table->render();
$table->list->getCollection();
```
Expand Down
3 changes: 1 addition & 2 deletions src/LaravelTableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public function boot(): void
$this->publishes([
__DIR__ . '/../resources/views' => resource_path('views/vendor/laravel-table'),
], 'laravel-table:views');
// we load the laravel html helper package
// https://github.com/Okipa/laravel-html-helper
// We load the laravel HTML helper package : https://github.com/Okipa/laravel-html-helper
$this->app->register(HtmlHelperServiceProvider::class);
}

Expand Down

0 comments on commit eb4cca2

Please sign in to comment.