Coder Social home page Coder Social logo

laravel-html-table's Introduction

Laravel HTML Table

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads codecov

Generate Html Table with data from array/object.

Installation

You can install the package via composer:

composer require lloricode/laravel-html-table

Usage

Sample in view

$headers = ['col1', 'col2'];

$data = [
    [
        'Lloric', 'Garcia',
    ],
    [
        'Foo', 'Bar',
    ],
    [
        'Foo1', 'bar11',
    ],
    [
        'tst', 'tesss',
    ],
];

$attributes = 'class="table"';
// Or
$attributes = ['myclass' => 'test_val'];

{!! Table::generate($headers, $data) !!}

{!! Table::generate($headers, $data, $attributes) !!}

// Model way

{!! 
    Table::generateModel(
        ['Id', 'Name', 'Email'],  // Column for table
        'App\User' // Model
        ,['id', 'name', 'email'], // Fields from model
        0, // Pagination Limit, if 0 all will show
        'border="1"' // Attributes sample js/css
    ) 
!!}

{{ Table::links() }} // Generate this when limit is not 0

// then you can add a links

{!!
    Table::optionLinks('my.route.name')
        ->modelResult(function($query){  // you can add filter if you are using model generate
            $query->where('user_id', auth()->user()->id);
            return $query;
        })
        ->generateModel(
            ['Id', 'Name', 'Email'],  // Column for table
            'App\User' // Model
            ,['id', 'name', 'email'], // Fields from model
            5, // Pagination Limit, if 0 all will show
            'border="1"' // Attributes sample js/css
        ) 
!!}

// you can specify more args
// 1st route name, 2nd header label, and 3rd is the every row label
{!! 
    Table::optionLinks('my.route.name', 'my option', 'view')
        ->generateModel(
            ['Id', 'Name', 'Email'],  // Column for table
            'App\User' // Model
            ,['id', 'name', 'email'], // Fields from model
            5, // Pagination Limit, if 0 all will show
            'border="1"' // Attributes sample js/css
        ) 
!!}

This is all default values html tags

$attributes = [
    // Main Table
    'table' => '<table>',
    'table_end' => '</table>',

    // Head
    'head' => '<thead>',
    'head_end' => '</thead>',

    'head_row' => '<tr>',
    'head_row_end' => '</tr>',
    'head_cell' => '<th>',
    'head_cell_end' => '</th>',

    // Data body
    'body' => '<tbody>',
    'body_end' => '</tbody>',

    'body_row' => '<tr>',
    'body_row_end' => '</tr>',
    'body_cell' => '<td>',
    'body_cell_end' => '</td>',

    // Alternative
    'alt_body_row' => '<tr>',
    'alt_body_row_end' => '</tr>',
    'alt_body_cell' => '<td>',
    'alt_body_cell_end' => '</td>',
];

{!! Table::generate($headers, $data, $attributes) !!}

Sample Output

<table myclass="test_val"><thead><tr><th>col1</th><th>col2</th></tr></thead><tbody><tr><td>Lloric</td><td>Garcia</td></tr><tr><td>Foo</td><td>Bar</td></tr><tr><td>Foo1</td><td>bar11</td></tr><tr><td>tst</td><td>tesss</td></tr></tbody></table>

Adding attributes in cell data

$header = ['Date', 'Description', 'Amount'];
$datas = [
    [
        ['data' => '1', 'scope' => 'row'],
        'Mark',
        'Otto',
    ],
    [
        ['data' => '2', 'scope' => 'row'],
        'foo',
        'varr',
    ],
];

{!! Table::generate($header, $datas, ['class'=>'table']) !!}

<table class="table">
   <thead>
       <tr>
           <th>Date</th>
           <th>Description</th>
           <th>Amount</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td scope="row">1</td>
           <td>Mark</td>
           <td>Otto</td>
       </tr>
       <tr>
           <td scope="row">2</td>
           <td>foo</td>
           <td>varr</td>
       </tr>
   </tbody>
</table>

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-html-table's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar lloricode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel-html-table's Issues

Table Links

This package is perfect for building admins on the fly but can you add links? Lets say I export all the blog posts like:

My Controller

$model 	= 'App\Post';
		$title 	= ['Title'];
		$fields = ['title'];

		
		return view('admin.dashboard',
        [
			'title' => $title,
			'model' => $model,
			'fields' => $fields
		]);

My View

{!! Table::generateTableFromModel(
                $title,  // Column for table
                $model,
                $fields, // Fields from model
                20) 
            !!}   

I would to add a link to the table row using $post->slug.

Set attributes to header cells

Hello!

I think it's really necessary to be able to set attributes to header cells, like it's possible to do it in body cells...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.