由於Dcat admin暫不支援 maatwebsite/excel3.1 對原本的匯出元件進行自定義,因此使用$grid->tool()自定義按鈕做匯出。
Dcat admin使用maatwebsite/excel2.1進行自定義匯出文件:資料匯出《Dcat Admin 中文文件》
1、使用maatwebsite/excel3.1完成匯出介面,用於 TestExport
工具類
<?php
namespace App\Admin\Controllers;
use App\Admin\Extensions\TestExpoter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
class TestController extends Controller
{
/**
* 匯出
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function export(Request $request)
{
$filename = $request->get('filename');
$param = json_decode($request->get('param'));
ob_end_clean();
return Excel::download(new TestExpoter($param),$filename . '.xlsx');
}
}
2、TestExpoter
類返回匯出資料
<?php
namespace App\Admin\Extensions;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class TestExpoter implements FromCollection,WithHeadings
{
private $row;
private $data;
private $headings;
/**
* TaskDataExcelExpoter constructor.
* @param array $param 篩選條件
* @param array $data 資料
* @param array $headings 表頭
*/
public function __construct($param = [], $data = [], $headings = [])
{
//表頭設定
$headings = [[
'id' => 'ID',
...
]];
$data = $this->getData($param);
$this->headings = $headings;
$this->data = $data;
}
public function headings(): array
{
return $this->headings;
}
public function collection()
{
return collect($this->data);
}
/**
* 獲取匯出資料
*
* @param $param
* @return mixed
*/
private function getData($param)
{
$param = json_decode(json_encode($param), true);
$note_monitor = DB::table('table_name')->get()->toArray();
$list = ['處理後的資料'];
return $list;
}
}
3、定義路由
Route::get('/export', 'TestController@export');
4、建立 TestExport
類繼承 Dcat\Admin\Grid\Tools\AbstractTool
類
<?php
namespace App\Admin\Actions\Grid;
use Dcat\Admin\Grid\Tools\AbstractTool;
use Illuminate\Http\Request;
class TestExport extends AbstractTool
{
/**
* @return string
*/
protected $title = '匯出當前頁';
protected $request_param = [];
protected $request_filename = '';
/**
* 接收引數
*/
public function __construct($param = null, $filename = null, $title)
{
$this->request_param = $param;
$this->request_filename = $filename;
parent::__construct($title);
$this->title = $title;
}
/**
* 按鈕樣式定義,預設 btn btn-white waves-effect
*
* @var string
*/
protected $style = 'btn btn-outline-info';
/**
* 按鈕文字
*
* @return string|void
*/
public function title()
{
return $this->title;
}
/**
* 處理請求
* 如果你的類中包含了此方法,則點選按鈕後會自動向後端發起ajax請求,並且會透過此方法處理請求邏輯
*
* @param Request $request
*/
public function handle(Request $request)
{
$param = $request->get('param');
$filename = $request->get('filename');
// 呼叫/export介面進行匯出
return $this->response()->download('/export?filename=' . $filename . '¶m=' . json_encode($param) . '&_export_=1');
}
/**
* 設定請求引數
*
* @return array|void
*/
public function parameters()
{
return [
'param' => $this->request_param,
'filename' => $this->request_filename,
'title' => $this->title,
];
}
}
5、使用TestRepository
類展示列表
<?php
namespace App\Admin\Controllers;
use App\Admin\Repositories\TestRepository;
use Dcat\Admin\Grid;
use \Dcat\Admin\Http\Controllers\AdminController;
class TestController extends AdminController
{
protected function grid()
{
return Grid::make(new TestRepository(), function (Grid $grid) {
// 第一列顯示id欄位,並將這一列設定為可排序列
$grid->column('id', 'ID')->sortable();
...
// 自定義工具
$grid->tools(function (Grid\Tools $tools) use ($grid){
// 獲取當前頁數
$currentPage = $grid->model()->getCurrentPage();
// 獲取每頁顯示行數
$perPage = $grid->model()->getPerPage();
$start = ($currentPage - 1) * $perPage;
// 獲取排序引數
$sort = $grid->model()->getSort();
// 獲取篩選條件
$id = $grid->filter()->input('id'),
// 獲取規格選擇器條件
$gender = $grid->filter()->input('_selector.gender'),
$param = [
'sort' => $sort,
'search' => ['id' => $id],
'selector' => ['gender' => $gender]
];
// 匯出
$tools->append(new TestExport($param, $this->title, '匯出當前頁'));
});
// 篩選
$grid->filter(function ($filter) {
// 設定id欄位的範圍查詢
$filter->equal('id', 'ID');
...
});
// 規格選擇器
$grid->selector(function (Grid\Tools\Selector $selector) {
//性別
$selector->select('gender', '性別', [0 => '男', 1 => '女', '' => '未標明']);
});
});
}
}
博文轉載:blog.csdn.net/weixin_43870916/arti...
本作品採用《CC 協議》,轉載必須註明作者和本文連結