codeator / laravel-table by codeator

Table functionality for Laravel collections
176
7
4
Package Data
Maintainer Username: codeator
Maintainer Contact: codeator@gmail.com (codeator)
Package Create Date: 2016-10-13
Package Last Update: 2016-12-11
Language: PHP
License: MIT
Last Refreshed: 2024-04-14 15:07:16
Package Statistics
Total Downloads: 176
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 7
Total Watchers: 4
Total Forks: 9
Total Open Issues: 0

Laravel table

Laravel module for table rendering

##Installation First, require the package using Composer:

composer require codeator/laravel-table

Add to your config/app.php providers section

Codeator\Table\Providers\TableServiceProvider::class

and to facades section

'Table' => Codeator\Table\Facades\Table::class

##Examples

##Translations

Soon

###Table class

<?php

namespace App\Tables\Admin;

use App\Models\User;
use App\Models\Role;
use Table;
use Codeator\Table\Filter\SelectFilter;

class UsersTable extends Table
{

    public static function create() {
        $roles = Role::all()->pluck('name', 'id');
        $table = self::from(new User())
            ->columns([
                'id' => 'Id',
                'name' => 'Name',
                'email' => 'Email',
                'balance' => 'Balance',
                'created_at' => 'Created at'
            ])
            /*
            For filters allowed types: string, range, date
            */
            ->filters([
                'name' => 'string',
                /*
                String filter can be strict (key=value) or not (key like %value%)
                */
                'email' => 'string|strict:true',
                'role_id' => (new SelectFilter('role_id'))
                    ->options($roles)
                    ->label('Role'),
                /*
                You can specify settings for some of the filters.
                Eg: range filter can take multiplier as the argument
                 */
                'balance' => 'range|multiplier:10000',                
                'created_at' => 'date'
            ])
            /*
            For totals allowed types: sum, count
            */
            ->totals([
                'balance' => 'sum',
                'id' => 'count'
            ])
            /*
            Sometimes you need to filter results without input
            If User with moderation role can't view admin users
            */
            ->filterCallback(function($model) {
                if (auth()->user() && auth()->user()->role->type == Role::TYPE_MODERATOR) {
                    $model = $model->whereHas('role', function($query) {
                        $query->where('type', '<>', Role::TYPE_ADMIN);
                    });
                }
                return $model;
            })
            /*
            Group operations for query
            Label for delete batch action is: trans('table::batch.labels.delete');
            */
            ->batchActions([
                'delete' => function ($queryBuilder) {
                    $queryBuilder->delete();
                }
            ])
            ->sortables([
                'id'
            ])
            ->exporters(['csv'])
            /*
                Route name -> Link name in last column
            */
            ->actions([
                'admin.users.view' => 'View'
            ])
            ->orderBy('id', 'asc')
            ->paginate(20);

        return $table;
    }

}

###Controller

<?php

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Tables\Admin\UsersTable;

class UsersController extends Controller
{

    public function index() {
        $table = UsersTable::create();

        return view('admin.users.index', [
            'table' => $table
        ]);
    }

}

###View

@extends('admin.layout')

@section('content')
    {!! $table->render() !!}
@stop

###Sample Sass based on bootstrap v4

.table {
  tbody {
    .ctable-total-content {
      border: none;
      background-color: transparent !important;

      td {
        border: none;
        background-color: transparent;
        font-size: 80%;
        padding-top: 0;
      }
    }

    .ctable-total-heading {
      border: none;
      background-color: transparent !important;

      td {
        border: none;
        background-color: transparent;
        font-size: 80%;
        font-weight: bold;
      }
    }

    tr:nth-of-type(even) {
      td.ctable-ordered {
        background-color: rgba(0, 0, 0, 0.10);
      }
    }

    tr:nth-of-type(odd) {
      td.ctable-ordered {
        background-color: rgba(0, 0, 0, 0.17);
      }
    }
  }

  thead {
    a {
      text-decoration: none;
    }
  }

  .table-arrow-up:before {
    font: normal normal normal 16px/1 "Material Design Icons";
    content: mdi('arrow-up');
    text-decoration: none;
  }

  .table-arrow-down:before {
    font: normal normal normal 16px/1 "Material Design Icons";
    content: mdi('arrow-down');
    text-decoration: none;
  }
}

###Some recommendations for JS

Use Flatpickr library for datepicker

require('flatpickr')('[data-toggle=datepicker]');