承接 camohub/laravel-datagrid 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

camohub/laravel-datagrid

Composer 安装命令:

composer require camohub/laravel-datagrid

包简介

Datagrid for Laravel

README 文档

README

Laravel datagrid

This is the datagrid for Laravel models.

Installation

composer install camohub/laravel-datagrid

Description

Datagrid constructor accepts instance of Illuminate\Database\Eloquent\Builder or Illuminate\Database\Query\Builder as datasource.

This package is based on form GET request. Whole table is a form. You can simply catch submit event if you need. Empty inputs are disabled on submit by js and automatically removed from url. Datagrid contains this groups of inputs:

  • sort inputs - every sortalbe field has its own hidden input. After click on the sortable column js sets the hidden input value. If value is empty hidden input is disabled.
  • filter inputs - filter inputs triggers form submit on input event. There is also timeout as throttling to wait for another input events. This timeout can be set in php grid definition globally by setJSFilterTimeout().
  • perPage select - onchange event triggers form submit immediately.
  • page - paginator page param is also as hidden input.

This form submit implementation has one little disadvantage. It removes all other GET parameters from url. But it is easy to fix it. You can set all necessary GET parameters via $grid->addGetParam('name').

Example

Controller code could implement a method which returns datagrid instance.

public function getArticlesDatagrid()
{
    $grid = new Datagrid(Article::with('user'));

    $grid->addColumn('id')
        ->setSort();

    $grid->addColumn('title')
        ->setSort()
        ->setFilter(function($model, $value) {
            return $value ? $model->where('title', 'like', "%$value%") : $model;
        });

    $grid->addColumn('created_at', 'Created')
        // Needs valid js regexp pattern.
        ->setJSFilterPattern('\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}')
        ->setRender(function($value, $item) {
            return '<b>' . $value->format('d.m.Y H:i') . '</b>';
        })
        // Turns off template html escaping.
        ->setNoEscape()
        ->setSort();

    $grid->addColumn('visible', 'Visible')
        ->setOutherClass(function($value, $row) {
            return $value ? 'bg-primary text-center' : 'bg-danger text-center';
        })
        ->setSelectFilter([0 => 'hidden', 1 => 'active'], 'all')
        ->setFilter(function ($model, $value) {
            return $model->where('visible', $value);
        });

    // HasOne relation
    $grid->addColumn('user.name', 'User');

    // ManyHasMany relation
    $grid->addColumn('user.roles', 'Roles')
        ->setRender(function($value, $item) {
            return $value->map( function($value) { return $value->name; } )->join(', ');
        });

    // TYPE_CUSTOM intended for content not related to model.
    $grid->addColumn('', '', Column::TYPE_CUSTOM)
        ->setNoEscape()
        ->setRender(function($value, $item) {
            return '
                <a href="' . route('admin.articles.edit', ['id' => $item->id]) . '">edit</a>
                <a href="' . route('admin.articles.visibility', ['id' => $item->id]) . '">visibility</a>
                <a href="' . route('admin.articles.delete', ['id' => $item->id]) . '" class="text-danger">delete</a>
            ';
        });

    return $grid;
}

And the template could look like

{{$grid->render()}}

Options

There are two groups of options. Global datagrid options and column specific options.

Datagrid options

  • setDefaultSort() - sort callback used when no other sort filter is in use.

  • setDefaultPerPage() - yes it really sets the default perPage items number.

  • setPerPage() - expects array with possible dropdown options like [10, 25, 50, 100].

  • setOnEachSide() - it is the wrapper above the Laravel pagination onEachSide() option.

  • setTableClass() - default is 'table table-striped table-hover table-bordered';

  • setJSFilterTimeout() - sets javascript timeout on input event. Default is 250ms.

  • setSubmitOnEnter() - prevent submit on input event and will wait for hit enter key to submit. This option is possible to set for the whole grid or for one column. Does not affect sorting, pagination and perPage select. They are still automatically submited.

  • setGetParams() - form submit removes all GET params from url which are not the part of the form. Request will contain only form inputs as GET prameters. setGetParams('paramName') will include all necessary GET params which should be included in all datagrid GET requests.

Column options

  • setRender() - accepts callback with two parameters - value and row.

  • setSort() - accepts empty to simple sort according flied name or callback which gets two params - queryBuilder and sort value.

  • setFilter() - accepts callback with two parameters - queryBuilder and filter value. Filter callback is not called if filter value is NULL or empty string. Other values like 0 will call the filter.

  • setFilterSelect() - expects associative array and options prompt parameter. This function renders select element in the filter field.

  • setJSFilterPattern() - accepts js regexp patterns as string. If value does not match the pattern validator will block the request and will add .text-danger class to input field.

  • setSubmitOnEnter() - prevent submit on input event and will wait for hit enter key to submit. This option is possible to set for the whole grid or for one column. Does not affect sorting, pagination and perPage select. They are still automatically submited.

  • setFilterRender() - allows you to render filter input manually. Be sure rendered filter input has css class chgrid-filter.

  • setNoEscape() - custom render wont be escaped. Template use {!! !!} instead of {{}}.

  • setOutherClass() - accepts callback. Callback will be useful if you need to make some conditional styles for the field. Callback will get two parameters - value and row.

  • setOutherTitleClass - accepts string value. Will set up css class of TH element with title.

camohub/laravel-datagrid 适用场景与选型建议

camohub/laravel-datagrid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 camohub/laravel-datagrid 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: mit
  • 更新时间: 2021-07-23