找回密码
 立即注册

QQ登录

只需一步,快速开始

Ellia.Duan SpreadJS 开发认证

超级版主

68

主题

4680

帖子

7242

积分

超级版主

Rank: 8Rank: 8

积分
7242

SpreadJS 认证SpreadJS 高级认证

Ellia.Duan SpreadJS 开发认证
超级版主   /  发表于:2024-9-2 10:31  /   查看:70  /  回复:0
本帖最后由 Ellia.Duan 于 2024-9-2 10:56 编辑

在SpreadJS中,下拉框是支持多选的,具体可以参考这篇学习指南。但是在一些场景中,想标识不同选项,如
image.png990332031.png

下面,我们看下怎么实现吧:
1、定义Style
在上面给出的学习指南中,介绍了在Style中定义cellButtons以及dropDowns属性,分别用来控制打开下拉框选项以及下拉框选项。
基本用法是:
  1. var style = new GC.Spread.Sheets.Style();
  2. style.cellButtons = [
  3.     {
  4.         imageType: GC.Spread.Sheets.ButtonImageType.dropdown,
  5.         command: "openList",
  6.         useButtonStyle: true,
  7.     }
  8. ];
  9. style.dropDowns = [
  10.     {
  11.         type: GC.Spread.Sheets.DropDownType.list,
  12.         option: {
  13.             items: [
  14.                 {
  15.                     text: 'item1',
  16.                     value: 'item1'
  17.                 },
  18.                 {
  19.                     text: 'item2',
  20.                     value: 'item2'
  21.                 },
  22.                 {
  23.                     text: 'item3',
  24.                     value: 'item3'
  25.                 },
  26.                 {
  27.                     text: 'item4',
  28.                     value: 'item4'
  29.                 }
  30.             ],
  31.         }
  32.     }
  33. ];
  34. sheet.setText(2, 1, "Vertical text list");
  35. sheet.setStyle(3, 1, style);
复制代码
但是,我们在点击选项的时候,要增加一个背景色。所以,这里dropDowns 中的option我们自定义一下:
  1.   customStyle.dropDowns = [
  2.             {
  3.                 type: GC.Spread.Sheets.DropDownType.list,
  4.                 option: colorListData
  5.             }
  6.         ];
复制代码
然后定义colorListData
  1. var colorListData = {
  2.             multiSelect: true,
  3.             onItemSelected: colorClicked,
  4.             items: generateThemeColors
  5.         };
复制代码
在上述代码中,我们定义了三个属性,multiSelect用来表示支持多选,onItemSelected用来表示选中事件,items表示下拉框选项。

接下来 ,我们定义下items


  1. let colorMap = ['#1abc9c', '#2ecc71', '#3498db', '#9b59b6', '#34495e', '#f1c40f', '#e67e22', '#C76DA2', '#e74c3c', '#95a5a6'];
  2. let arr = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10']
  3. function generateColors(count, start, stop) {
  4.             var div = document.createElement("div");
  5.             div.style.width = "50px";
  6.             var step = (stop - start) / count | 0;

  7.             for (var i = start, index = 0; i < stop && index < count; i += step, index++) {
  8.                 var item = document.createElement("div");
  9.                 item.style.backgroundColor = colorMap[index];
  10.                 item.style.width = '35px';
  11.                 item.style.height = '15px';
  12.                 item.style.border = '1px solid #c3c3c3';
  13.                 item.style.color = 'white';
  14.                 item.style.padding = '2px';
  15.                 item.style.margin = '4px';
  16.                 item.classList.add("custom-color-block");
  17.                 item.innerHTML = arr[index]
  18.                 div.appendChild(item);
  19.             }
  20.             return div;
  21.         }

复制代码
在上述代码中,我们遍历了arr  ,生成了dom元素,并为dom元素。

我们运行一下上述代码,结果如下:
image.png923260497.png
此时选中B2单元格,选中item1 ,因为B2单元格一开始包含item1,所以此时如果再次选择item1  ,应该是取消选中item1 ,但是显示了undefined ,如下图所示

image.png221335753.png
这是为什么呢,不知道大家还记不记上面提到了onItemSelected用来表示选中事件 ,我们接下来对onItemSelected进行编码:
  1.   function colorClicked(event) {
  2.             var target = event.target;
  3.             if (target && target.classList.contains("custom-color-block")) {
  4.                 return target.innerHTML;
  5.             }
  6.         }
复制代码

添加上述代码后,我们就可以正常显示选中内容了。

但是,光选中内容还不够,怎么将选中内容的颜色也添加到单元格中呢?
接下来,我们需要自定义单元格。

2、自定义单元格
在SpreadJS中,是支持自定义单元格的,具体可以参考这篇学习指南:在这篇指南中,我们看到将布尔值true|false 转换为黄色五角星和灰色五角星。
看具体的代码,我们发现重写了Paint方法,所以我们也需要重写下paint ,具体代码如下:
  1.   ColorBlockCellType = function () {
  2.             GC.Spread.Sheets.CellTypes.Base.apply(this, arguments);
  3.             this.typeName = 'ColorBlockCellType'
  4.         }

  5.         ColorBlockCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  6.         ColorBlockCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  7.             //using the base text to render the text option.
  8.             GC.Spread.Sheets.CellTypes.Text.prototype.paint.call(this, ctx, '', x, y, w, h, style, options);

  9.             if (style.cellButtons && style.cellButtons.length > 0) {
  10.                 w = w - 28 * style.cellButtons.length;
  11.             }
  12.             var valueArr = value && value.split(','), newH = h, newY = y, newStyle = style.clone(), padding = 4,
  13.                 newX = x;
  14.             var sheet = options.sheet, zoomFactor = sheet.zoom();
  15.             if (valueArr && valueArr.length > 0) {
  16.                 newH = newH - 4;
  17.                 newY = y + 2;

  18.                 // remove the un-support options for block style;
  19.                 newStyle.cellButtons = [];

  20.                 valueArr.forEach((item, index) => {
  21.                     var itemWidth = GC.Spread.Sheets.CellTypes.Text.prototype.getAutoFitWidth(item, item, newStyle, zoomFactor, options) + 4;
  22.                     var itemBackground = colorMap[index];
  23.                     newStyle.backColor = itemBackground;

  24.                     newStyle.foreColor = 'white';
  25.                     if (newX + itemWidth > x + w) {
  26.                         itemWidth = x + w - newX;
  27.                     }
  28.                     if (itemWidth <= 0) {
  29.                         return;
  30.                     }
  31.                     GC.Spread.Sheets.CellTypes.Text.prototype.paint.call(this, ctx, item, newX, newY, itemWidth, newH, newStyle, options);
  32.                     newX += itemWidth + padding;
  33.                 });
  34.             }
  35.         };
复制代码
在上述代码中 ,我们遍历了每一个选项,给其添加背景色,前景色。·重新计算宽高,以及位置。
最后,我们为上述单元格赋值
  1.       spread.suspendPaint();
  2.         sheet.setText(0, 0, "Custom list");
  3.         sheet.setColumnWidth(1, 500);
  4.         sheet.setValue(0, 1, 'item1');
  5.         sheet.setValue(1, 1, 'item1,item2');
  6.         sheet.setValue(2, 1, 'item1,item2,item3');
  7.         sheet.setValue(3, 1, 'item1,item2,item3,item4');
  8.         sheet.setValue(4, 1, 'item1,item2,item3,item4,item5');
  9.         sheet.setValue(5, 1, 'item1,item2,item3,item4,item5,item6');
  10.         sheet.setValue(6, 1, 'item1,item2,item3,item4,item5,item6,item7');
  11.         sheet.setValue(7, 1, 'item1,item2,item3,item4,item5,item6,item7,item8');
  12.         sheet.setValue(8, 1, 'item1,item2,item3,item4,item5,item6,item7,item8,item9');
  13.         sheet.setValue(9, 1, 'item1,item2,item3,item4,item5,item6,item7,item8,item9,item10');
  14.         sheet.getRange('B1:B10').setStyle(customStyle);
  15.         sheet.getRange('B1:C10').cellType(new ColorBlockCellType());
  16.         spread.resumePaint();
复制代码
那么,本次想要实现的功能就实现了,具体可以参考附件demo。
多选背景色_update.html (7.67 KB, 下载次数: 0)

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部