在SpreadJS中实现中文日期选择器

发布时间:2015/10/21 00:10 发布者:frank.zhang

返回博客中心

SpreadJS 是一款基于 HTML5 和 jQuery 技术的插件支持自定义单元格类型,本文主要介绍如何使用自定义单元格类型和Jquery UI实现中文日期选择器。

1.下载JQuery UI的Datepicker

2015-10-21_113727

 

2.修改源码实现中文日期选择器

	this.regional[""] = { // Default regional settings
		closeText: "Done", // Display text for close link
		//prevText: "Prev", // Display text for previous month link
	    //nextText: "Next", // Display text for next month link
		prevText: "上一月",  
		nextText: "下一月",
		currentText: "Today", // Display text for current month link
		//monthNames: ["January","February","March","April","May","June","July","August","September","October","November","December"], // Names of months for drop-down and formatting
		monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月", ],
		monthNamesShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], // For formatting
		dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], // For formatting
	    dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], // For formatting
	    //dayNamesMin: ["Su","Mo","Tu","We","Th","Fr","Sa"], // Column headings for days starting at Sunday
		dayNamesMin: ["", "", "", "", "", "", "", ],
		weekHeader: "Wk", // Column header for week of the year
	    //dateFormat: "mm/dd/yy", // See format options on parseDate
		dateFormat: "yy年mm月dd日", // See format options on parseDate
		firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ...
		isRTL: false, // True if right-to-left language, false if left-to-right
		showMonthAfterYear: false, // True if the year select precedes month, false for month then year
		yearSuffix: "" // Additional text to append to the year in the month headers
	};

2015-10-21_114152

 

3.在SpreadJS创建CustomCellType

        var ns = GcSpread.Sheets;
        function DatePickerCellType() {
        }
        DatePickerCellType.prototype = new ns.CustomCellType();
        DatePickerCellType.prototype.createEditorElement = function (context) {
            //Create input presenter.
            return document.createElement("input");
        };

 

4.实现CustomCellType的activateEditor和deactivateEditor方法

        DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
            //Initialize input editor.
            if (editorContext) {
                $editor = $(editorContext);
                ns.CustomCellType.prototype.activateEditor.apply(this, arguments);
                $editor.datepicker();
                $editor.css("position", "absolute");
                $editor.attr("gcUIElement", "gcEditingInput");
                $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
            }
        }
        DatePickerCellType.prototype.deactivateEditor = function (editorContext, context) {
            //Remove input editor when end editor status.
            if (editorContext) {
                var element = editorContext;
                $(element).datepicker("hide");
                $(element).datepicker("destroy");
            }
            ns.CustomCellType.prototype.deactivateEditor.apply(this, arguments)
        };

 

5.实现setEditorValue和getEditorValue方法

        DatePickerCellType.prototype.setEditorValue = function (editor, value, context) {
            //Sync value from Cell value to editor value.
            $(editor).datepicker("setDate", value);
        };
        DatePickerCellType.prototype.getEditorValue = function (editor, context) {
            //Sync value from editor value to cell value.
            return $(editor).datepicker("getDate");
        };

 

6.实现updateEditor方法

        DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
            if (editorContext) {
                $editor = $(editorContext);
                $editor.css("width", cellRect.width - 1);
                $editor.css("height", cellRect.height - 3);
            }
        };

 

7.在SpreadJS中调用

        $(document).ready(function () {
            var spread = new GcSpread.Sheets.Spread(document.getElementById("ss"), { sheetCount: 1 });
            var sheet = spread.getActiveSheet();
            sheet.isPaintSuspended(true);
            sheet.setValue(0, 1, "DatePicker", ns.SheetArea.colHeader);
            sheet.getColumn(1).cellType(new DatePickerCellType()).width(100).formatter("yyyy年MM月dd日");
            sheet.isPaintSuspended(false);
        });

 

效果截图:

2015-10-21_114408

2015-10-21_114449

示例下载:

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


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网