{"action":"created","issue":{"url":"https://api.github.com/repos/lorenzofox3/Smart-Table/issues/454","labels_url":"https://api.github.com/repos/lorenzofox3/Smart-Table/issues/454/labels{/name}","comments_url":"https://api.github.com/repos/lorenzofox3/Smart-Table/issues/454/comments","events_url":"https://api.github.com/repos/lorenzofox3/Smart-Table/issues/454/events","html_url":"https://github.com/lorenzofox3/Smart-Table/issues/454","id":97027674,"number":454,"title":"stSort and stPersist not working if stSort contains expression + Solution","user":{"login":"ChrisWiGit","id":12424397,"avatar_url":"https://avatars.githubusercontent.com/u/12424397?v=3","gravatar_id":"","url":"https://api.github.com/users/ChrisWiGit","html_url":"https://github.com/ChrisWiGit","followers_url":"https://api.github.com/users/ChrisWiGit/followers","following_url":"https://api.github.com/users/ChrisWiGit/following{/other_user}","gists_url":"https://api.github.com/users/ChrisWiGit/gists{/gist_id}","starred_url":"https://api.github.com/users/ChrisWiGit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ChrisWiGit/subscriptions","organizations_url":"https://api.github.com/users/ChrisWiGit/orgs","repos_url":"https://api.github.com/users/ChrisWiGit/repos","events_url":"https://api.github.com/users/ChrisWiGit/events{/privacy}","received_events_url":"https://api.github.com/users/ChrisWiGit/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-07-24T11:02:18Z","updated_at":"2015-09-01T14:53:13Z","closed_at":null,"body":"Hi,\r\nsorry if this was already posted here. There are so many posts similar to my problem that I didn't know what to do. However, I also give a solution.\r\n\r\nI found out that stSort does not correctly read the table state if the sort attribute is a method instead of property. I'm using stPersist to jsonfy the tablestate.\r\n\r\ne.g.\r\n```javascript\r\n<th st-sort=\"sort.aMethod\"..>\r\n```\r\nwhere sort is a scope object with methods for sorting.\r\n\r\nhttps://github.com/lorenzofox3/Smart-Table/blob/master/dist/smart-table.js\r\nIn Line 373 (in stSort) there is \r\n```javascript\r\nif (newValue.predicate !== predicate) {\r\n```\r\nwhere\r\n```javascript\r\nvar predicate = attr.stSort; \r\n```\r\nin our example it is \"sort.amethod\".\r\n\r\nHowever, if methods are used for sorting, the ctrl.tablestate().sort.predicate contains a function binding and not a string. So the if decision in L373 cannot work since nowhere in code the function tableState.sort.predicate is created and JSON.parse does not work with functions, of course. \r\n(As a side note: the functionName attribute, if a binding is a function, will be empty if the sorter object looks like this:\r\n```javascript\r\n$scope.sorter = {\r\n   aMethod : function () { return ... };\r\n}\r\n```\r\nfunctionName uses prototype Function.name which will use the name between \"function\" and \"()\". However it is empty in most examples I've seen. On the other hand \"name\" of Function prototype doesn't seem to be readily available everywhere since it is a ES6 standard. See:\r\nhttps://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Function/name\r\n)\r\n\r\n**SOLUTION**\r\nI have created a plunk to show the problems\r\nhttp://plnkr.co/edit/kqyv4I?p=preview\r\n\r\nIn the header you can find stSort with direct property name (PropertyPersistSinceName) of a row and also a method name of a sorter object in scope (EvalNotPersist). \r\nClick on EvalNotPersist to sort and stop then run the plunk again to see that persistance is not given.\r\n\r\nIt works, however, for the suggested solution. I created a stSortx directive (mostly copying stSortx) and added a //NEW comment to highlight the changes. In short, I also save the plain attribute content as a string, save it to a new property of the tablestate and compare it later to check whether the directive is currently responsible for sorting.\r\nIt does not need a function name and is simple imho.\r\n\r\n\r\nI hope it helps.\r\n\r\nOtherwise good work! :+1: "},"comment":{"url":"https://api.github.com/repos/lorenzofox3/Smart-Table/issues/comments/136750216","html_url":"https://github.com/lorenzofox3/Smart-Table/issues/454#issuecomment-136750216","issue_url":"https://api.github.com/repos/lorenzofox3/Smart-Table/issues/454","id":136750216,"user":{"login":"chriswong929","id":6518517,"avatar_url":"https://avatars.githubusercontent.com/u/6518517?v=3","gravatar_id":"","url":"https://api.github.com/users/chriswong929","html_url":"https://github.com/chriswong929","followers_url":"https://api.github.com/users/chriswong929/followers","following_url":"https://api.github.com/users/chriswong929/following{/other_user}","gists_url":"https://api.github.com/users/chriswong929/gists{/gist_id}","starred_url":"https://api.github.com/users/chriswong929/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chriswong929/subscriptions","organizations_url":"https://api.github.com/users/chriswong929/orgs","repos_url":"https://api.github.com/users/chriswong929/repos","events_url":"https://api.github.com/users/chriswong929/events{/privacy}","received_events_url":"https://api.github.com/users/chriswong929/received_events","type":"User","site_admin":false},"created_at":"2015-09-01T14:53:13Z","updated_at":"2015-09-01T14:53:13Z","body":"I think Smart table should implement their st-sort attribute similarly. I encountered a similar problem where I had: \r\n\r\n```\r\n<th st-sort=\"::column.sorter\"\r\n...\r\nng-repeat=\"column in ctrl.vm.columns\">\r\n```\r\n\r\nand each column would have a custom sorting functionality:\r\n```\r\ncolumn.sorter = function sorter(data) {\r\n   return customSortingFunction(data);\r\n}\r\n```\r\n\r\nHowever, this will not trigger the `$scope.$watch` on `tableState` because we are only updating the sorting function's name in table state (L119):\r\n```\r\nif (ng.isFunction(predicate)) {\r\n        tableState.sort.functionName = predicate.name; // predicate.name is always 'sorter'\r\n```\r\nIn the way it is currently implemented, it does not allow for dynamic sorting functionality. We would have to specify a different function name for each column's sorting functionality. If the sorting function name is not unique, then `tableState.sort.funtionName` will not change and we run into problems because the `$scope.$watch` on `tableState` will not trigger."}}