SpreadJS自定义单元格系列-AutoComplete

SpareadJS强大的拓展性让我们可以轻而易举的开发出满足项目需求的内容,在这个系列中我们将为大家展示各式各样的自定义单元格。

发布于 2016/03/16 00:00

SpreadJS

 

SpareadJS强大的拓展性让我们可以轻而易举的开发出满足项目需求的内容,在这个系列中我们将为大家展示各式各样的自定义单元格。文中自定义单元格类型都以TypeScript完成,有关TypeScript的使用可参考用Visual Studio Code + TypeScript 完成自定义颜色选择器单元格

本文将介绍如何实现AutoComplete单元格。关于jQuery UI Autocomplete的详细使用请参考http://jqueryui.com/autocomplete/

 

1. 添加AutoCompleteCellType.ts文件

/// <reference path="../lib/SpreadJS/definition/gcspread.sheets.d.ts"/>
// http://jqueryui.com/autocomplete/
declare var $;
class AutoCompleteCellType extends GcSpread.Sheets.CustomCellType {
    availableTags = [
    ];
    constructor(availableTags: Array<string>) {
        super();
        if (availableTags && availableTags.length > 0) {
            this.availableTags = availableTags;
        }
    }
    createEditorElement(context) {
        //Create input presenter.
        return document.createElement("input");
    }
    activateEditor(editorContext: any, cellStyle: GcSpread.Sheets.Style, cellRect: GcSpread.Sheets.Rect, context?: any) {
        var $editor = $(editorContext);
        super.activateEditor(editorContext, cellStyle, cellRect, context);
        $editor.css("position", "absolute");
        $editor.attr("gcUIElement", "gcEditingInput");
        $editor.autocomplete({
            source: this.availableTags
        }); // initialize autocomplete widget
        $editor.autocomplete("widget").attr("gcUIElement", "gcEditingInput"); // keep focus when mouse down on dropdown
        return $editor;
    }
    deactivateEditor(editorContext: any, context?: any) {
        if (editorContext) {
            var $editor = $(editorContext);
            // $editor.autocomplete("hide");
            $editor.autocomplete("destroy");
        }
        super.deactivateEditor(editorContext, context);
    }
    setEditorValue(editorContext: any, value: any, context?: any) {
        $(editorContext).val(value);
    }
    getEditorValue(editorContext: any, context?: any) {
        return $(editorContext).val();
    }
    updateEditor(editorContext: any, cellStyle: GcSpread.Sheets.Style, cellRect: GcSpread.Sheets.Rect, context?: any) {
        if (editorContext) {
            var $editor = $(editorContext);
            $editor.css("width", cellRect.width);
            $editor.css("height", cellRect.height);
        }
    }
    isReservedKey(event: KeyboardEvent, context?: any): boolean {
        if (context.isEditing && (event.keyCode == 40 || event.keyCode == 38)) { // reserve up/down key to select items
            return true;
        }
        return false;
    }
    setAutocompleteSource(source: Array<string>) {
        this.availableTags = source;
    }
    getAutocompleteSource(): Array<string> {
        return this.availableTags;
    }
}

 

2. 使用AutoCompleteCellType

var ns = GcSpread.Sheets;
$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    ];
    var spread = new GcSpread.Sheets.Spread(document.getElementById("ss"), { sheetCount: 1 });
    var sheet = spread.getActiveSheet();
    sheet.isPaintSuspended(true);
    sheet.setValue(0, 1, "AutoComplete1", ns.SheetArea.colHeader);
    sheet.getColumn(1).cellType(new AutoCompleteCellType(availableTags)).width(200);
    
    sheet.setValue(0, 2, "AutoComplete2", ns.SheetArea.colHeader);
    var autoCompleteCellType = new AutoCompleteCellType();
    autoCompleteCellType.setAutocompleteSource(availableTags);
    sheet.getColumn(2).cellType(autoCompleteCellType).width(200);
    
    sheet.isPaintSuspended(false);
});

 

3. HTML代码

<html>
<head>
    <meta charset="UTF-8">
    <link href="./lib/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
    <link href="http://demo.grapecity.com.cn/SpreadJS/TutorialSample/external/spreadjs/css/gcspread.sheets.excel2013white.9.40.20153.0.css" rel="stylesheet" />
    <link href="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/css/smoothness/jquery-ui-1.10.0.custom.min.css" rel="stylesheet" />
</head>
<body>
    <div id="ss" style="width:800px;height:500px"></div>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="./lib/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/jquery-ui.js"></script>
    <script src="http://demo.grapecity.com.cn/SpreadJS/TutorialSample/external/spreadjs/gcspread.sheets.all.9.40.20153.0.min.js"></script>
    <script src="./src/AutoCompleteCellType.js"></script>
    <script src="./index.js"></script>
</body>
</html>

 

在第二三列单元格中输入内容即可查看效果。

preview

 

示例下载:

SpreadJS | 下载试用

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

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

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

关注“葡萄城社区”

活字格低代码二维码

关注“活字格低代码”

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