<commit_before># Resource Table
## About
This Laravel package has been created as a alternative for DataTable. It doesn't use AJAX or any JavaScript. It's very light and scalable. Use it for generating table with data without paying attention to searching/sorting/paginating results. It'll do it for yourself! I'll do my best to develop it all the time because I'll be using it on my projects. 

## TO DO
 - custom table layouts
 - searchable columns
 - custom pagination layouts *(not available in Laravel 5 but it'll be using this package)*
 - add some tests
 
## Compatibility
Currently package is compatible with Laravel 5

## Feature overview
 - supporting Eloquent ORM and Fluent Query Builder
 - ability to join tables and sort results by joined columns
 - uses built in Laravel's paginator
 - more coming...
 
## Installation
Add the following to your `composer.json` file:

```json
"msieprawski/resource-table": "*"
```

Then register this service provider with Laravel:

```php
'Msieprawski\ResourceTable\ResourceTableServiceProvider',
```

and add class alias for easy usage
```php
'ResourceTable' => 'Msieprawski\ResourceTable\ResourceTable',
```

Don't forget to use ``composer update`` and ``composer dump-autoload`` when is needed!

## Usage
All you need to do is creating a `ResourceTable` with prepared builder object. Then add columns and call `make()`. That's it!

## Examples
### Example 1: Simple use
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->make();
```

### Example 2: Adding columns
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => function ($row) {
            return '<a href="'.url('news/'.$row->id.'/delete').'">Delete</a>';
        }
    ])
    ->make();
```

### Example 3: Joining tables
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject', 'categories.name AS category_name'])
    ->leftJoin('categories', 'news.category_id', '=', 'categories.id');

return ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'category_name',
        'label' => 'Category',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => function ($row) {
            return '<a href="'.url('news/'.$row->id.'/delete').'">Delete</a>';
        }
    ])
    ->make();
```

### Example 4: Set custom conditions
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->perPage(20)
    ->page(2)
    ->orderBy('id', 'DESC')
    ->make();
```

Where `perPage(20)` sets resources per page. Method `page(2)` sets current page. Method `orderBy('id', 'DESC')` sets default sorting.

## License
Licensed under the MIT License<commit_msg>Added templating guide to README.md<commit_after># Resource Table
## About
This Laravel package has been created as a alternative for DataTable. It doesn't use AJAX or any JavaScript. It's very light and scalable. Use it for generating table with data without paying attention to searching/sorting/paginating results. It'll do it for yourself! I'll do my best to develop it all the time because I'll be using it on my projects. 

## TO DO
 - custom table layouts
 - searchable columns
 - custom pagination layouts *(not available in Laravel 5 but it'll be using this package)*
 - add some tests
 
## Compatibility
Currently package is compatible with Laravel 5

## Feature overview
 - supporting Eloquent ORM and Fluent Query Builder
 - ability to join tables and sort results by joined columns
 - uses built in Laravel's paginator
 - more coming...
 
## Installation
Add the following to your `composer.json` file:

```json
"msieprawski/resource-table": "*"
```

Then register this service provider with Laravel:

```php
'Msieprawski\ResourceTable\ResourceTableServiceProvider',
```

and add class alias for easy usage
```php
'ResourceTable' => 'Msieprawski\ResourceTable\ResourceTable',
```

Don't forget to use ``composer update`` and ``composer dump-autoload`` when is needed!

## Usage
All you need to do is creating a `ResourceTable` with prepared builder object. Then add columns and call `make()`. That's it!

## Examples
### Example 1: Simple use
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->make();
```

### Example 2: Adding columns
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => function ($row) {
            return '<a href="'.url('news/'.$row->id.'/delete').'">Delete</a>';
        }
    ])
    ->make();
```

### Example 3: Joining tables
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject', 'categories.name AS category_name'])
    ->leftJoin('categories', 'news.category_id', '=', 'categories.id');

return ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'category_name',
        'label' => 'Category',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => function ($row) {
            return '<a href="'.url('news/'.$row->id.'/delete').'">Delete</a>';
        }
    ])
    ->make();
```

### Example 4: Set custom conditions
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->perPage(20)
    ->page(2)
    ->orderBy('id', 'DESC')
    ->make();
```

Where `perPage(20)` sets resources per page. Method `page(2)` sets current page. Method `orderBy('id', 'DESC')` sets default sorting.

## Templating
Resource Table allows you to create your own templates! If you don't need to then you are free to use one of the following built-in templates:
 - `simple` *(default)*
 - `bootstrap` *(supported by Bootstrap 3)*
 - `advanced_example` - it's just a advanced template example, you can use it as a blueprint of your own one!

### Using built-in/custom table templates
If you want to use core template just call `view()` method on your `ResourceTable` object:
```php
$collection = ResourceTable::of($news)
    ->addColumn...
    ...
    ->view('bootstrap');
```

### Creating custom table template
Create your own blade view file, name it as you want. For this example I named my file `my_table.blade.php` under `tables` directory.
Let's say that I need to put custom attribute on each `<tr>` node in `<tbody>`:
```html
<table class="my-resource-table">
    {!! $table->head() !!}
    <tbody>
    @if (empty($collection))
        <tr><td colspan="{{ count($columns) }}">No records found.</td></tr>
    @else
        @foreach ($collection as $row)
            
            <!-- Here is my custom attribute -->
            <tr data-id="{{ $row->id }}">
            
                @foreach ($columns as $column)
                <td>{!! $column->content($row) !!}</td>
                @endforeach
            </tr>
        @endforeach
    @endif
    </tbody>
</table>
@if ($paginator)
{!! $paginator->render() !!}
@endif
```

In every view template you aree free to use following variables:
 - `$collection` - it's just a array with all results
 - `$columns` - an array with table columns objects (see `Msieprawski\ResourceTable\Helpers\Column` for available methods)
 - `$paginator` - an Laravel's built-in pagination presenter (for now it's a `Illuminate\Pagination\BootstrapThreePresenter`)
 - `$table` - table generator object (see `Msieprawski\ResourceTable\Generators\Table` for available methods)
 
At the end just tell your `ResourceTable` object to use your custom template:
```php
$news = DB::table('news')
    ->select(['news.id', 'news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
    ])
    ->customView('tables.my_table')
    ->make();
```

## License
Licensed under the MIT License
{'fraction_non_alphanumeric': 0.1726149283242709, 'fraction_numerical': 0.002471576866040534, 'mean_word_length': 2.8537142857142856, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 165, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '995493', 'n_tokens_mistral': 3122, 'n_tokens_neox': 3022, 'n_words': 962}