From 483b901cc4d6a1aec63f82e07089e25932cf458d Mon Sep 17 00:00:00 2001 From: Karson Date: Mon, 25 Dec 2017 12:21:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AF=BC=E5=85=A5=E6=97=B6?= =?UTF-8?q?=E5=9C=A8=E8=A1=A8=E5=AD=97=E6=AE=B5=E8=B6=85=E8=BF=8726?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E9=94=99=E8=AF=AF=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=AF=BC=E5=85=A5Excel=E6=96=87=E4=BB=B6=E9=A6=96=E8=A1=8C?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=85=8D=E7=BD=AE,=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=88=96=E8=A1=A8=E5=AD=97=E6=AE=B5=E5=90=8D?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E8=A1=A8=E6=A0=BC=E5=9C=A8=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=B8=BAnull=E5=80=BC=E6=97=B6=E7=9A=84=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/library/traits/Backend.php | 25 +++++++++++++------- application/common/controller/Backend.php | 7 ++++++ public/assets/js/require-backend.min.js | 15 ++++++++---- public/assets/js/require-table.js | 11 +++++---- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/application/admin/library/traits/Backend.php b/application/admin/library/traits/Backend.php index 2c89d442..fbf60115 100644 --- a/application/admin/library/traits/Backend.php +++ b/application/admin/library/traits/Backend.php @@ -329,25 +329,35 @@ trait Backend } } + //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name + $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment'; + $table = $this->model->getQuery()->getTable(); $database = \think\Config::get('database.database'); $fieldArr = []; $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]); foreach ($list as $k => $v) { - $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME']; + if ($importHeadType == 'comment') + { + $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME']; + } + else + { + $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME']; + } } $PHPExcel = $PHPReader->load($filePath); //加载文件 $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 - $allColumn = $currentSheet->getHighestColumn(); //取得最大的列号 + $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号 $allRow = $currentSheet->getHighestRow(); //取得一共有多少行 - + $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn); for ($currentRow = 1; $currentRow <= 1; $currentRow++) { - for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) + for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) { - $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); + $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $fields[] = $val; } } @@ -355,11 +365,10 @@ trait Backend for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { $values = []; - for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) + for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) { - $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); /* * ord()将字符转为十进制数 */ + $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue(); $values[] = is_null($val) ? '' : $val; - //echo iconv('utf-8','gb2312', $val)."\t"; } $row = []; $temp = array_combine($fields, $values); diff --git a/application/common/controller/Backend.php b/application/common/controller/Backend.php index 6533529c..fe52d733 100644 --- a/application/common/controller/Backend.php +++ b/application/common/controller/Backend.php @@ -77,6 +77,13 @@ class Backend extends Controller */ protected $multiFields = 'status'; + /** + * 导入文件首行类型 + * 支持comment/name + * 表示注释或字段名 + */ + protected $importHeadType = 'comment'; + /** * 引入后台控制器的traits */ diff --git a/public/assets/js/require-backend.min.js b/public/assets/js/require-backend.min.js index 8679e87b..47514b26 100644 --- a/public/assets/js/require-backend.min.js +++ b/public/assets/js/require-backend.min.js @@ -9942,9 +9942,9 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr return ''; }, images: function (value, row, index) { - value = value.toString(); + value = value === null ? '' : value.toString(); var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center'; - var arr = value.toString().split(','); + var arr = value.split(','); var html = []; $.each(arr, function (i, value) { value = value ? value : '/assets/img/blank.gif'; @@ -9959,7 +9959,7 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr if (typeof this.custom !== 'undefined') { colorArr = $.extend(colorArr, this.custom); } - value = value.toString(); + value = value === null ? '' : value.toString(); var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary'; value = value.charAt(0).toUpperCase() + value.slice(1); //渲染状态 @@ -9983,6 +9983,7 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr return '' + value + ''; }, flag: function (value, row, index) { + value = value === null ? '' : value.toString(); var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'}; //如果字段列有定义custom if (typeof this.custom !== 'undefined') { @@ -9993,9 +9994,9 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr } //渲染Flag var html = []; - var arr = value.toString().split(','); + var arr = value.split(','); $.each(arr, function (i, value) { - value = value.toString(); + value = value === null ? '' : value.toString(); if (value == '') return true; var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary'; @@ -14394,6 +14395,10 @@ define("addtabs", function(){}); if ($(elem.combo_input).attr('placeholder')) $(elem.combo_input).attr('placeholder_bak', $(elem.combo_input).attr('placeholder')); } + + if ($(elem.container).parent().hasClass("input-group")) { + $(elem.container).height($(elem.container).parent().height()); + } this.elem = elem; }; diff --git a/public/assets/js/require-table.js b/public/assets/js/require-table.js index 5b5b23b3..251f7444 100644 --- a/public/assets/js/require-table.js +++ b/public/assets/js/require-table.js @@ -341,9 +341,9 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table return ''; }, images: function (value, row, index) { - value = value.toString(); + value = value === null ? '' : value.toString(); var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center'; - var arr = value.toString().split(','); + var arr = value.split(','); var html = []; $.each(arr, function (i, value) { value = value ? value : '/assets/img/blank.gif'; @@ -358,7 +358,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table if (typeof this.custom !== 'undefined') { colorArr = $.extend(colorArr, this.custom); } - value = value.toString(); + value = value === null ? '' : value.toString(); var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary'; value = value.charAt(0).toUpperCase() + value.slice(1); //渲染状态 @@ -382,6 +382,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table return '' + value + ''; }, flag: function (value, row, index) { + value = value === null ? '' : value.toString(); var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'}; //如果字段列有定义custom if (typeof this.custom !== 'undefined') { @@ -392,9 +393,9 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table } //渲染Flag var html = []; - var arr = value.toString().split(','); + var arr = value.split(','); $.each(arr, function (i, value) { - value = value.toString(); + value = value === null ? '' : value.toString(); if (value == '') return true; var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';