From 483b901cc4d6a1aec63f82e07089e25932cf458d Mon Sep 17 00:00:00 2001
From: Karson <karsonzhang@163.com>
Date: Mon, 25 Dec 2017 12:21:40 +0800
Subject: [PATCH] 修复导入时在表字段超过26时的错误 新增导入Excel文件首行类型配置,默认注释或表字段名 修复表格在字段为null值时的报错

---
 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 2c89d44..fbf6011 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 6533529..fe52d73 100644
--- a/application/common/controller/Backend.php
+++ b/application/common/controller/Backend.php
@@ -78,6 +78,13 @@ class Backend extends Controller
     protected $multiFields = 'status';
 
     /**
+     * 导入文件首行类型
+     * 支持comment/name
+     * 表示注释或字段名
+     */
+    protected $importHeadType = 'comment';
+
+    /**
      * 引入后台控制器的traits
      */
     use \app\admin\library\traits\Backend;
diff --git a/public/assets/js/require-backend.min.js b/public/assets/js/require-backend.min.js
index 8679e87..47514b2 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 '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />';
                 },
                 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 '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
                 },
                 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 5b5b23b..251f744 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 '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />';
                 },
                 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 '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
                 },
                 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';
--
libgit2 0.24.0