fazzinipierluigi/laraexpress_datasource

一个用于为 DevExpress 前端库生成数据源的 Laravel 库

1.2.6 2024-09-16 15:07 UTC

This package is auto-updated.

Last update: 2024-09-16 15:08:07 UTC


README

此库提供了一个辅助工具,用于生成 DevExpress JavaScript 库中小部件的数据源。

进步

  • 加载
    • 过滤器
    • 排序
    • 分页
    • 分组
  • 插入
  • 更新
  • 删除

用法

在您的控制器方法内部

public function index(Request $request)
{
	if($request->ajax())
	{
		$users = \App\Model\User::where('is_admin', '=', 0);
		
		$data_source = new EloquentSource(); // Remember that you have to add the "use" directive first.
		$data_source->apply($users, $request);
		return $data_source->getResponse();
	}
	else
	{
		return view('user.index')
	}
}

您可以将前端时区传递给构造函数(默认为 'Euurope/Rome'),或者您可以使用 setTimezone 方法稍后设置它

$my_data_source = new EloquentSource(); // This has as its timezone "Europe/Rome"

// These two options are equivalent
$data_source = new EloquentSource("America/Chicago");
$data_source->setTimezone("America/Chicago");

在前端中,我们首先包括数据存储的库(请参阅文档),在我的情况下

<script src="https://unpkg.com/devextreme-aspnet-data/js/dx.aspnet.data.js"></script>

之后

$("#dataGridContainer").dxDataGrid({
    dataSource: DevExpress.data.AspNet.createStore({
        loadUrl: '{{ URL::current() }}',
    }),
    remoteOperations: {
		filtering: true,
		sorting: true,
		paging: true,
		grouping: true,
		groupPaging: true,
	},
    // ...
})

apply 方法接受一个可选的第三个参数,即 "field_map",该参数用于在生成响应时字段被重命名或在多个数据库列对应于数据网格列时映射数据库字段

public function index(Request $request)
{
	if($request->ajax())
	{
		$users = \App\Model\User::select('users.*');
		
		$data_source = new EloquentSource();
		$field_map = [ // I create the array that contains the mapping field name => column name
			'fiscal_reference' => ['users.tax_code', 'users.vat'],
			'creation_date' => 'created_at'
		];
		$data_source->apply($users, $request, $field_map);
		
		return $data_source->getResponse(function($data) {
			// Optionally you can pass a Clojure function
			// to the "getResponse" function to format the
			// data or perform operations. If it is not
			// provided the toArray() method will be used.
			return [
				'username' => $data->username;
				'fiscal_reference' => ($data->is_company())? $data->vat : $data->tax_code;
				'creation_date' => $data->created_at->format('d/m/Y');
				// ... and so on
			];
		});
	}
	else
	{
		return view('user.index')
	}
}

apply 方法还接受一个可选的第四个参数;这用于覆盖排序行为

public function index(Request $request)
{
	if($request->ajax())
	{
		$contracts = \App\Model\Contracts::select('contracts.*')
										 ->join('customers', 'customers.id', '=', 'contracts.customer_id');
		
		$data_source = new EloquentSource();
		$field_map = [ // I create the array that contains the mapping field name => column name
			'customer' => 'customers.id'
		];
		$field_sorting = [
			'customer' => 'customers.company_name'
		];
		$data_source->apply($contracts, $request, $field_map, $field_sorting);
		
		return $data_source->getResponse(function($data) {
			// ....
		});
	}
	else
	{
		return view('customer.index')
	}
}

在这种情况下,如果我们只传递 "field_map" 数组给 apply,排序将基于客户 ID 进行,而通过覆盖它,我们可以强制按公司名称进行排序,同时仍然保持按 ID 进行搜索。