irpcpro/table-soft

用于管理表格数据的包。将模型数据转换为表格对象。

2.0.0 2023-01-13 18:21 UTC

This package is auto-updated.

Last update: 2024-09-30 01:47:15 UTC


README

版本: 2.0.0

用于管理表格数据的包。将您的模型或数据列表转换为表格对象,轻松...

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

TableSoft

安装

将包安装到您的laravel项目中

composer require irpcpro/table-soft

通过将TableSoft服务提供者添加到config/app.php文件中的提供者中注册TableSoft服务提供者。

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

如果您想,您可以通过将其添加到config/app.php文件中的别名中为TableSoft门面添加别名。

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

配置

  • 将Collection或Builder数据传递给门面。
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);
    }
}

创建列

用于向表格添加列

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

用于从特定的键名获取数据

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

可以使用这些类型的数据

  • 整数
  • 字符串
  • 浮点数
  • 日期
  • 布尔值

用于排序数据

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

可以使用这些类型的排序数据

  • 升序
  • 降序

用于值的回调函数

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

也可以在不排序数据的情况下使用

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

第二个(fieldName:type)参数必须设置

设置可搜索

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

或定义列后设置

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

设置列宽

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

在第二个参数中设置度量

  • 像素
  • %

自动设置行计数器

$table->rowCounter('row', 'row-name:string', function($val){
    return $value;
});
重要:字段名应以`row`开头

为列表设置分页

$table->paginate(10);

如果设置为0,将返回所有数据。(无限制)

设置缓存数据

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

id-name-table应该是此表特定的唯一字符串。

从服务获取数据

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

更多

// 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();
  • 响应有几个控制器来管理您的表格
array:5 [▼
  "head" => Illuminate\Support\Collection {#334 ▶}
  "body" => Illuminate\Pagination\LengthAwarePaginator {#339 ▶}
  "sort_fields" => Illuminate\Support\Collection {#316 ▶}
  "query_params" => array:3 [▶]
  "exists" => true
]

headbody的数据结构相同

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

以下是一个在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>