2017 © Pedro Peláez
 

library resource-table

Alternative for Laravel DataTable

image

msieprawski/resource-table

Alternative for Laravel DataTable

  • Monday, April 18, 2016
  • by msieprawski
  • Repository
  • 13 Watchers
  • 64 Stars
  • 318 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 15 Versions
  • 6 % Grown

The README.md

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., (*1)

TO DO

  • default value for searchable columns
  • more searchable columns types (date, datetime, range)
  • add some tests

Compatibility

Currently package is compatible with Laravel 5, (*2)

Feature overview

  • supporting Eloquent ORM and Fluent Query Builder
  • ability to join tables and sort results by joined columns
  • searchable columns - select or text fields!
  • supporting filter callbacks
  • custom pagination layouts (called presenters in Laravel 5)
  • translations
  • more coming...

Installation

Add the following to your composer.json file:, (*3)

"msieprawski/resource-table": "*"

Then register this service provider with Laravel:, (*4)

'Msieprawski\ResourceTable\ResourceTableServiceProvider',

and add class alias for easy usage, (*5)

'ResourceTable' => 'Msieprawski\ResourceTable\ResourceTable',

Don't forget to use composer update and composer dump-autoload when is needed!, (*6)

Usage

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

Examples

Example 1: Simple use

$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

$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>';
        }
    ])
    // Or you can use a string which is a name of renderer class
    // NOTE: all renderer objects must have callable "render" method - an $row stdClass will be given with all row data
    ->addColumn([
        'index' => 'operations',
        'label' => 'Operations',
        'sortable' => false,
        'renderer' => 'Your\Full\Namespace\To\Renderer',
    ])
    ->make();

Example 3: Joining tables

$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

$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)
    ->paginate(true)
    ->sort('id', 'DESC')
    ->filter(true)
    ->customView('my.custom.view.name')
    ->make();

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

Example 5: Setting up the searchable columns

Let's say your news can be event or hot_topic type., (*9)

$news = DB::table('news')
    ->select(['news.id', 'news.subject', 'news.type']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'id',
        'label' => 'ID',
        'sortable' => true,
    ])
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
        'searchable' => true,
    ])
    ->addColumn([
        'index' => 'type',
        'label' => 'Type',
        'sortable' => true,
        'searchable' => true,
        'type' => 'select',
        'options' => [
            'event' => 'Event',
            'hot_topic' => 'Hot topic',
        ]
    ])
    ->make();

Example 6: Custom filter logic

$news = DB::table('news')
    ->select(['news.subject']);

echo ResourceTable::of($news)
    ->addColumn([
        'index' => 'subject',
        'label' => 'Subject',
        'sortable' => true,
        'searchable' => true,
        'filter' => function($value) {
            // Do whatever you want with given value!
            return trim(mb_strtolower(($value));
        },

        // You can specify column name to search
        'filter_column' => 'subject_alias',
    ])
    ->make();

Resource Table will generate a thead tag with two rows. First will contain standard th columns but the second one will contain text inputs or select fields (depends on column configuration). Currently Resource Table version supports following column types: - string - script will be looking for value matching pattern index LIKE '%value%' (used by default) - select - script will be looking for value matching pattern index = 'value', (*10)

Note

Resource Table will automatically inject All option with _all key to your all select type columns., (*11)

Creating pagination presenters

Let's say you don't want to use default built-in Bootstrap 3 pagination HTML structure for your pagination. With ResourceTable you can create your own one or use built-in AdminLTEPresenter. So if you'are using Admin LTE Admin Theme you don't have to worry about pagination HTML! By default ResourceTable use Bootstrap 3 presenter which is default for Laravel 5., (*12)

Using Admin LTE pagination presenter

$collection = ResourceTable::of($news)
    ->addColumn...
    ...
    ->setPaginationPresenter('Msieprawski\ResourceTable\Presenters\AdminLTEPresenter');

Note

Remember to use full path to class!, (*13)

Creating your own pagination presenter

You can create your pagination presenter wherever you want but it's recommended to create Presenters directory under your app directory. For this example I created MyCustomPresenter under app/Presenters directory:, (*14)

<?php namespace App\Presenters;

use Msieprawski\ResourceTable\Presenters\DefaultPresenter;

class MyCustomPresenter extends DefaultPresenter
{
    protected function getAvailablePageWrapper($url, $page, $rel = null)
    {
        $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';
        return '<li class="my-custom-class-here"><a href="'.htmlentities($url).'"'.$rel.'>'.$page.'</a></li>';
    }
}

All of your custom presenters must extends DefaultPresenter class. Feel free to see how it works (it's strongly based on Laravel's BootstrapThreePresenter). Just copy method which is responsible for element that you want to customize and change it! That's it!, (*15)

After creating your custom presenter - don't forget to set it in ResourceTable:, (*16)

$collection = ResourceTable::of($news)
    ->addColumn...
    ...
    ->setPaginationPresenter('App\Presenters\MyCustomPresenter');

Templating

Resource Table allows you to create your own templates! However if you don't need to use own templates, then you are free to use one of the following built-in views: - 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!, (*17)

Using built-in table templates

If you want to use core template just call view() method on your ResourceTable object:, (*18)

$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>:, (*19)

@if ($collection_generator->renderFilterForm())


Reset form
@endif {!! $table->head() !!} @if (empty($collection)) @else @foreach ($collection as $row) @foreach ($columns as $column) @endforeach @endforeach @endif
No records found.
{!! $column->content($row) !!}
@if ($collection_generator->renderFilterForm())
@endif @if ($paginator) {!! $paginator->render() !!} @endif

In every view template you are free to use following variables: - $collection - an array with all results (literally array with arrays) - $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) - $collection_generator - collection generator object (see Msieprawski\ResourceTable\Generators\Collection for available methods), (*20)

At the end just tell your ResourceTable object to use your custom template:, (*21)

$collection = ResourceTable::of($news)
    ->addColumn...
    ...
    ->customView('tables.my_table');

Auto-generated searchable content for column

Let's say you want to use $column->searchableContent() in your custom template but you want to add custom class for every input or placeholder. It's very easy:, (*22)

@foreach ($columns as $column)
    @if (!$column->searchable())
    <td></td>
    @else
    <td>{!! $column->searchableContent(['control_class' => 'form-control input-sm my-custom-class', 'placeholder' => 'Custom placeholder for '.$column->label()]) !!}</td>
    @endif
@endforeach

Setting default configuration for each ResourceTable object

If you want to set bootstrap view for each ResourceTable object or you want to set 100 elements per page you can use ResourceTable available static functions: - ResourceTable::setPaginationPresenter() - set custom or buil-in pagination presenter object name - ResourceTable::setView() - use this if you want to use buil-in template - ResourceTable::setCustomView() - use this if you've created your own template - ResourceTable::setPaginate() - enable/disable pagination - ResourceTable::setPerPage() - how many results to display on page - ResourceTable::setPage() - hardcode specific page (not sure why I've created this) - ResourceTable::setFilter() - enable/disable results filter - ResourceTable::setRendererNamespace() - set default renderers class namespaces to prevent long class names You can call it within boot method in AppServiceProvider object:, (*23)

public function boot()
{
    ResourceTable::setView('bootstrap')
    ResourceTable::setPaginationPresenter('App\Presenters\MyCustomPresenter')
}

Translations

ResourceTable has built-in polish and english translations. Please contact me if you've created translations for more languages - I'll be happy to share it with others! If you want to use your own translations or override existing please follow Laravel's instructions available here. Please use resource-table as package name and default.php as translations file., (*24)

<?php

return [
    'No_records' => 'No records found.',
    'Search' => 'Search',
    'Reset_form' => 'Reset form',
    'All' => 'All',
    'Search_for' => 'Search for',
];

License

Licensed under the MIT License, (*25)

The Versions

18/04 2016

dev-develop

dev-develop

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

18/04 2016

dev-master

9999999-dev

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

16/07 2015

v0.8

0.8.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

10/07 2015

v0.71

0.71.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

01/07 2015

v0.7

0.7.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

21/06 2015

v0.6.1

0.6.1.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

21/06 2015

v0.6

0.6.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

18/05 2015

v0.5.1

0.5.1.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

18/05 2015

v0.5

0.5.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

18/05 2015

v0.4.1

0.4.1.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

14/05 2015

v0.4

0.4.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

13/05 2015

v0.3

0.3.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

13/05 2015

v0.2

0.2.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

12/05 2015

v0.1.1

0.1.1.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski

12/05 2015

v0.1

0.1.0.0

Alternative for Laravel DataTable

  Sources   Download

The Requires

 

by Mateusz Sieprawski