lorenzofox3 / Smart-Table

Code source of Smart Table module: a table/grid for Angularjs
http://lorenzofox3.github.io/smart-table-website/
1.8k stars 513 forks source link

st-persist not working with angular 1.5.9 #768

Closed falkontre closed 7 years ago

falkontre commented 7 years ago

I am using angular 1.5.9 and latest smart-table version (2.1.8).

So I'm looking for the persist feature and I came across the example in the official documentation (http://plnkr.co/edit/ekwiNt?p=preview) that uses different versions respect mine.

The example runs perfectly in plunkr but if I copy paste the example in my code the only thing that is persisted is only some filtering (not all of them work).

my table is declared like this

<table st-persist="myTable" st-table="productsColl" st-safe-src="products" class="table table-striped"> and in my angular controller the directive of st-persist is the same as the example.

` .directive('stPersist', function () { return { require: '^stTable', link: function (scope, element, attr, ctrl) { var nameSpace = attr.stPersist;

                //save the table state every time it changes
                scope.$watch(function () {
                    return ctrl.tableState();
                }, function (newValue, oldValue) {
                    if (newValue !== oldValue) {
                        localStorage.setItem(nameSpace, JSON.stringify(newValue));
                    }
                }, true);

                //fetch the table state when the directive is loaded
                if (localStorage.getItem(nameSpace)) {
                    var savedState = JSON.parse(localStorage.getItem(nameSpace));
                    var tableState = ctrl.tableState();
                    angular.extend(tableState, savedState);
                    ctrl.pipe();
                    scope.var = false;
                }
            }
        };
    });

`

Maybe this feature is broken by new angular version?

MrWook commented 7 years ago

Hello @falkontre,

i used the angular 1.5.9 in this plunker and it works just fine. I suggest you look into your localstorage and watch for mistakes. Maybe you overwrite the localstorage at some point in your application

lorenzofox3 commented 7 years ago

thanks @MrWook