handsontable-developer guide-cell editor
单元格编辑
cell editor
renderer:展示数据;editor:改变数据;renderer用一个函数表示;后者有一系列的操作,需要用class来表示;
EditorManager
handsontable()--init()--EditorManage实例化
1、选择editor
columns中editor的value可以是alias,也可以是class;每个table,有自己的editor instance;
2、prepare
3、display
trigger:enter,双击,F2;会调用beginEditing
4、close
会调用finishEditing
custom editor
enchance existing editor
var PasswordEditor = Handsontable.editors.TextEditor.prototype.extend();
PasswordEditor.prototype.createElements = function(){
Handsontable.editors.TextEditor.prototype.createElements.apply(this, arguments);
this.TEXTAREA = document.createElement('input');
this.TEXTAREA.setAttribute('type', 'password');
this.TEXTAREA.className = 'handsontableInput';
this.textareaStyle = this.TEXTAREA.style;
this.textareaStyle.width = 0;
this.textareaStyle.height = 0; Handsontable.Dom.empty(this.TEXTAREA_PARENT);
this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);
};
BaseEditor
它是一抽象类
common method
BaseEditor已经实现的方法。如果想创建复杂的编辑器,需要override common method,应该调用原生方法。
Handsontable.editors.BaseEditor.prototype.prepare.apply(this, arguments);
prepare(row, col, prop, td, cellProperties);
beginEditing(initialValue); 内部调用open()
finishEditing(revertToOriginal, ctrlDown, callback); 内部调用saveValue(), discardEditor()
discardEditor(validationResult);
saveValue(val, ctrlDown);
isOpend()
extend()
weditor specific method
这些方法在BaseEditor中没有实现,但在其他编辑器(TextEditor)中实现了。
init() 用法:创建编辑器的html结构
geValue()
setValue(newValue)
open()
close() 隐藏editor:this.editorDiv.style.display = 'none';
focus() 选择另一个cell,但是之前的值没有验证: this.editorInput.focus();
editor常用的属性
row, col, prop(数据源是对象数组时,表示属性名), TD, cellProperties 'object'
extends an existing editor
//passwordEditor(input type='password') extends textEditor(textarea),
var PasswordEditor = Handsontable.editors.TextEditor.prototype.extend();
PasswordEditor.prototype.createElements = function () {
// Call the original createElements method
Handsontable.editors.TextEditor.prototype.createElements.apply(this, arguments);
// Create password input and update relevant properties
this.TEXTAREA = document.createElement('input');
this.TEXTAREA.setAttribute('type', 'password');
this.TEXTAREA.className = 'handsontableInput';
this.textareaStyle = this.TEXTAREA.style;
this.textareaStyle.width = 0;
this.textareaStyle.height = 0;
// Replace textarea with password input
Handsontable.Dom.empty(this.TEXTAREA_PARENT);
this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);
};
create editor form scratch
1、create new editor
var SelectEditor = Handsontable.editors.BaseEditor.prototype.extend();
2、select
init()在创建editor时,调用(table);prepare()--cell 不展示编辑器--和open()有时间间隔;open()--css
SelectEditor.prototype.init = function() {
// Create detached node, add CSS class and make sure its not visible
this.select = document.createElement('SELECT');
Handsontable.Dom.addClass(this.select, 'htSelectEditor');
this.select.style.display = 'none';
// Attach node to DOM, by appending it to the container holding the table
this.instance.rootElement.appendChild(this.select);
};
3、option
想要这样使用
var hot = new Handsontable(document.getElementById('container'), {
data: someData,
columns: [
{
editor: SelectEditor,
selectOptions: ['option1', 'option2', 'option3']
}
]
});
应该在那个函数中处理option?init():使用selectEditor的column,会有相同的option,肯定不行;open():要尽可能快得打开编辑器,如果option值多得话,会很耗时;所以应该放在prepare()中。
override prepare()时,需要调用BaseEditor的原生方法,因为BaseEditor.prototype.prepare()会设置很多属性,这些属性会被其他方法调用。
4、实现特定的方法
SelectEditor.prototype.getValue = function(){
return this.select.value;
};
SelectEditor.prototype.setValue = function(value){
this.seletct.value = value;
};
SelectEditor.prototype.open = function(){
//如下
};
SelectEditor.prototype.close = function(value){
//如下
};
5、override behavior
监听程序
//通过上下箭头,选择值,enter确认选择值,enter保存选择值,最后EditorManager决定什么时候关闭editor
//EditorManager有一beforeKeyDown函数,可以调用stopImmediatePropagation(),来取消默认行为。
//覆盖默认行为的操作,只是针对指定的editor,所以在open中注册监听器,在close中移除监听器,以免影响其他editor的行为
var onBeforeKeyDown = function(event){
//this 是Handsontable.Core,而不是当前的editor
var instance = this;
//获取当前editor,需要使用getActiveEditor()。返回的是最近调用prepare()的编辑器
var editor = instance.getActiveEditor();
var selectedIndex = editor.select.selectedIndex; Handsontable.Dom.enableImmediatePropagation(event);
switch(event.keyCode){
case Handsontable.helper.keyCode.ARROW_UP:
var previousOption = editor.select.options[selectedIndex-1];
if(previousOption){
editor.select.value = previousOption.value;
}
event.stopImmediatePropagation();
event.preventDefault();
break;
case Handsontable.helper.keyCode.ARROW_DOWN:
var nextOption = editor.select.options[selectedIndex+1];
if(nextOption){
editor.select.value = nextOption.value;
}
event.stopImmediatePropagation();
event.preventDefault();
break;
}
};
注册监听器:包含css变换
//open的实现基于的假设是:option的处理函数是放在prepare()中的
//展示列表之前,设定了高和最小宽,以便匹配相应的单元格,这一步可选,但是不这样做的话,editor会根据浏览器来设定大小,不太容易
//把select放在cell上面
SelectEditor.prototype.open = function(){
var width = Handsontable.Dom.outerWidth(this.TD);
var height = Handsontable.Dom.outerHeight(this.TD);
var rootOffset = Handsontable.Dom.offset(this.instance.rootElement);
var tdOffset = Handsontable.Dom.offset(this.TD);
var editorSection = this.checkEditorSection();
var cssTransformOffset; //open editor后,需要进行css调整
//cssTransformOffset和resetCssTransform
switch(editorSection){
case 'top': cssTransformOffset = Handsontable.Dom.getCssTransform(this.instance.view.wt.wtScrollbars.vertical.clone.wtTable.holder.parentNode);
break;
case 'left': cssTransformOffset = Handsontable.Dom.getCssTransform(this.instance.view.wt.wtScrollbars.horizontal.clone.wtTable.holder.parentNode);
break;
case 'corner': cssTransformOffset = Handsontable.Dom.getCssTransform(this.instance.view.wt.wtScrollbars.corner.clone.wtTable.holder.parentNode);
break;
} var selectStyle = this.select.style;
if(cssTransformOffset && cssTransformOffset !== -1){
selectStyle[cssTransformOffset[0]] = cssTransformOffset[1];
}else{
Handsontable.Dom.resetCssTransform(this.select);
} this.select.style.height = height + 'px';
this.select.style.minWidth = width + 'px';
this.select.style.top = tdOffset.top - rootOffset.top + 'px';
this.select.style.margin = '0'; this.select.style.display = ''; //register listener
this.instance.addHook('beforeKeyDown', onBeforeKeyDown); };
SelectEditor.prototype.close = function(){
this.select.style.display = 'none'; //remove listener
this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
};
检查监听器
SelectEditor.protoype.checkEditorSection = function(){
if(this.row < this.instance.getSetting().fixedRowsTop){
if(this.row < this.instance.getSetting().fixedColumnsLet){
return 'corner';
}else{
return 'top';
}
}else{
if(this.row < this.instance.getSetting().fixedColumnsLeft){
return 'left';
}
}
};
6、注册editor
//如果把别名设为select,之前已经有select的话,就会重写,如果想重写的话,可以这样做;否则,还是用一个其他别名,比如,前缀为github的账号,这样就不容易冲突了。
Handsontable.editors.registerEditor('sfp12.selcet', SelectEditor);
默认的别名和类
| editor alias | editor class |
| text | Handsontable.editors.TextEditor |
| numeric | Handsontable.editors.NumericEditor |
| date | Handsontable.editors.DateEditor |
| autocomplete | Handsontable.eitors.AutocompleteEditor |
| checkbox | Handsontable.editors.CheckboxEditors |
| hansontable | Handsontable.editors.HandsontableEditor |
自定义编辑器的封装:
//放在立即执行的匿名函数中
(function(Handsontable){
var CustomEditor = Handsontable.editors.BaseEditor.prototype.extend(); // ...rest of the editor code // Put editor in dedicated namespace
//作为全局变量的一个属性(变量)
Handsontable.editors.CustomEditor = CustomEditor; // Register alias
//别名
Handsontable.editors.registerEditor('theBestEditor', CustomEditor); })(Handsontable);
使用
//扩展自定义编辑器
var AnotherEditor = Handsontable.editors.CustomEditor.prototype.extend()
//常规使用
var hot = new Handsontable(document.getElementById('container'), {
data: someData,
columns: [
//两种方式
{
editor: Handsontable.editors.CustomEditor
},
{
type: 'my.select'
}
]
});
handsontable-developer guide-cell editor的更多相关文章
- Microsoft Visual Studio 2008 未能正确加载包“Visual Web Developer HTML Source Editor Package” | “Visual Studio HTM Editor Package”
在安装Microsoft Visual Studio 2008 后,如果Visual Studio 2008的语言版本与系统不一致时,比如:在Windows 7 English System 安装Vi ...
- Ehcache(2.9.x) - API Developer Guide, Key Classes and Methods
About the Key Classes Ehcache consists of a CacheManager, which manages logical data sets represente ...
- Ehcache(2.9.x) - API Developer Guide, Cache Usage Patterns
There are several common access patterns when using a cache. Ehcache supports the following patterns ...
- Ehcache(2.9.x) - API Developer Guide, Write-Through and Write-Behind Caches
About Write-Through and Write-Behind Caches Write-through caching is a caching pattern where writes ...
- Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms
About Cache Eviction Algorithms A cache eviction algorithm is a way of deciding which element to evi ...
- 移动端目标识别(2)——使用TENSORFLOW LITE将TENSORFLOW模型部署到移动端(SSD)之TF Lite Developer Guide
TF Lite开发人员指南 目录: 1 选择一个模型 使用一个预训练模型 使用自己的数据集重新训练inception-V3,MovileNet 训练自己的模型 2 转换模型格式 转换tf.GraphD ...
- Intel® Threading Building Blocks (Intel® TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图
https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency G ...
- Ehcache(2.9.x) - API Developer Guide, Basic Caching
Creating a CacheManager All usages of the Ehcache API start with the creation of a CacheManager. The ...
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- Ehcache(2.9.x) - API Developer Guide, Using Explicit Locking
About Explicit Locking Ehcache contains an implementation which provides for explicit locking, using ...
随机推荐
- netty基本组件介绍
Netty做为一款用于搭建高性能网络应用程序的高级框架,由以下几个主要构件组成: 一.Channel Channel 是java NIO的一个基本构造,可以把channel看作是传入或者传出的数据载体 ...
- ETL流程概述及常用实现方法
ETL流程概述及常用实现方法 http://blog.csdn.net/btkuangxp/article/details/48224187 目录(?)[-] 1抽取作业 1手工开发抽取作业时候的常用 ...
- 【Codeforces】Codeforces Round #491 (Div. 2) (Contest 991)
题目 传送门:QWQ A:A - If at first you don't succeed... 分析: 按照题意模拟 代码: #include <bits/stdc++.h> usin ...
- Oracle 字符集的查看和修改 --转载
原文地址:Oracle 字符集的查看和修改 作者:piaoliuxiong 一.什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE ...
- Python 常用 PEP8 编码规范和建议
代码布局 缩进 每级缩进用4个空格. 括号中使用垂直隐式缩进或使用悬挂缩进. EXAMPLE: # (垂直隐式缩进)对准左括号 foo = long_function_name(var_one, va ...
- SQL Server实时同步更新远程数据库遇到的问题
工作中遇到这样的情况,需要在更新表TableA(位于服务器ServerA 172.16.8.100中的库DatabaseA)同时更新TableB(位于服务器ServerB 172.16.8.101中的 ...
- Elastic Search的学习
那些必须要知道的事儿 自然语言处理 自然语言处理之中文分词器 什么是Apache Lucene 什么是elasticsearch 搭建elasticsearch环境 Windows下 Java环境配置 ...
- 跟我学算法 - 读取excel文件(xlrd)
import xlrd import numpy as np # fname 表示文件名 fname = '1白.xlsx'# 打开文件 bk = xlrd.open_workbook(fname)# ...
- 分享 - 普通程序员如何转向AI方向
原作者:计算机的潜意识 原文链接,内容稍有改动,侵删 1. 目的2. AI领域简介3. 学习方法4. 学习路线 0) 领域了解1) 知识准备2) 机器学习3) 实践做项目4) 深度学习5) 继续机器学 ...
- 【333】Python3.6 格式化文本
看如下例子进行体会: min_temperature = 0 max_temperature = 300 step = 20 # \t: A tab print('Fahrenheit\tCelsiu ...