Browse Source

树结构自动换行,行高计算相关

MaiXinRong 9 months ago
parent
commit
b925e539a0

+ 1 - 1
app/public/js/shares/select_rela_tender.js

@@ -8,7 +8,7 @@ class srObject {
         const srSpreadSetting = {
         const srSpreadSetting = {
             cols: [
             cols: [
                 {title: '选择', field: 'selected', hAlign: 1, width: 40, formatter: '@', cellType: 'checkbox'},
                 {title: '选择', field: 'selected', hAlign: 1, width: 40, formatter: '@', cellType: 'checkbox'},
-                {title: '名称', field: 'name', hAlign: 0, width: 400, formatter: '@', cellType: 'tree', wordWrap: true},
+                {title: '名称', field: 'name', hAlign: 0, width: 440, formatter: '@', cellType: 'tree', wordWrap: true},
                 {title: '期数', field: 'lastStageOrder', hAlign: 1, width: 60, formatter: '@'},
                 {title: '期数', field: 'lastStageOrder', hAlign: 1, width: 60, formatter: '@'},
                 {title: '审批状态', field: 'lastStageStatus', hAlign: 1, width: 60, formatter: '@'},
                 {title: '审批状态', field: 'lastStageStatus', hAlign: 1, width: 60, formatter: '@'},
             ],
             ],

+ 26 - 2
app/public/js/spreadjs_rela/spreadjs_zh.js

@@ -535,6 +535,26 @@ const SpreadJsObj = {
             cell.setBorder(sheet.borderLine, {all: true});
             cell.setBorder(sheet.borderLine, {all: true});
         });
         });
     },
     },
+    _autoFitRowWithTree: function(sheet, data, row, treeCol) {
+        const treeCell = sheet.getCell(row, treeCol);
+        const setting = sheet.zh_tree.setting;
+        const xOffset = 2 + 5;
+        const indent = 20;
+        const levelIndent = -5;
+        const move = (data[setting.level]) * indent + (data[setting.level]) * levelIndent + xOffset;
+        const validWidth = sheet.zh_setting.cols[treeCol].width - move;
+        const getTextHeight = function(sheet, str, font, width) {
+            const xs = sheet.getParent().xs;
+            const ctx = xs.childNodes[0].getContext("2d");
+            ctx.font = font;
+            const textMertrices = ctx.measureText(str);
+            const fontHeight = textMertrices.fontBoundingBoxAscent + textMertrices.fontBoundingBoxDescent;
+            const textRow = Math.ceil(textMertrices.width / width);
+            return textRow * fontHeight + 2 + (textRow - 1) * 1;
+        };
+        const treeCellHeight = getTextHeight(sheet, treeCell.text(), treeCell.font(), validWidth);
+        sheet.setRowHeight(row, Math.max(treeCellHeight, sheet.getRowHeight(row)));
+    },
     _loadRowData: function (sheet, data, row) {
     _loadRowData: function (sheet, data, row) {
         // 单元格重新写入数据
         // 单元格重新写入数据
         if (!data) { return }
         if (!data) { return }
@@ -542,8 +562,9 @@ const SpreadJsObj = {
             data.waitingLoading = true;
             data.waitingLoading = true;
             return;
             return;
         }
         }
-        let autoFit = false;
+        let autoFit = false, treeWordWrapCol = -1;
         sheet.zh_setting.cols.forEach(function (col, j) {
         sheet.zh_setting.cols.forEach(function (col, j) {
+            if (col.cellType === 'tree' && col.wordWrap) treeWordWrapCol = j;
             const cell = sheet.getCell(row, j);
             const cell = sheet.getCell(row, j);
             if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
             if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
                 cell.value(col.getValue(data, col));
                 cell.value(col.getValue(data, col));
@@ -595,7 +616,10 @@ const SpreadJsObj = {
             }
             }
             data.waitingLoading = false;
             data.waitingLoading = false;
         });
         });
-        if (autoFit) sheet.autoFitRow(row);
+        if (autoFit) {
+            sheet.autoFitRow(row);
+            if (treeWordWrapCol >= 0) this._autoFitRowWithTree(sheet, data, row, treeWordWrapCol);
+        }
     },
     },
     _addActivePaintEvents: function (sheet, cellType) {
     _addActivePaintEvents: function (sheet, cellType) {
         if (!sheet.ActiveType) {
         if (!sheet.ActiveType) {