SpreadJS自定义右键菜单

SpreadJS是一个JavaScript的电子表格和网格功能控件,基于HTML5和jQuery技术,通过画布呈现在客户端。SpreadJS使用Canvas绘制表格,默认情况下没有右键菜单,本文主要介绍如何使用JavaScript创建一个右键菜单。

发布于 2015/10/20 00:00

SpreadJS

SpreadJS是一个JavaScript的电子表格和网格功能控件,基于HTML5和jQuery技术,通过画布呈现在客户端。SpreadJS使用Canvas绘制表格,默认情况下没有右键菜单,本文主要介绍如何使用JavaScript创建一个右键菜单。

1.添加一个spreadContextMenu菜单

<body>
    <div id="ss" style="width: 500px; height: 300px; border: 1px solid gray">
    </div>
    <ul id="spreadContextMenu" class="dropdown-menu" role="menu" style="display: none">
        <li><a class="localize" data-action="cut">剪切</a></li>
        <li><a class="localize" data-action="copy">复制</a></li>
        <li><a class="localize" data-action="paste">粘贴</a></li>
        <li class="context-header divider"></li>
        <li class="context-header"><a class="localize" data-action="insert">插入</a></li>
        <li class="context-header"><a class="localize" data-action="delete">删除</a></li>
        <li class="context-cell divider"></li>
        <li class="context-cell context-merge"><a class="localize" data-action="merge">合并</a></li>
        <li class="context-cell context-unmerge"><a class="localize" data-action="unmerge">取消合并</a></li>
    </ul>
</body>

 

2.右键单击的事件

//右键点击触发
        function processSpreadContextMenu(e)
        {
            // move the context menu to the position of the mouse point
            var sheet = spread.getActiveSheet(),
                target = getHitTest(e.pageX, e.pageY, sheet),
                hitTestType = target.hitTestType,
                row = target.row,
                col = target.col,
                selections = sheet.getSelections();
            var isHideContextMenu = false;
            if (hitTestType === GcSpread.Sheets.SheetArea.colHeader)
            {
                if (getCellInSelections(selections, row, col) === null)
                {
                    sheet.setSelection(-1, col, sheet.getRowCount(), 1);
                }
                if (row !== undefined && col !== undefined)
                {
                    $(".context-header").show();
                    $(".context-cell").hide();
                }
            } else if (hitTestType === GcSpread.Sheets.SheetArea.rowHeader)
            {
                if (getCellInSelections(selections, row, col) === null)
                {
                    sheet.setSelection(row, -1, 1, sheet.getColumnCount());
                }
                if (row !== undefined && col !== undefined)
                {
                    $(".context-header").show();
                    $(".context-cell").hide();
                }
            } else if (hitTestType === GcSpread.Sheets.SheetArea.viewport)
            {
                if (getCellInSelections(selections, row, col) === null)
                {
                    sheet.clearSelection();
                    sheet.endEdit();
                    sheet.setActiveCell(row, col);
                    updateMergeButtonsState();
                }
                if (row !== undefined && col !== undefined)
                {
                    $(".context-header").hide();
                    $(".context-cell").hide();
                    showMergeContextMenu();
                } else
                {
                    isHideContextMenu = true;
                }
            } else if (hitTestType === GcSpread.Sheets.SheetArea.corner)
            {
                sheet.setSelection(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
                if (row !== undefined && col !== undefined)
                {
                    $(".context-header").hide();
                    $(".context-cell").show();
                }
            }
            var $contextMenu = $("#spreadContextMenu");
            $contextMenu.data("sheetArea", hitTestType);
            if (isHideContextMenu)
            {
                hideSpreadContextMenu();
            } else
            {
                $contextMenu.css({ left: e.pageX, top: e.pageY });
                $contextMenu.show();
                $(document).on("click.contextmenu", function ()
                {
                    if ($(event.target).parents("#spreadContextMenu").length === 0)
                    {
                        hideSpreadContextMenu();
                    }
                });
            }
        }

 

3.给菜单处理方法

        //右键菜单点击触发
        function processContextMenuClicked()
        {
            var action = $(this).data("action");
            var sheet = spread.getActiveSheet();
            var sheetArea = $("#spreadContextMenu").data("sheetArea");
            hideSpreadContextMenu();
            switch (action)
            {
                case "cut":
                    GcSpread.Sheets.SpreadActions.cut.call(sheet);
                    break;
                case "copy":
                    GcSpread.Sheets.SpreadActions.copy.call(sheet);
                    break;
                case "paste":
                    GcSpread.Sheets.SpreadActions.paste.call(sheet);
                    break;
                case "insert":
                    if (sheetArea === GcSpread.Sheets.SheetArea.colHeader)
                    {
                        sheet.addColumns(sheet.getActiveColumnIndex(), 1);
                    } else if (sheetArea === GcSpread.Sheets.SheetArea.rowHeader)
                    {
                        sheet.addRows(sheet.getActiveRowIndex(), 1);
                    }
                    break;
                case "delete":
                    if (sheetArea === GcSpread.Sheets.SheetArea.colHeader)
                    {
                        sheet.deleteColumns(sheet.getActiveColumnIndex(), 1);
                    } else if (sheetArea === GcSpread.Sheets.SheetArea.rowHeader)
                    {
                        sheet.deleteRows(sheet.getActiveRowIndex(), 1);
                    }
                    break;
                case "merge":
                    var sel = sheet.getSelections();
                    if (sel.length > 0)
                    {
                        sel = sel[sel.length - 1];
                        sheet.addSpan(sel.row, sel.col, sel.rowCount, sel.colCount, GcSpread.Sheets.SheetArea.viewport);
                    }
                    updateMergeButtonsState();
                    break;
                case "unmerge":
                    var sels = sheet.getSelections();
                    for (var i = 0; i < sels.length; i++)
                    {
                        var sel = getActualCellRange(sels[i], sheet.getRowCount(), sheet.getColumnCount());
                        for (var r = 0; r < sel.rowCount; r++)
                        {
                            for (var c = 0; c < sel.colCount; c++)
                            {
                                var span = sheet.getSpan(r + sel.row, c + sel.col, GcSpread.Sheets.SheetArea.viewport);
                                if (span)
                                {
                                    sheet.removeSpan(span.row, span.col, GcSpread.Sheets.SheetArea.viewport);
                                }
                            }
                        }
                    }
                    updateMergeButtonsState();
                    break;
                default:
                    break;
            }
        }

 

4.将事件初始化

        $(document).ready(function ()
        {
            spread = new GcSpread.Sheets.Spread($("#ss").get(0), {
                sheetCount: 3
            });
            $("#ss").bind("contextmenu", processSpreadContextMenu);
            $("#spreadContextMenu a").click(processContextMenuClicked);
            $(document).on("contextmenu", function ()
            {
                event.preventDefault();
                return false;
            });
        });

 

效果截图:

2015-10-20_152016

2015-10-20_152025

 

示例下载:

这就是你想要的SpreadJS,快来官网了解并下载它吧!

SpreadJS | 下载试用

纯前端表格控件SpreadJS,兼容 450 种以上的 Excel 公式,具备“高性能、跨平台、与 Excel 高度兼容”的产品特性,备受华为、苏宁易购、天弘基金等行业龙头企业的青睐,并被中国软件行业协会认定为“中国优秀软件产品”。SpreadJS 可为用户提供类 Excel 的功能,满足表格文档协同编辑、 数据填报、 类 Excel 报表设计等业务场景需求,极大的降低企业研发成本和项目交付风险。

如下资源列表,可以为您评估产品提供帮助:

相关产品
推荐相关案例
推荐相关资源
关注微信
葡萄城社区二维码

关注“葡萄城社区”

活字格低代码二维码

关注“活字格低代码”

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态