lorenzofox3 / lrStickyHeader

make table headers sticky
MIT License
174 stars 27 forks source link

How to make lrStickyHeader to be used in angular app #3

Closed hnguyenec closed 8 years ago

hnguyenec commented 9 years ago

I am current struggling how to inject lrStickyHeader to angular app to be able to call: lrStickyHeader(element[0]) as from example as in strict DI mode, we need explicitly specify lrStickyHeader as a dependency

lorenzofox3 commented 9 years ago

lrStickyheader supports the standard packagings: AMD, Commonjs and default to a global variable if none of it is used.

So the easiest would be to wrap the global in a factory so it becomes injectable by angular:

// assuming the script lrStickyHeader.js has already run (so the global variable exists)

angular.module('myModule', ['myDependencies'])
    .factory('lrStickyHeader', function(){
         return function(table){
            lrStickyHeader(table);
         }
})

so now you can get it injected

hnguyenec commented 9 years ago

Thanks lorenzofox3 Will give it a try

I have defined a factory as above then create a directive angular .module('core') .directive('stStickyHeader', stStickyHeader);

stStickyHeader.$inject = ['$window', 'lrStickyHeader'];
function stStickyHeader($window, lrStickyHeader){
    var DDO = {
        require: '^?stTable',
        link: stStickyHeaderLink
    };

    function stStickyHeaderLink(scope, element, attrs, tableController){
        var stickyHeader = lrStickyHeader(element[0]);
        scope.$on('$destroy', function () {
          stickyHeader.clean();
        });

        scope.$watch(function () {
          return tableController.tableState();
        }, function () {
          $window.scrollTo(0, lrStickyHeader.treshold);
        }, true);
    }

    return DDO;
}

And my table html "table st-table="list" st-safe-src="safeList" class="table table-striped" st-sticky-header"

However, lrStickyHeader(element[0]) always return 'Undefined' Do you have any idea ?

ghost commented 8 years ago

lorenzofox3 , why not add this to your documentation? give an example on the document so that we can use it.

ghost commented 8 years ago

require: '^?stTable' should be require: '?^stTable'

lorenzofox3 commented 8 years ago

see code source of stStickyHeader and example on readme