承接 distilleries/datatable-builder 相关项目开发

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

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

distilleries/datatable-builder

Composer 安装命令:

composer require distilleries/datatable-builder

包简介

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

README 文档

README

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version License

#Laravel 5 Datatable Builder

Based on chumper/datatable. It's an abstract class to implement the datatable like the form generator.

Table of contents

  1. Installation
  2. Basic usage
  3. Closure
  4. BaseQuery
  5. Filters
  6. Styles
  7. Controller

##Installation

Add on your composer.json

    "require": {
        "distilleries/datatable-builder": "1.*",
    }

run composer update.

Add Service provider to config/app.php:

    'providers' => [
        // ...
	   'Distilleries\FormBuilder\FormBuilderServiceProvider',
       'Distilleries\DatatableBuilder\DatatableBuilderServiceProvider',
    ]

And Facade (also in config/app.php)

    'aliases' => [
        // ...
		'FormBuilder'     => 'Distilleries\FormBuilder\Facades\FormBuilder',
        'Datatable'       => 'Distilleries\DatatableBuilder\Facades\DatatableBuilder',
    ]

Add the javascript on your bower dependencies:

    "dependencies": {
        "datatables": "~1.10.4",
    }

Export the configuration:

php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider"

Export the views (optional):

php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider"  --tag="views"

Export the Javascript assets (optional):

Include the javascript with gulp or grunt vendor/distilleries/datatable-builder/resources/**/*. If you don't use a task manager or you want override the javascript just publish those with the command line:

php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider"  --tag="assets"

You can find those in resources/assets/vendor/datatable-builder.

Basic usage

Creating form classes is easy. With a simple artisan command I can create a datatable:

php artisan datatable:make Datatables/PostDatatable

you create form class in path app/Datatables/PostDatatable.php that looks like this:

<?php namespace App\Datatables;

use Distilleries\DatatableBuilder\EloquentDatatable;

class PostDatatable extends EloquentDatatable
{
    public function build()
    {
        // Add fields here...

        $this->addDefaultAction();

    }
}

You can add fields which you want when creating command like this:

php artisan datatable:make Datatables/SongDatatable --fields="name, lyrics"

And that will create a datatable in path app/Datatables/SongDatatable.php with content:

<?php namespace App\Datatables;

use Distilleries\DatatableBuilder\EloquentDatatable;

class SongDatatable extends EloquentDatatable
{
    public function build()
    {
        $this
            ->add('name',null,_('Name'))
            ->add('lyrics',null,_('Lyrics'));

        $this->addDefaultAction();

    }
}

The method add have in param:

add($name, $closure = null, $translation = '', $orderable = true)

Param Usage
name Name of the column
closure Function with the model in parameter to return a template. By default null use the attribute of the model.
translation Translation of the column, by default empty use the column name
orderable Flag to handle column orderable state, by default true

Closure

When you create a datatatable sometime the content need to be get from other table or stylize by a template like the actions buttons.

For example an address is link to a profile. To display the profile name on the datatable like the picture you can use the closure.

    $this->add('profile_name', function ($model)
    {
        $profile = $model->profile;
   
        return (empty($profile)?'':$profile->first_name . ' ' . $profile->last_name);
    }, _('Profile'));
    

On my model Address I have:

    public function profile()
    {
         return $this->belongsTo('Profile');
    }

datatable

You can return a template if you want:

    $this->add('html_row', function ($model)
    {
        return View::make('admin.address.html_row',[
        ])->render();
    });

BaseQuery

You can override the base query for the datatable query.

By default it will send a fresh full Query: $this->model->newModelQuery();

    /**
     * {@inheritdoc}
     */
    protected function baseQuery()
    {
        return $this->model->newModelQuery()
            ->selectRaw("id, data->>'$.title' AS title, data->>'$.chapo' AS intro, created_at");
    }

Filters

You can use complex filter to filter your datatable. To do that I use the library FormBuilder. All the datatable have a plain form filter. If you had field on this form that display the filters.

For example we want all the user only online.

I create a choice field:

public function filters()
{
    $this->form->add('status', 'choice', [
        'choices'     => StaticLabel::status(),
        'empty_value' => _('-'),
        'validation'  => 'required',
        'label'       => _('Status')
    ]);

}

filters

When the filters are submitted you can apply it with the method applyFilters. By default this method create a where with the name of the filter field if it's an attribute of the model. If you want change this behaviour you can override it.

    public function applyFilters()
    {
        $allInput = Input::all();
        $columns  = \Schema::getColumnListing($this->model->getTable());

        foreach ($allInput as $name => $input)
        {
            if (in_array($name, $columns) and $input != '')
            {

                $this->model = $this->model->where($name, '=', $input);
            }
        }

    }

If you don't want create a global scope of your model you just want restrict the display for this datatable. you can use applyFilters to do that.

For example I want display only the customer of the application: The datable work on the user model.

    public function applyFilters()
    {
        parent::applyFilters();
        $customer = \Role::where('initials', '==', '@c')->get()->last();
        $this->model = $this->model->where('role_id', '=', $customer->id);
    }

Styles

You want stylize your row to put it on green, blue or red. You can do that with setClassRow. By default this method check the status attribute. If the status exist and it is empty we add the danger class to display it in red.

    public function setClassRow($datatable)
    {
        $datatable->setRowClass(function ($row)
        {
            return (isset($row->status) and empty($row->status)) ? 'danger' : '';
        });

        return $datatable;
    }

##Controller You can use the trait Distilleries\DatatableBuilder\States\DatatableStateTrait to add in your controller the default methods use with the datatable.

Example: I created a controller app/Http/Controllers/DatatableController:

<?php namespace App\Http\Controllers;

use App\Datatables\UserDatatable;

class DatatableController extends Controller {

	use \Distilleries\DatatableBuilder\States\DatatableStateTrait;
	/*
	|--------------------------------------------------------------------------
	| Welcome Controller
	|--------------------------------------------------------------------------
	|
	| This controller renders the "marketing page" for the application and
	| is configured to only allow guests. Like most of the other sample
	| controllers, you are free to modify or remove it as you desire.
	|
	*/

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct(\App\User $model, UserDatatable $datatable)
	{
		$this->model = $model;
		$this->datatable = $datatable;
	}

	/**
	 * Show the application welcome screen to the user.
	 *
	 * @return Response
	 */
	public function getIndex()
	{
		return view('welcome',[
			'datatable'=>$this->getIndexDatatable()
		]);
	}

}

I add the controller on the route file :

Route::controllers([
	'datatable' => 'DatatableController'
]);

Like you can see I inject the model and the datatable on the constructor. On the welcome template I put:

<html>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
		<script src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>
		<!-- Latest compiled and minified CSS -->
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

		<!-- Optional theme -->
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
		<link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css">

		<!-- Latest compiled and minified JavaScript -->
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
		<script src="/vendor/datatable-builder/js/datatable.js"></script>
	</head>
	<body>
		<div class="container">
			{!! $datatable !!}
		</div>
	</body>
</html>

That it you have your datatable from the user model.

distilleries/datatable-builder 适用场景与选型建议

distilleries/datatable-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.6k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2015 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 distilleries/datatable-builder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 9.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 3
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 7
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-11