Skip to content

头部和脚部

头部和脚部

数据表格支持往头部和脚部两个区块填入你想要的内容

php
$table->header(function ($collection) {
    return 'header';
});

$table->footer(function ($collection) {
    return 'footer'; 
});

头部

php
$table->header(function ($collection) use ($table) {
    $query = Model::query();

    // 拿到表格筛选 where 条件数组进行遍历
    $table->model()->getQueries()->unique()->each(function ($value) use (&$query) {
        if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) {
            return;
        }

        $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
    });

    // 查出统计数据
    $data = $query->get();

    // 自定义组件
    return new Card($data);
});

脚部

一个比较常见的场景是在数据表格的脚部显示统计信息,比如在订单表格的脚部加入收入统计,可以参考下面的代码实现:

php
$table->footer(function ($collection) use ($table) {
    $query = Model::query();

    // 拿到表格筛选 where 条件数组进行遍历
    $table->model()->getQueries()->unique()->each(function ($value) use (&$query) {
        if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) {
            return;
        }

        $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
    });

    // 查出统计数据
    $data = $query->get();

    return "<div style='padding: 10px;'>总收入 : $data</div>";
});