From 14424980429c2824faf0442df59d709f75bae633 Mon Sep 17 00:00:00 2001 From: augushong Date: Wed, 27 Apr 2022 17:43:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A1=A8=E6=A0=BCfieldFormat?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=A1=B9=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/traits/Curd.php | 4 +- app/common/tools/ExcelTools.php | 12 +++- public/static/plugs/easy-admin/easy-admin.js | 58 +++++++++++++++++--- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index 11ebee3..c9fd569 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -120,12 +120,14 @@ trait Curd $fields = $this->request->param('fields', '{}', null); $image_fields = $this->request->param('image_fields', '{}', null); $select_fields = $this->request->param('select_fields', '{}', null); + $date_fields = $this->request->param('date_fields', '{}', null); $fields = json_decode($fields, true); $image_fields = json_decode($image_fields, true); $select_fields = json_decode($select_fields, true); + $date_fields = json_decode($date_fields, true); - $content = \app\common\tools\ExcelTools::exportModel($this->model, $where, $fields, $image_fields, $select_fields); + $content = \app\common\tools\ExcelTools::exportModel($this->model, $where, $fields, $image_fields, $select_fields, $date_fields); return download($content, $this->model->getName() . date('YmdHis') . '.xlsx', true); } diff --git a/app/common/tools/ExcelTools.php b/app/common/tools/ExcelTools.php index 1b7a623..3d77a5f 100644 --- a/app/common/tools/ExcelTools.php +++ b/app/common/tools/ExcelTools.php @@ -6,7 +6,7 @@ use PhpOffice\PhpSpreadsheet\Cell\DataType; class ExcelTools { - public static function exportModel($model, $where = [], $fields = [], $image_fields = [], $select_fields = []) + public static function exportModel($model, $where = [], $fields = [], $image_fields = [], $select_fields = [], $date_fields = []) { $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); @@ -24,7 +24,7 @@ class ExcelTools $runtime_file_list = []; $model - ->where($where)->chunk(100, function ($list) use ($sheet, &$write_line, $fields, $image_fields, &$runtime_file_list, $select_fields) { + ->where($where)->chunk(100, function ($list) use ($sheet, &$write_line, $fields, $image_fields, &$runtime_file_list, $select_fields, $date_fields) { foreach ($list as $list_index => $item) { $write_line++; $write_col = 1; @@ -68,11 +68,17 @@ class ExcelTools // 需要设置选项 $cel = $select_fields[$field_key][$value]; + } else if (in_array($field_key, $date_fields)) { + if (empty($value)) { + $cel = ''; + } else { + $cel = date('Y-m-d H:i:s', $value); + } } else { $cel = $value; } - $sheet->setCellValueExplicit($col_key . $write_line, $cel,DataType::TYPE_STRING); + $sheet->setCellValueExplicit($col_key . $write_line, $cel, DataType::TYPE_STRING); $write_col++; } } diff --git a/public/static/plugs/easy-admin/easy-admin.js b/public/static/plugs/easy-admin/easy-admin.js index eb44261..ec875e1 100644 --- a/public/static/plugs/easy-admin/easy-admin.js +++ b/public/static/plugs/easy-admin/easy-admin.js @@ -689,12 +689,33 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine } } + if (val.fieldFormat == undefined) { + + switch (val.templet) { + case admin.table.image: + val.fieldFormat = 'image'; + break; + case admin.table.date: + val.fieldFormat = 'date'; + break; + default: + val.fieldFormat = 'text'; + + if (val.selectList !== undefined) { + val.fieldFormat = 'select'; + } + break; + } + } + if (admin.checkMobile()) { if (val.fixed !== undefined) { val.fixed = false; } } + + } } return cols; @@ -1043,21 +1064,39 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine var selectFields = {}; + var dateFields = []; + options.cols[0].forEach(col => { if (col.field) { exportFields[col.field] = col.title; - if (col.templet == admin.table.image) { - imageFields.push(col.field) - } - - if (col.selectList instanceof Object) { - if (Object.keys(col.selectList).length > 0) { - selectFields[col.field] = col.selectList; + options.cols[0].forEach(col => { + if (col.field) { + + exportFields[col.field] = col.title; + + switch (col.fieldFormat) { + case 'image': + imageFields.push(col.field) + break; + + case 'select': + if (Object.keys(col.selectList).length > 0) { + selectFields[col.field] = col.selectList; + } + break; + case 'date': + + dateFields.push(col.field) + break; + + default: + break; + } + } - - } + }); } }); @@ -1088,6 +1127,7 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine fields: JSON.stringify(exportFields), image_fields: JSON.stringify(imageFields), select_fields: JSON.stringify(selectFields), + date_fields: JSON.stringify(dateFields), } var query = $.param(searchQuery);