如何在指令AngularJS中包装jQuery(默认)handsontable?

时间:2015-09-28 18:22:07

标签: javascript jquery angularjs angularjs-directive handsontable

这里我有一个非AngularJS应用程序的动手,我正在研究广泛使用AngularJS(SPA)的软件的新版本。

我想知道:有没有办法将现有的handsontable实现包装在AngularJS指令中而不重写所有内容?

提前谢谢!

var hot = new Handsontable(container, {
    colHeaders: configTable.columnsHeader,
    columns: configTable.columnsConfig,
    colWidths: configTable.colWidths,
    rowHeight: 5,
    data: configTable.data,
    minSpareRows: 0,
    rowHeaders: false,
    contextMenu: false,
    currentRowClassName: 'row_selected',
    height: parentWindowHeight,
    width: parentWindowWidth,
    multiSelect: false,
    autoWrapRow: true,
    autoWrapCol: true,
    fillHandle: false,
    afterOnCellMouseOver: function (event, coords, cell) {
        // Long Implementation...
    },
    afterOnCellMouseDown: function (r, p, r2, p2) { //(r: Number, p: Number, r2: Number, p2: Number)
        // Long Implementation...
    },
    beforeKeyDown: function (event) { // event: Object
    },
    beforeChange: function (changes, source) { //(changes: Array, source: String)
        // Long Implementation...
    },
    afterChange: function (changes, source) { // (changes: Array, source: String)
        // Long Implementation...
    },
    beforeValidate: function (value, row, prop, source) { // value: Mixed, row: Number, prop: String, source: String
        valorMaximo = numeral($(hot.getColHeader()[prop]).data('valor')).value();
    },
    cells: function (row, col, prop) {
        var cellProperties = {};

        var sit = $(this.instance.getData()[row][0])[0];

        if (sit !== undefined) {
            sit = sit.value;

            if (sit != "1" && sit != "+" && sit != "-" && sit != "*") {
                cellProperties.readOnly = true;
                cellProperties.renderer = disabledRowRenderer;
            }
        }

        return cellProperties;
    },
    onSelection: function (r, c, r2, c2) { // readOnly cannot be selected
        var sit = $(this.getData()[r][0])[0];
        var meta = this.getCellMeta(r, c);

        if (sit !== undefined) {
            sit = sit.value;

            if (sit != "1" && sit != "+" && sit != "-" && sit != "*") {
                this.deselectCell();
            }
        }

        if (meta.readOnly) {
            this.deselectCell();
        }
    }
});

2 个答案:

答案 0 :(得分:1)

是的,你当然可以。最简单的方法是创建一个简单的指令,将所有现有逻辑放在链接函数中。您可能需要稍微调整一下代码,以便获得对您正在使用的元素的正确引用。参见:

myApp.directive('handsOnTable', function(){
    return {
        link: function(scope, element){
            // Your code here, using the element attribute.
        }
    };
});

但是......因为你要切换到AngularJS,我强烈建议你重写你的代码。它可能并不那么难,也没有那么多的工作。它会给你更多的未来代码,你可以摆脱jQuery(你应该,实际上)。在你的情况下,这可能意味着大多数选项,如autoWrapRow和autoWrapCol将成为你的指令的属性,像beforeValidate这样的方法将最终在控制器中。类似的东西:

myApp.directive('handsOnTable', function(){
    return {
        scope: {
          autoWrapRow: '=',
          autoWrapCol: '='
        },
        controller: function($element){
            var vm = this;

            vm.beforeValidate = beforeValidate;

            function beforeValidate(){
               // Do stuff. You can use the $element to do DOM manupulation
               // but you should keep that to a minimum and try to think the
               // Angular way of doing things.
            }
        },
        controllerAs: 'table',
        bindToController: true
    };
});

希望这会有所帮助。当然,这取决于你对Angular有多好。

答案 1 :(得分:0)

您是否尝试过不需要jQuery的ngHandsontable库?

https://github.com/handsontable/ngHandsontable