Excel喜欢SlickGrid的行为

时间:2010-11-19 18:14:52

标签: javascript jquery jquery-plugins slickgrid

我想用SlickGrid模仿电子表格的特定行为。用户:

  1. 点击要激活的单元格
  2. 输入=sum(或任何公式,
  3. 原始单元格地址已保存
  4. 用户选择单元格范围(我假设原始单元格关闭了编辑器)
  5. 焦点返回到原始单元格,并附加新的单元格范围。即= sum(r1c1,r2c2)。
  6. 让我失望的是需要改变焦点。

    var cell_with_formula = null;
    grid = new Grid($("#myGrid"), data, columns, options);
    
    // save original cell address, but there is no onBlur event
    grid.onBlur = function(args){
      cell_with_formula = args; // save address
    }; 
    
    grid.onCellRangeSelected = function(){
      if(cell_with_formula){
        // check if cell_with_formula has `=` at begining
        // if so, append selected range    
        cell_with_formula = null;
      }
    }; 
    

    提前致谢!

1 个答案:

答案 0 :(得分:6)

这在SlickGrid 1.4.x中是不可能的,但在目前仍处于活动开发状态的2.0版本中将受到支持。 alpha被托管在GitHub上的一个单独的分支 - https://github.com/mleibman/SlickGrid/tree/v2.0a中,我刚用一个例子检查了支持这个的初步代码。请参阅https://github.com/mleibman/SlickGrid/commit/17b1bb8f3c43022ee6aec89dcab185cd368b8785

这是一个基本的公式编辑器实现:

function FormulaEditor(args) {
    var _self = this;
    var _editor = new TextCellEditor(args);
    var _selector;

    $.extend(this, _editor);

    function init() {
        // register a plugin to select a range and append it to the textbox
        // since events are fired in reverse order (most recently added are executed first),
        // this will override other plugins like moverows or selection model and will
        // not require the grid to not be in the edit mode
        _selector = new Slick.CellRangeSelector();
        _selector.onCellRangeSelected.subscribe(_self.handleCellRangeSelected);
        args.grid.registerPlugin(_selector);
    }

    this.destroy = function() {
        _selector.onCellRangeSelected.unsubscribe(_self.handleCellRangeSelected);
        grid.unregisterPlugin(_selector);
        _editor.destroy();
    };

    this.handleCellRangeSelected = function(e, args) {
        _editor.setValue(_editor.getValue() + args.range);
    };


    init();
}