bored-programmers/laragrid 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

bored-programmers/laragrid

Composer 安装命令:

composer require bored-programmers/laragrid

包简介

LaraGrid is a powerful and customizable grid system package for Laravel. It provides an easy way to create sortable, filterable tables with pagination. The package is designed to be highly customizable, allowing developers to define columns, apply filters, and sort data with ease.Key Features:- **Co

README 文档

README

🚧 This package is in development, and is not ready for use in production yet. 🚧

LaraGrid Logo

Latest Stable Version Total Downloads License

LaraGrid is a Laravel package that provides a powerful and customizable grid system. It allows you to easily create sortable, filterable tables with pagination.

Table of Contents

Requirements

  • PHP 8.1 or higher
  • Laravel 10.0 or higher

Installation

To install LaraGrid, you need to run the following command:

composer require bored-programmers/laragrid

Publishable

You can publish the package's configuration, language files, and views using the following commands:

required

php artisan vendor:publish --tag=laragrid-assets

optional

php artisan vendor:publish --tag=laragrid-config
php artisan vendor:publish --tag=laragrid-lang
php artisan vendor:publish --tag=laragrid-views

TODO List

  1. Write tests for all functionality

Base Usage

Creating a Grid

To create a grid, you need to extend the BaseLaraGrid class and implement the getColumns, and getDataSource methods.

use BoredProgrammers\LaraGrid\Components\ColumnComponents\Column;
use BoredProgrammers\LaraGrid\Livewire\BaseLaraGrid;
use Illuminate\Database\Eloquent\Builder;
use BoredProgrammers\LaraGrid\Filters\FilterResetButton;

class MyGrid extends BaseLaraGrid
{

    protected function getColumns(): array
    {
        return [
            Column::make('id', 'ID'),
            Column::make('name', 'Name'),
            // Add more columns as needed
        ];
    }

    protected function getDataSource(): Builder
    {
        return MyModel::query();
    }

}

In the getColumns method, you define the columns that will be displayed in the grid. The Column::make method takes two arguments: the model field and the label.

The getDataSource method should return an instance of Illuminate\Database\Eloquent\Builder for the model you want to display in the grid.

Displaying the Grid

To display the grid in a Blade view, you can use the @livewire or <livewire> directive:

@livewire('my-grid')
<livewire:my-grid/>

Replace 'my-grid' with the actual name of your Livewire component.

Customizing the Theme

You can customize the appearance of the grid by extending the BaseLaraGridTheme class and setting the desired CSS.

use BoredProgrammers\LaraGrid\Theme\BaseLaraGridTheme;
use BoredProgrammers\LaraGrid\Theme\FilterTheme;
use BoredProgrammers\LaraGrid\Theme\TBodyTheme;
use BoredProgrammers\LaraGrid\Theme\THeadTheme;

class MyTheme extends BaseLaraGridTheme
{

    public static function make(): static
    {
        $theme = new static();

        $theme->setTableClass('min-w-full table-auto');

        $theme->setTheadTheme(
            THeadTheme::make()
                ->setTheadClass('pb-4')
                ->setThClass('pb-3 px-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap')
        );

        $theme->setTbodyTheme(
            TBodyTheme::make()
                ->setEmptyMessageClass('text-white')
                ->setTdClass('whitespace-nowrap p-3 text-sm text-gray-500')
                ->setGroupTdClass('whitespace-nowrap flex items-center p-3 text-sm text-gray-500')

                ->setRecordTrClass(fn($record) => $record->role === 'admin' ? 'bg-red-500' : 'bg-white'); // you can also set a closure for record tr class. Pass a closure that returns a string class.
                ->setRecordTrClass('bg-white odd:bg-gray-100'); // If you don't want to set a closure, you can just pass a string class.
        );

        $theme->setFilterTheme(
            FilterTheme::make()
                ->setFilterTextClass('bg-white w-full rounded-xl border border-gray-300')
                ->setFilterSelectClass('bg-white w-full rounded-xl border border-gray-300')
                ->setFilterDateClass('bg-white w-full rounded-xl border border-gray-300')
        );
        
        // those are not all methods, you can find all of them in BaseLaraGridTheme, THeadTheme, TBodyTheme and FilterTheme classes

        return $theme;
    }
    
}

Then, in your grid class, you need to override the getTheme method and return an instance of your theme class.

    protected function getTheme(): BaseLaraGridTheme
    {
        return MyTheme::make();
    }

By default, there is a theme called TailwindTheme.

Show filtering and sorting in url

If you want to show filtering and sorting in url, you need to rewrite default LaraGrid properties. You can do it like this:

abstract class MyBaseGrid extends BaseLaraGrid
{

    #[Url]
    public array $filter = [];

    #[Url(except: 'id')]
    public string $sortColumn = 'id';

    #[Url]
    public string $sortDirection = 'desc';

    protected function getFilterResetButton(): FilterResetButton
    {
        return FilterResetButton::make();
    }

    protected function getTheme(): BaseLaraGridTheme
    {
        return MyTheme::make();
    }

}

Those Url params are default from livewire, so you can customize them as you want by following livewire docs.

Contribution Guidelines

We welcome contributions to LaraGrid. If you'd like to contribute, please fork the repository, make your changes, and submit a pull request. We have a few requirements for contributions:

  • Follow the PSR-2 coding standard.
  • Write tests for new features and bug fixes.
  • Only use pull requests for contributions.

Changelog

For a detailed history of changes, see releases on GitHub.

License

This project is licensed under the MIT license.

Contact Information

For any questions or concerns, please feel free to create a discussion on GitHub.

Credits

Created by Matěj Černý from Bored Programmers.

Acknowledgments

We would like to thank all the contributors who have helped to make LaraGrid a better package.

bored-programmers/laragrid 适用场景与选型建议

bored-programmers/laragrid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 98 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「pagination」 「filter」 「sort」 「grid」 「laravel」 「table」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 bored-programmers/laragrid 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-30