1

我的自定义渲染器会导致在handsontable 中出现奇怪的单元格行为吗?当我尝试编辑单元格时,该光标自动下降到其他单元格并且其他单元格是混乱的,并且整个编辑过程从那一刻起就崩溃了。

我需要根据值对一些单元格进行着色,这就是为什么我需要使用一些机制,我在列定义中选择了定义类型,如下所示:

columns: { data: "some-property", type: {renderer: colorRenderer, editor:colorRenderer}}

不幸的是,当我这样做时,在单元格编辑期间会发生一些奇怪的行为。请在此处查看 jsfiddle:http: //jsfiddle.net/6QEtF/3/ line: 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;
    }
};
4

1 回答 1

2

尝试升级到最新版本

http://handsontable.com/

看看条件格式

于 2013-07-23T01:25:12.643 回答