[{"id":"ff052704-1389-4029-bcdd-73c6cbe9f807","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8e72e33e-b4ab-4fb3-98fc-a0b148134aed","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c90d5fdf-420f-4978-8bf5-c9a2bb4334b3","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"900e6cab-065d-4f1c-844d-efa3c074e270","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"760b37c7-c713-4b24-b9ba-4bfe7d8437a6","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"89c707ed-9841-4e53-96fb-940cc3214804","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"64e2f4a3-2303-4bfa-8a93-6c23ef01de58","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"aa71a884-c5bc-4842-8d6a-873dfd645167","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2605431b-dc80-491d-886e-28981595d277","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"69e79655-e015-4f9a-a230-2a25c988c926","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a570c8e7-07a2-47da-965b-da44fd1fa5cf","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86662220-9b9e-4940-9ced-d22642ea49a8","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24769ecd-2b08-4a85-b318-4f533bbf8393","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a244ead7-a2c6-47a3-ac17-c5dbfa337362","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"04e73782-aa78-4dfe-a4f9-e72ed4c78a11","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4f41e22e-eb51-49e5-aeae-a42dd6bf352c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"790bc77a-9216-48fd-b8f9-fbc374ebb155","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"21c6131f-0f2d-41d1-9284-6ad9ee803c1f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ded3a22-15eb-49b8-a488-e83c2cd872eb","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"33638a1c-7196-42c1-a96d-38b2d9ba8ac4","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a0842a3-20b1-40c3-8e00-cd5941ffdf53","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6e6ac5b1-1501-4e28-89cc-525139488537","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b0576ca2-cb84-4390-9f95-9354ec20eda5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"55fd3bb8-18d4-4edb-9640-ca3a365b798f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dacc7931-6785-4675-be31-80930403cf7b","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"63c577e6-6cf9-497e-94e1-2307f7d3f498","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1b88a165-2563-437e-99bb-ae30bd4b56db","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"adf3817f-7667-4a4c-8a5f-767b0b7e1e3e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"3a083583-1d5f-492b-b450-34b2b5c775b8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5d37413b-3600-4da9-9700-feea54355f59","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a5a73576-16ea-4cbc-925c-ef547389eaa5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1126cfab-6210-4e28-bee0-02c113fb7a0c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"09a66339-64c0-415c-b142-0691587a8e4a","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"0baaf91d-84f6-404c-a487-735226b6d5b6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5b7aeac5-c755-426a-95c7-1ae8e547179a","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"57c94653-8893-403b-a5b1-0d1e33a0bd0f","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]}]
在前篇《深入解析如何自定义Spread JS右键菜单(上)》中,我们介绍了如何在V11及以上版本的Spread JS自定义右键菜单的命令项,本文就这个话题继续深入讨论一下,研究一下如何自定义右键菜单样式,引入自定义功能。
注意:本文适用Spread JS版本是V11及以上版本。
enuView类的全名是GC.Spread.Sheets.ContextMenu.MenuView,它是SpreadJS界面元素的渲染,Spread JS将此类作为用户自定义的接口放出,以便于Spread JS用户可以方便地进行扩展和定制。关于MenuView更多用法可以参考学习指南。
MenuView包括了一个Constructor构造器以及两个方法,分别是createMenuItemElement和getCommandOptions。
getCommandOptions是用来获取对应右键菜单项绑定的命令的参数。
本例是采用Dom绑定事件来实现菜单功能,因此暂不涉及此方法,如果用绑定命令的方式指定右键菜单的功能时,可以方便地在此方法中执行自定义逻辑,或者修改命令参数。
本例中关注的关键点在于MenuView的createMenuItemElement方法,当用户在页面上右键点击触发右键菜单事件时,Spread JS会调用此方法来执行右键菜单Dom的渲染操作。因此,我们可以通过该方法来加入我们自定义的内容。
OK,现在大家已经了解了关于MenuView的作用,结合《深入解析如何自定义Spread JS右键菜单(上)》的内容,现在开始准备右键菜单项的定义,并且实现一个简单的color picker。
首先,定义color picker菜单项,并添加到右键菜单中。代码如下:
var colorPickers = {
text: "颜色选择器",
name: "gc.spread.colorPicker",
workArea: "viewport",
subMenu: [{
text: "背景色:",
name: "selectColorPicker1"
}, {
text: "字体色:",
name: "selectColorPicker2"
}]
};
var menuData = spread.contextMenu.menuData;
menuData.splice(1, 0, colorPickers);
以上代码不详细解释,只是说明一点,本例是采用Dom注册事件的方式来实现的功能,因此定义右键菜单项时并没有command属性,这不影响右键菜单的呈现。
其次,定义右键菜单执行的命令,命令分为两个,一个控制背景色,一个控制字体色。用命令实现功能的原因是命令可以方便用户执行撤回操作。代码如下:
/*
* 定义本例中用到的两个命令,
* 使用命令有个优点,就是方便用户执行撤回操作
* */
spread.commandManager().register("colorPicker_backColor", {
canUndo: true,
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
// 在此加cmd
options.cmd = "colorPicker_backColor";
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
changeColor(spread, options.color, true);
Commands.endTransaction(context, options);
return true;
}
}
});
spread.commandManager().register("colorPicker_foreColor", {
canUndo: true,
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
// 在此加cmd
options.cmd = "colorPicker_foreColor";
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
changeColor(spread, options.color, false);
Commands.endTransaction(context, options);
return true;
}
}
});
function changeColor(spread, color, flag) {
var sheet = spread.getActiveSheet();
var selections = sheet.getSelections();
if (selections && selections.length > 0) {
// 重要:挂起sheet绘制
sheet.suspendPaint();
selections.forEach(function (item) {
var cellRange = sheet.getRange(item.row, item.col, item.rowCount, item.colCount);
// 当flag为true时改变背景色,否则改变字体色
if (flag) {
cellRange.backColor(color);
} else {
cellRange.foreColor(color);
}
});
// 重要:恢复绘制
sheet.resumePaint();
}
}
最后,我们来实现一个基于Dom的Color Picker:
/*
* 创建颜色选择面板,并注册事件
* */
function createColorpicker(flag) {
var colors = ['rgb(255,255,255)',
'rgb(0,255,255)', 'rgb(255,0,255)',
'rgb(255,255,0)', 'rgb(255,0,0)',
'rgb(0,255,0)', 'rgb(0,0,255)',
'rgb(0,0,0)', 'rgb(64,64,64)',
'rgb(128,128,128)'];
var colorPicker = $("<div id='colorPicker'></div>");
for (var i = 0; i < colors.length; i++) {
var color = $("<span class='colorPicker colorPickerBorderMouseout' mycolor='" + colors[i] +
"' style='background-color:" + colors[i] + "'></span>");
color.mouseenter(function () {
$(this).removeClass("colorPickerBorderMouseout");
$(this).addClass("colorPickerBorderMouseenter");
});
color.mouseout(function () {
$(this).removeClass("colorPickerBorderMouseenter");
$(this).addClass("colorPickerBorderMouseout");
});
color.click(function () {
var that = $(this);
var color = that.attr("mycolor");
var spread = GC.Spread.Sheets.findControl(document.getElementById("ss"));
var sheet = spread.getActiveSheet();
var commandName = "colorPicker_foreColor";
if (flag) {
commandName = "colorPicker_backColor";
}
spread.commandManager().execute({
cmd: commandName,
sheetName: sheet.name(),
color: color
});
});
colorPicker.append(color);
}
return colorPicker;
}
OK,重点来了!现在我们通过代码,将我们实现的color picker加入到右键菜单中。
首先,定义自己的MenuView类,代码如下:
// 通过重写MenuView类,将自定义功能加入右键样式
function CustomMenuView() { }
第二步,继承原MenuView的功能。我们并不想自己实现整个右键菜单,因此我们需要用到原右键菜单的大部分样式与功能。代码如下:
// 继承原MenuView的功能
CustomMenuView.prototype = new GC.Spread.Sheets.ContextMenu.MenuView();
第三步,重写原生createMenuItemElement方法。重点来了,就是在这里加入自己的代码逻辑。代码如下:
CustomMenuView.prototype.createMenuItemElement = function (menuItemData) {
CustomMenuView.prototype.createMenuItemElement = function (menuItemData) {
// 先执行原类的方法,继承右键菜单的样式
var menuItemView = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(this,
menuItemData);
/*
* 添加自己的样式。
*
* menuItemView 是右键菜单当前项的容器div,
* 自定义的样式可以在此添加。
*
* 需要注意的是,如果想引入第三方控件,比如datepicker之类,
* 可能会与SpreadJS的样式与事件产生冲突,
* 因此除非您从技术上做过充分的验证,
* 否则不推荐在此设计引入过重的第三方控件。
* */
if (menuItemData.name === "selectColorPicker1") {
$(menuItemView).append(createColorpicker(1));
return menuItemView;
} else if (menuItemData.name === "selectColorPicker2") {
$(menuItemView).append(createColorpicker());
return menuItemView;
} else {
return menuItemView;
}
};
在SpreadJS渲染右键菜单时,每生成一项,都会调用createMenuItemElement方法。在重写createMenuItemElement时,关键点有以下几处:
1、 执行原createMenuItemElement的逻辑,获取menuItemView实例;
GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(this, menuItemData);
我们通过执行这句代码让SpreadJS为我们生成一个右键菜单,并且返回了menuItemView的实例,这个实例就是当前右键菜单项所在的Dom。
2、 对menuItemView实例进行操作;
if (menuItemData.name === "selectColorPicker1") {
$(menuItemView).append(createColorpicker(true));
return menuItemView;
} else if (menuItemData.name === "selectColorPicker2") {
$(menuItemView).append(createColorpicker(false));
return menuItemView;
} else {
return menuItemView;
}
好了,以上就是本文分享的全部内容,是不是正在惊叹于Spread JS的强大呢?不过要提醒大家的是,从设计上来讲,右键菜单中还是不宜引入过重的第三方控件的,如果一些动手能力强且脑洞比较大的同学想这样做,前期的技术调研一定要充分,只有很好地兼容Spread JS右键菜单的组件,才能让我们愉快地进行开发工作。