Smart table sorting not working for ajax loaded data
Hi,
I have created a plunker link: http://plnkr.co/edit/PBX00TGC5E1UEWzRAmPU?p=preview
to show that smart table data get's reset to blank once we click header for sorting. This issue is particularly seen when data is loaded using ajax.
Any quick resolution would be really appreciated.
Thanks Amit
...experiencing exactly the same issue... it seems the initialisation of safeCopy fails
var safeCopy = copyRefs(displayGetter($scope));
results in undefinedfor safeCopy.
Btw: I'm using v. 1.2.23
ok. it seems, this is probably not a bug in the code; but in the documentation.
Obviously stSafeSrc attribute is the solution to the problem. Using this attribute according to the provided example (http://lorenzofox3.github.io/smart-table-website/) fixes the sorting issue.
I would suggest to mention the importance of this attribute for use cases with async data loading.
I tried that attribute "st-safe-src" . Although it fixes the problem but I see too many errors in the console while performing sorting. Like the one below: Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
This looks like a bug to me.
no, not really; you have to copy the data array to an other variable in the scope:
st-table="displayedCollection" st-safe-src="rowCollection"
and
//copy the references (you could clone ie angular.copy but then have to go through a dirty checking for the matches)
$scope.displayedCollection = [].concat($scope.rowCollection);
Hello
you are correct.
@cloud9amit You can have a look at that similar issue.
@tufan-yoc, please click on the "improve this documentation" and modify the markup file if you feel you can improve it.
But yep the idea is:
- You have a displayed collection (ie the repeater) which will be the result of sort, filter, slice operations on a base collection.
- when initialised smart-table create a copy of the provided st-table collection as its base collection. So the sort,filter,slice are based on the original asset. When calling with ajax, you don't have the dataset at initialisation time so that the base copy is actually wrong.
As a side node, I would rather fetch the data when resolving the controller dependencies (ie the
resolvein the route) it will ease your unit tests - When using the
st-safe-srcyou explicitly tells smart-table to watch and update its base collection ie the one it uses as the reference when sorting, filtering and slicing.
So basically the st-table is the displayed collection (the result) and st-safe-src is the base collection (the original dataset).
so if you set both to the same collection, the latest will be watched and will change the first one, which will trigger the watch...which will update the displayed one....etc.
Laurent
Thanks for explanation.
Closing this issue as It's working as expected.
Hi I am using angular smart table and used st-table="displayedCollection" st-safe-src="rowCollection" both but st-sort not working in my case.Will you please suggest some fix of it.
Hi,
I am having the same problem as Keshavmca12
Same problem as the two guys above.
Me too.
In my case it wasn't clear on the documentation that you must change:
<tr ng-repeat="row in rowCollection">
to:
<tr ng-repeat="row in displayedCollection">
when using st-safe-src. After that change everything work as expected.
Wrestling with this as we speak, I am really saddened that there is so much config required on an otherwise excellent table package.
This is a really core feature to any data table, how is this not enabled out of the box?
I don't think there is much configuration needed as discussed in many issues
- st-table is just a way to declare a variable name to expose it to the view
- st-safe-src is the only thing you have to set in the controller if you want smart table to keep tracks of external changes, that's it
http://plnkr.co/edit/UiPpWX9nUzRXopayLOxY?p=preview
Obviously the doc could be improved though, any suggestion ?
Thanks for the plunker. I have wired everything up as shown, but the result is randomly sorted data (the rows will change/move on click events, but the order is not correct).
In other example, the following hack is implemented/recomeneded, but it doesn't appear in the version you shared above:
$scope.displayed = [].concat($scope.rowCollection); via https://github.com/lorenzofox3/Smart-Table/issues/215
General suggestions for Smart Table semantic attributes
As for semantics of st-safe-src, it might be better to have something like st-table-data-from-XHR and st-table-placeholder. While these are much more verbose they are more semantic and would help with picking up the nomenclature.
My expectation for a drop-in angular datatable is to slap the st-table directive on my <table>, then pass it my data (an array of objects), and away you go! Ideally, the defaults settings provide a sortable, filterable table. Otherwise, given the troubles myself and others had with data from $http and/or XHRs, wiring it up by hand is tempting despite strong desire for a solution like smart-table to perform this for me.
+1 for proteo5 comment. And thank you for this comment!
I think the ideal solution to handle this is not to pass 2 collections in the cofiguration but to create a new attribute that is a flag to indicate if the user plans to update the original collection or not. The ST should handle this by itself and probably with no loss in performance. For example:
<table st-table="rowCollection" st-watch-changes="true">
Well, I sent a pull request that can improve the doc about this attribute.
I had the same problem too, it was not obvious in the documentation, however solved when following the instructions in the pluckr.
I did a pull request improving this part of the documentation but it's still in the "opened" status.
I agree that this may just be an issue with documentation/clarification of how to use this feature. When I set ng-repeat to the same variable as st-table, but not st-safe-src, it all started working again. It's also important that st-table and st-safe-src are not the same.
Also, it wasn't clear for me in the docs that along with st-safe-src you actually have to set the array in the controller (thought it was automatic, creating a copy on the fly).
By the way, couldn't this be done? It would be a lot easier