为什么自定义渲染器不像预期的那样工作,并导致奇怪的表单元格行为
Why custom renderer do not work as expected and cause strange table cells behavior?
为什么我的自定义渲染器在handsontable中导致奇怪的细胞行为?当我尝试编辑单元格时,然后光标自动下降到其他单元格,其他单元格混乱,整个编辑过程从那一刻起崩溃。
我需要根据值给一些单元格上色这就是为什么我需要使用一些机制,我在列定义中选择了定义类型,像这样:
columns: { data: "some-property", type: {renderer: colorRenderer, editor:colorRenderer}}
不幸的是,当我这样做时,在单元格编辑期间会发生一些奇怪的行为。请在这里看看jsfiddle: http://jsfiddle.net/6QEtF/3/第191行是有问题的问题
我如何在不使用渲染器的情况下以其他方式对细胞着色,或者我的渲染器有什么问题?
请帮…
这是我的示例代码,完全工作是在JsFiddle(上面的链接)。
var COLUMN_TYPES = new Object();
// JSON fetched via Ajax from backend:
var res = getJSON();
// Create grid header names and definitions based on fetched JSON:
var column_names = [];
var column_defs = [];
for (var i=0; i<res.headers.length; i++) {
column_names[i] = res.headers[i].name;
column_defs[i] = {
data: "valueDTO." + i + ".value",
type: {renderer: colorRenderer, editor:colorEditor}, // 1 way <--- cause a problem!
//type: res.headers[i].type, // 2nd way is correct but without custom renderer which I need to colorize some cells.
source: res.headers[i].sources,
readOnly: res.headers[i].readOnly,
strict: res.headers[i].strict
};
COLUMN_TYPES["valueDTO."+i+".value"] = res.headers[i].type;
}
// Create grid table:
createHandsontable(res.rows, column_names, column_defs, res.values);
function createHandsontable(row_names, column_names, column_defs, values) {
var $container = $("#spreadsheet");
var $parent = $container.parent();
$container.handsontable({
startRows: 4,
startCols: 20,
manualColumnResize: true,
manualColumnMove: true,
columnSorting: true,
contextMenu: true,
rowHeaders: row_names,
colHeaders: column_names, //grid.headers,
data: values, // data init only once at start
columns: column_defs,
cells: function (row, col, prop) {
//return {type: {renderer: colorRenderer, editor: colorEditor}};
}
});
}
function colorRenderer (instance, td, row, col, prop, value, cellProperties) {
switch (COLUMN_TYPES[prop]) {
case 'text':
Handsontable.TextCell.renderer.apply(this, arguments);
Handsontable.TextCell.editor.apply(this, arguments);
if (value != null) {
if (value.toString().toLowerCase() === "green") {
$(td).css({
background: '#00DC00'
});
}
else if (value.toString().toLowerCase() === "amber") {
$(td).css({
background: '#FAE600'
});
}
else if (value.toString().toLowerCase() === "red") {
$(td).css({
background: 'red'
});
}
}
break;
case 'autocomplete':
Handsontable.AutocompleteCell.renderer.apply(this, arguments);
Handsontable.AutocompleteCell.editor.apply(this, arguments);
break;
case 'checkbox':
Handsontable.CheckboxCell.renderer.apply(this, arguments);
Handsontable.CheckboxCell.editor.apply(this, arguments);
break;
case 'numeric':
Handsontable.NumericCell.renderer.apply(this, arguments);
Handsontable.NumericCell.editor.apply(this, arguments);
break;
case 'date':
Handsontable.DateCell.renderer.apply(this, arguments);
Handsontable.DateCell.editor.apply(this, arguments);
break;
case 'handsontable':
Handsontable.HandsontableCell.renderer.apply(this, arguments);
Handsontable.HandsontableCell.editor.apply(this, arguments);
break;
default:
Handsontable.TextCell.renderer.apply(this, arguments);
Handsontable.TextCell.editor.apply(this, arguments);
break;
}
};
function colorEditor (instance, td, row, col, prop, value, cellProperties) {
switch (COLUMN_TYPES[prop]) {
case 'text':
Handsontable.TextCell.renderer.apply(this, arguments);
Handsontable.TextCell.editor.apply(this, arguments);
break;
case 'autocomplete':
Handsontable.AutocompleteCell.renderer.apply(this, arguments);
Handsontable.AutocompleteCell.editor.apply(this, arguments);
break;
case 'checkbox':
Handsontable.CheckboxCell.renderer.apply(this, arguments);
Handsontable.CheckboxCell.editor.apply(this, arguments);
break;
case 'numeric':
Handsontable.NumericCell.renderer.apply(this, arguments);
Handsontable.NumericCell.editor.apply(this, arguments);
break;
case 'date':
Handsontable.DateCell.renderer.apply(this, arguments);
Handsontable.DateCell.editor.apply(this, arguments);
break;
case 'handsontable':
Handsontable.HandsontableCell.renderer.apply(this, arguments);
Handsontable.HandsontableCell.editor.apply(this, arguments);
break;
default:
Handsontable.TextCell.renderer.apply(this, arguments);
Handsontable.TextCell.editor.apply(this, arguments);
break;
}
};
尝试升级到最新版本:
http://handsontable.com/
看一下条件格式
相关文章:
- 如何在单击单元格中的链接值时动态更改表行背景色
- 将JS封装到GWT单元格表中
- 谷歌工作表中的按钮,当单击时,会更改特定单元格的背景
- 通过单击引导表单元格选中复选框
- 在javascript中第一次单击行时获取表单元格值
- 获取HTML表中单击的列中第一个单元格和单击的行中第一个单元的内容
- 谷歌应用程序将电子邮件表单脚本转换为PDF格式,但希望清除一些单元格
- 在没有jQuery的JavaScript中查找单击的表单元格的行和列
- 单击表格单元格或单击图像本身时翻转图像
- 向动态创建的表中的单元格添加单击函数 - JavaScript
- 如何使用jQuery使表格中的每个单元格可单击
- JQuery或Javascript获得标签文本在表格单元格与单选按钮和标签的组合
- 如何在Meteor中为带有输入单元格表的表单做响应式绑定?
- 如何获得骨干事件的单元格视图单击
- Javascript隐藏表格单元格按钮单击
- 更改jQuery单元格表值
- 如何使单元格可单击并返回值
- 剑道网格文本选择在单元格/表内与多选启用
- 在td -访问下一个单元格中单击
- 打开一个表单后点击更多的单元格表(jquery/php)