gdgrid / gd
PHP 7 网格数据库
dev-master
2020-01-07 10:42 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- illuminate/database: 5.7
- illuminate/filesystem: 5.7
- illuminate/translation: 5.7
- illuminate/validation: 5.7
- seytar/php-router: dev-master
- symfony/var-dumper: ^4.1
This package is auto-updated.
Last update: 2024-09-07 20:59:27 UTC
README
PHP 7 网格数据库。
库的主要目的是在视图中自动生成表格、表单以及某些实体的表示。如果实体的表单和列字段设置未指定,则这些设置将从数据库表中的列类型及其名称中获取。
为此,您需要在实体本身中实现一个特定的接口,或者连接一个实现该接口的独立类,并将其传递给生成器。
安装
composer require gdgrid/gd
如果您想运行测试应用程序(包含在库的 "testapp" 文件夹中),因此您将需要从 composer.json 中安装所有必要的依赖项
{
"require": {
"gdgrid/gd": "dev-master"
},
"require-dev": {
"illuminate/database": "5.7",
"illuminate/filesystem": "5.7",
"illuminate/translation": "5.7",
"illuminate/validation": "5.7",
"symfony/var-dumper": "^4.1",
"seytar/php-router": "dev-master"
},
"autoload": {
"classmap": [
"testapp/models/"
]
}
}
使用示例
1. 使用实体本身。
您的模型类
<?php use Illuminate\Database\Eloquent\Model as Eloquent; use gdgrid\gd\IGridFormProvider; use gdgrid\gd\IGridTableProvider; class User extends Eloquent implements IGridFormProvider, IGridTableProvider { protected $fillable = ['name', 'image', 'email', 'gender', 'character']; public $timestamps = false; protected $errors = []; ... public function gridFields(): array { return [ 'name' => 'Hero', 'email' => 'Email', 'image' => 'Photo', 'character' => 'Description', 'gender' => 'Gender', ]; } public function gridInputTypes(): array { return [ 'name' => 'text', 'email' => 'email', 'image' => 'text', 'character' => 'textarea', 'gender' => 'radio', ]; } public function gridInputOptions(): array { return [ 'gender' => ['Female', 'Male'], ]; } public function gridInputSizes(): array { return [ 'name' => 100, 'email' => 100, 'image' => 255, 'character' => 1000, ]; } public function gridSafeFields(): array { return ['id']; } public function gridInputErrors(): array { return $this->errors; } public function gridTableCellPrompts() { return '(no data)'; } public function gridInputPrompts(): array { return []; } }
视图文件
<?php use gdgrid\gd\GridTable; use gdgrid\gd\GridForm; $provider = new User; $items = $provider->filter(Request::capture()->all())->get()->all(); $table = (new GridTable($provider))->loadColumns(); $table->plugin()->setConfig('bulk-actions', ['view' => false, 'set_query' => false]); $table->plugin()->hook('filter', function(GridForm $plugin) { $plugin->loadInputs()->setValues(Request::capture()->all()); }); $table->disableEmbedPlugin('pagination'); $table->setProviderItems($items)->setCell('image', function($data) { return $data->image ? '<img src="' . $data->image . '" />' : null; }); echo $table->render();
2. 使用数据提供者。
在这种情况下,您不需要在实体类中实现接口。
您的模型类
<?php use Illuminate\Database\Eloquent\Model as Eloquent; class User extends Eloquent { protected $fillable = ['name', 'image', 'email', 'gender', 'character']; public $timestamps = false; protected $errors = []; ... }
视图文件
<?php use gdgrid\gd\bundle\Grid as BundleGrid; use gdgrid\gd\Grid; use gdgrid\gd\GridData; use gdgrid\gd\GridDataProvider; use gdgrid\gd\GridForm; use gdgrid\gd\GridTable; use Illuminate\Http\Request; $provider = new User; # The "isStoreOutdated" method checks if the current dataProvider`s instance is outdated in the BundleGrid`s cache: $items = BundleGrid::capture()->isStoreOutdated('someStoreKey') ? $provider->filter(Request::capture()->all())->get()->all() : []; $dataProvider = new GridDataProvider($provider); $dataProvider->setDataProvider((new GridData) ->setPdo(DB::capsule()->getConnection()->getPdo()) ->setTable('users') ->setLocale('en')); $dataProvider->fetchData(); $dataProvider->mergeData([ 'safeFields' => [ 'id', ], 'inputOptions' => [ 'gender' => ['Female', 'Male'] ] ]); $table = (new GridTable($dataProvider))->loadColumns(); if (sizeof($items)) $table->setProviderItems($items); # Use of the Bundle Grid simplifies all initializations produced above in a single line: // $table = BundleGrid::capture() # method "capture" for create/access the GridBundle`s singleton. // ->store('someStoreKey') # method "store" (optional) for serialization/access the current GridBundle instance. // ->setProvider($provider) // ->fetchData(DB::capsule()->getConnection()->getPdo(), 'users') // ->mergeData([ // 'inputOptions' => [ // 'gender' => ['FEMALE', 'MALE'] // ] // ])->table(); # Serialize changes in the current BundleGrid`s instance # (The methods "store/restore" brings ability for further access the dataProvider`s instance from BundleGrid`s cache): // if (BundleGrid::capture()->isStoreOutdated('someStoreKey')) BundleGrid::capture()->restore('someStoreKey', 3600); $table->plugin()->setConfig('bulk-actions', ['view' => false, 'set_query' => false]); $table->plugin()->hook('filter', function(GridForm $plugin, Grid $grid) { $plugin->loadInputs()->setValues(Request::capture()->all()); }); # Can Disable the Embedded Plugins: // $table->disableEmbedPlugins(); # Pagination disabled. To enable it, you must specify quantity of records # in the "totalCount" configuration parameter: // $table->plugin()->setConfig('pagination', ['totalCount' => ???]); $table->disableEmbedPlugin('pagination'); # Can Format the values in the data table cells: // $table->setFormatAll(['truncate' => 5]); // $table->formatter()->mergeFormats([['strtoupper', []]]); // $table->setFormat([ // [['name', 'email'], ['trim', 'strip_tags']], // ['character', ['strip_html']], // ]); $table->setCell('image', function($data) { return $data->image ? '<img src="' . $data->image . '" />' : null; }); echo $table->render();
您可以在库的 "testapp" 目录中找到表示示例的完整代码。只需将文件复制/粘贴到应用程序的根目录即可。