定制 mzprog/datatables 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mzprog/datatables

Composer 安装命令:

composer require mzprog/datatables

包简介

simple laravel/livewire datatable easy to use

README 文档

README

create a better tables with few lines of code.

Installation

simply install the package by:

composer require mzprog/datatables

make sure that livewire is installed, and styles and scripts added to the blade layout: @livewireStyles and @livewireScripts

Basic Usage

  • create a livewire component, without view file, and without render method.
  • The component should extends Mzprog\Datatables\Datatable
  • add columns method to your component
    • public function columns(): array;
    • This Should return array of Mzprog\Datatables\Column
  • add query method to have the main query
    • public function query() : Builder

Column class

just add Column::name('id') to your columns array, and it will be added and viewed in your table, but it will be labeled as Id.

To change the label name just use the second parameter Column::name('id', 'ID').

The name will be used as the array index, and database field by defualt.

field method is used to allow you to different key for the column as in this example(orderable& edit will be explained later):

Column::name('added')->field('created_at')
    ->orderable()
    ->edit(fn($row) => $row->created_at->diffForHumans()),

we have change the format for created_at but can't store it in same properties (because casted to date, and work as getCreateAtAttribute).

so when use orderable it will order using created_at.

edit method is used to change or add data.
it accept callback with the current raw data as parameter, example:

Column::name('full_name', 'Name)
    ->edit(fn($raw) => "{$raw->first_name} {$raw->last_name}")

orderablemethod will allow you to order the column by pressing on the column name. it will order your data based on your field name, unless you add a callback as a parameter. the callback parameter are: the query Builder, and the order direction, example:

return [
    Column::name('id', 'ID')->orderable(),
    Column::name('full_name', 'Name)
        ->orderable(fn($q, $dir) => $q->orderBy('first_name', $dir)->orderBy('last_name', $dir))
        ->edit(fn($raw) => "{$raw->first_name} {$raw->last_name}"),
];

searchable method is work as orderable, and the callback function will pass the search keyword instead of order direction, example:

Column::name('full_name', 'Name)
    ->searchable(
        fn($q, $keyword) => $q
            ->where(DB::raw('CONCAT(first_name," ", last_name)'), 'like', "%${keyword}%")
    )
    ->orderable(fn($q, $dir) => $q->orderBy('first_name', $dir)->orderBy('last_name', $dir))
    ->edit(fn($raw) => "{$raw->first_name} {$raw->last_name}"),

raw method is used when you need to print HTML in your table. example:

Column::name('actions')->raw()
    ->edit(fn($row) => '<a href="' . route('users.show',['user' => $row->id]) . '" >View</a>';

Filter Class

If you want to select one or more value as a filter, like you only need to see rows from today, you can use:

public function filters()
{
	return [
        Filter::name('date', 'Select a Date'),
    ];
}

this will let you filter from your table using date column, also you can skip the label name, by defualt it will be Date.

also you can use custom data and filters:

example 1 (if you want to filter by name first letter):

Filter::name('name')->options(function () {
    $options = User::query()
        ->select([
            DB::raw('LEFT(name,1) as letter'),
            DB::raw('COUNT(*) as total')
        ])->groupBy('letter')->get();

    return $options->map(fn ($d) => [
        'value' => $d['letter'],
        'name' => "Starts with '{$d['letter']}'",
        'total' => $d['total'],
    ])->toArray();
})
->filter(function (Builder $query, array $values) {
    $query->whereIn(DB::raw('LEFT(name,1)'), $values);
})

for options you need to return array of options, and option has (name, value, total).

filter if this filter is selected you can add conditions to the provided query, and you will get also array of the selected values.

example 2(filter by success: success, fail)

Filter::name('success')->options(function () {
    return [
        [
            'name' => 'Success',
            'value' => '>=',
            'total' => Exam::where('points', ">=", 50)->count()
        ],
        [
            'name' => 'Fail',
            'value' => '<',
            'total' => Exam::where('points', "<", 50)->count()
        ],
    ];
})
->filter(function (Builder $query, array $values) {
    if(count($values) ==2) return;
    $val = current($values);
    $query->whereIn('points', $val, 50);
})

mzprog/datatables 适用场景与选型建议

mzprog/datatables 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mzprog/datatables 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mzprog/datatables 我们能提供哪些服务?
定制开发 / 二次开发

基于 mzprog/datatables 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-03