在SpreadJS中,bindingPath 是一种用于绑定数据的路径。然而在某些情况下,我们可能需要快速清除这个 bindingPath 属性,以便重新进行数据绑定或者进行其他操作。传统的方法可能需要通过复杂的代码或者手动操作来实现,这不仅效率低下,而且容易出错。因此,我们需要一种更加便捷的方法来实现清除 bindingPath 的功能。
本文将介绍通过重写右键菜单的“清除内容”选项,当用户点击这个选项时,能够快速清除当前选中单元格区域内的所有单元格的 bindingPath。如下图:
要实现这个需求,只需要重新定义一个Command,然后将原来的Command替换掉即可。
- let myClearContents = {
- canUndo: true,
- name: "myClearContents",
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- Commands.startTransaction(context, options);
- let sheet = context.getSheetFromName(options.sheetName);
- sheet.suspendPaint();
- sheet.suspendEvent();
- sheet.suspendCalcService();
- options.cmd = "myClearContents";
- let selections = sheet.getSelections();
- selections.forEach((e) => {
- let { row, col, rowCount, colCount } = e;
- sheet.clear(
- row,
- col,
- rowCount,
- colCount,
- GC.Spread.Sheets.SheetArea.viewport,
- GC.Spread.Sheets.StorageType.data
- );
- sheet.clear(
- row,
- col,
- rowCount,
- colCount,
- GC.Spread.Sheets.SheetArea.viewport,
- GC.Spread.Sheets.StorageType.bindingPath
- );
- });
- sheet.resumePaint();
- sheet.resumeEvent();
- sheet.resumeCalcService();
- Commands.endTransaction(context, options);
- return true;
- }
- },
- };
- spread.commandManager().register("myClearContents", myClearContents);
- let oldOpenMenu = spread.contextMenu.onOpenMenu;
- spread.contextMenu.onOpenMenu = function (
- menuData,
- itemsDataForShown,
- hitInfo,
- spread
- ) {
- oldOpenMenu.apply(this, arguments);
- itemsDataForShown.forEach(function (item, index) {
- if (item && item.name === "gc.spread.contextMenu.clearContents") {
- item.command = "myClearContents";
- }
- });
- };
复制代码
为了避免撤销栈重做栈的显示信息有误,还需要修改undoList和redoList:
- let undoList = GC.Spread.Sheets.Designer.getCommand(
- GC.Spread.Sheets.Designer.CommandNames.UndoList
- );
- let redoList = GC.Spread.Sheets.Designer.getCommand(
- GC.Spread.Sheets.Designer.CommandNames.RedoList
- );
- redoList.commandMap["myClearContents"] = "清除内容";
- undoList.commandMap["myClearContents"] = "清除内容";
- designerConfig.commandMap = {};
- designerConfig.commandMap[GC.Spread.Sheets.Designer.CommandNames.UndoList] =
- undoList;
- designerConfig.commandMap[GC.Spread.Sheets.Designer.CommandNames.RedoList] =
- redoList;
复制代码
|
|