irpcpro/table-soft

A package for managing table data.convert model data to objects of table.

2.0.0 2023-01-13 18:21 UTC

This package is auto-updated.

Last update: 2024-10-30 01:59:42 UTC


README

Version: 2.0.0

A package for managing table data. convert your model or list of your data to table object, easily...

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

TableSoft

Installation

install package to your laravel project:

composer require irpcpro/table-soft

Register the TableSoft service provider by adding it to the providers in config/app.php file.

'providers' => [
    ...
    ...
    \Irpcpro\TableSoft\ServiceProviders\TableSoftServiceProvider::class,
]

If you want you can alias the TableSoft facade by adding it to the aliases in config/app.php file.

'aliases' => Facade::defaultAliases()->merge([
    ...
    ...
    'TableSoft' => \Irpcpro\TableSoft\Facade\TableSoftFacade::class,
])->toArray(),

Configurations

  • Pass Collection or Builder data into the facade.
use TableSoft;

class HomeController extends Controller {
    public function index(){
        // get data from collection
        $data = collect([ [..], [..], [..] ]);
        
        // Or ..
        
        // get data from models
        $data = App\Models\Product::query();
       
        // finally pass the data to TableSoft
        $table = TableSoft::data($data);
    }
}

create columns

for adding column to table:

$table->column('Title') // default key name = Title

for get data from specific key name:

$table->column('Title', 'columnTitle')
$table->column('Title', 'columnTitle:string') // default type column is string

can use these type of data:

  • int
  • string
  • float
  • date
  • bool

for sorting data:

$table->column('Title', 'columnTitle:string', 'sort') // default ASC
$table->column('Title', 'columnTitle:string', 'sort:asc')

can use these type of sorting data:

  • asc
  • desc

callback function for value

$table->column('Price', 'price:int', 'sort', function($value){
    return $value . '$';
});

also use without sorting data

$table->column('Price', 'price:int', function($value){
    return $value . '$';
});

the second (fieldName:type) parameter must be set

set searchable:

$table->column('Price', 'price:int', function($value){
    return $value . '$';
})->searchable();

or set after define column:

$table = $table->column('Price', 'price:int', function($value){
    return $value . '$';
});
$table->searchable();

set width for column:

$table->setWidth(20);
$table->setWidth(20, 'px');

set measure in second parameter

  • px
  • %

set row counter automatically:

$table->rowCounter('row', 'row-name:string', function($val){
    return $value;
});
Important: the field name should start with `row`

set paginate for list:

$table->paginate(10);

if set 0 it will return all data. (without limitation)

set caching data:

$table->setCaching('id-name-table');

id-name-table should be a specific and unique string for this table.

get data from service:

$data = Http::get('https://...../products');
$data = collect($data->json());

for more:

// get data
$data = Product::query();

// set table
$table = TableSoft::data($data);
$table = $table->column('Title', 'title:string', 'sort')->searchable();
$table = $table->column('Image', 'thumbnail:string', function($value){
    return "<img src='$value'/>";
});
$table = $table->column('Description', 'description:string', 'sort:asc')->searchable();
$table = $table->column('Price', 'price:int', 'sort', function($value){
    return $value . '$';
})->setWidth(50, 'px')->searchable();
$table = $table->rowCounter('row')->setWidth(20,'px');
$table = $table->setCaching('table-product4');
$table = $table->paginate(10);

// get table
$data = $table->get();
  • the response have several controller for manage your table:
array:5 [▼
  "head" => Illuminate\Support\Collection {#334 ▶}
  "body" => Illuminate\Pagination\LengthAwarePaginator {#339 ▶}
  "sort_fields" => Illuminate\Support\Collection {#316 ▶}
  "query_params" => array:3 [▶]
  "exists" => true
]

the data of head and body have same data structure:

{
    +title: "Description"
    +name: "description"
    +type: "string"
    +sort: "sort"
    +sortBy: "asc"
    +value: "Description"
    +width: null
    +widthMeasure: null
    +searchable: true
}

here's a sample for show table in blade:

<table class="table table-bordered">
    <thead>
        <tr>
            @foreach($data['head'] as $head)
                <th width="{{$head->width ? $head->width.$head->widthMeasure : ''}}">{{$head}}</th>
            @endforeach
        </tr>
    </thead>
    <tbody>
        @foreach($data['body'] as $body)
            <tr>
                @foreach($body as $item)
                    <td width="{{$item->width ? $item->width.$item->widthMeasure : ''}}">{!! $item !!}</td>
                @endforeach
            </tr>
        @endforeach
    </tbody>
</table>