PHP code example of msieprawski / resource-table

1. Go to this page and download the library: Download msieprawski/resource-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

msieprawski / resource-table example snippets


'Msieprawski\ResourceTable\ResourceTableServiceProvider',

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

$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();

$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();

$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();

$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();

$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();

$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();

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

 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>';
    }
}

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

$collection = ResourceTable::of($news)
    ->addColumn...
    ...
    ->view('bootstrap');

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

@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

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



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