定制 uestla/twigrid 二次开发

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

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

uestla/twigrid

Composer 安装命令:

composer require uestla/twigrid

包简介

Experimental DataGrid for Nette Framework

README 文档

README

... is a DataGrid for Nette Framework.

Buy me a Coffee

Demo: https://kesspess.cz/twigrid/
Demo sources: https://github.com/uestla/twigrid-demo

It's based on another (and great) datagrid written by @hrach - https://github.com/nextras/datagrid. My datagrid is hugely inspired by this component and its programming ideas and therefore I would like to give this man a maximum credit and respect :-)

Quickstart

Let's see how many steps do we have to make to create our first datagrid.

  1. Create new project

    composer create-project nette/web-project twigrid-quickstart
  2. Install TwiGrid & client-side assets

    cd twigrid-quickstart
    composer require uestla/twigrid
    yarn add twigrid-datagrid --modules-folder www/assets/vendor

    If you're not using yarn, you can install assets manually by looking into package.json and see required dependencies there.

    We'll then update app/Presentation/@layout.latte to load downloaded assets - just replace {asset? 'main.js'} with:

    <!-- app/Presentation/@layout.latte -->
    
    {asset 'vendor/bootstrap/dist/css/bootstrap.min.css'}
    {asset 'vendor/twigrid-datagrid/assets/twigrid.datagrid.css'}
    
    {asset 'vendor/jquery/dist/jquery.min.js'}
    {asset 'vendor/bootstrap/dist/js/bootstrap.min.js'}
    {asset 'vendor/nette-forms/src/assets/netteForms.min.js'}
    {asset 'vendor/nette.ajax.js/nette.ajax.js'}
    {asset 'vendor/twigrid-datagrid/assets/twigrid.datagrid.js'}
    
    {asset 'script.js'}

    Then we'll create www/assets/script.js with Nette Forms and nette.ajax initialization:

    Nette.initOnLoad();
    
    $(function () {
    	$.nette.init();
    });
  3. Database

    Download the SQLite3 file from the demo application and place it in app/Model/users.s3db.

    And we'll configure this database to be used by the application:

    # config/common.neon
    database:
    	dsn: 'sqlite:%appDir%/Model/users.s3db'
  4. Create datagrid

    Now it's finally time to create our first datagrid - let's create an app/Grids/UsersGrid.php file. We'll need database connection for data loading, so we inject it properly via constructor.

    // app/Grids/UsersGrid.php
    
    /** @implements TwiGrid\DataGrid<Nette\Database\Table\ActiveRow> */
    final class UsersGrid extends TwiGrid\DataGrid
    {
    	public function __construct(
    		private readonly Nette\Database\Explorer $database,
    	) {
    		parent::__construct();
    	}
    
    	protected function build(): void
    	{
    		// TODO
    	}
    }

    We'll define the datagrid body inside the build() method. Although the table user has many columns, we'll have just some of them in our grid just to make it easy.

    // app/Grids/UsersGrid.php
    
    /** @implements TwiGrid\DataGrid<Nette\Database\Table\ActiveRow> */
    final class UsersGrid extends TwiGrid\DataGrid
    {
    	// ...
    
    	protected function build(): void
    	{
    		$this->addColumn('firstname', 'Firstname');
    		$this->addColumn('surname', 'Surname');
    		$this->addColumn('streetaddress', 'Street address');
    		$this->addColumn('city', 'City');
    		$this->addColumn('country_code', 'Country');
    	}
    }

    TwiGrid also needs to know what column(s) it should consider as a primary key:

    $this->setPrimaryKey('id');

    And finally we'll tell TwiGrid how to load our users:

    $this->setDataLoader(fn() => $this->database->table('user'));
  5. Factory

    To properly inject our grid into presenters, we'll need to create a factory interface:

    // app/Grids/UsersGridFactory.php
    
    interface UsersGridFactory
    {
    	public function create(): UsersGrid;
    }

    This interface will now be used for automatic factory generation and autowired thanks to SearchExtension, which is handy.

  6. Presenter

    Having all of this done, we can now simply inject our grid factory into HomePresenter.

    // app/Presentation/Home/HomePresenter.php
    
    final class HomePresenter extends Nette\Application\UI\Presenter
    {
    	public function __construct(
    		private readonly \UsersGridFactory $usersGridFactory,
    	) {
    		parent::__construct();
    	}
    }

    Now we'll add the control factory itself:

    // app/Presentation/Home/HomePresenter.php
    
    protected function createComponentUsersGrid(): \UsersGrid
    {
    	return $this->usersGridFactory->create();
    }
  7. Render the grid

    We're nearly done! Just open app/Presentation/Home/default.latte and replace the whole content with

    {block content}
    	<div class="container">
    		<h1>UsersGrid example</h1>
    
    		{control usersGrid}
    	</div>
    {/block}
  8. Custom template

    Maybe showing the country code isn't that sexy - we'd like to have the whole country name in "Country" column. To achieve that, we'll create custom grid template:

    {* app/Grids/UsersGrid.latte *}
    
    {extends $defaultTemplate}
    
    {define body-cell-country_code}
    	<td>{$record->country->title}</td>
    {/define}

    And tell TwiGrid to use this template:

    // app/Grids/UsersGrid.php::build()
    
    $this->setTemplateFile(__DIR__ . '/UsersGrid.latte');

    That's all, folks!

    Now when you'll open the page, you might see something like this:

    Result screenshot

More

To see more examples, please visit the demo page. Enjoy!

uestla/twigrid 适用场景与选型建议

uestla/twigrid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.35k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2013 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 21
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 4
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-01-02