lloricode / laravel-html-table
laravel的HTML表格生成器
v2.1.1
2024-03-05 15:18 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.16.2
Requires (Dev)
- composer-runtime-api: ^2.2
- larastan/larastan: ^2.9.2
- laravel/pint: ^1.14
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.34.1
- phpstan/phpstan: ^1.10.59
- rector/rector: ^1.0.2
- spatie/pest-plugin-snapshots: ^2.1.0
README
使用数组/对象数据生成HTML表格。
安装
您可以通过composer安装此包
composer require lloricode/laravel-html-table
使用方法
视图中的示例
$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 ) !!}
这些都是默认的HTML标签值
$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) !!}
示例输出
<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>
在单元格数据中添加属性
$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>
测试
composer test
更新日志
请参阅 更新日志 了解最近的变化。
贡献
请参阅 贡献指南 了解详情。
鸣谢
许可证
MIT许可证(MIT)。请参阅 许可证文件 获取更多信息。