Smart-Table icon indicating copy to clipboard operation
Smart-Table copied to clipboard

Smart table sorting not working for ajax loaded data

Open cloud9amit opened this issue 12 years ago • 21 comments

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

cloud9amit avatar Sep 02 '14 10:09 cloud9amit

...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

tufan-yoc avatar Sep 02 '14 13:09 tufan-yoc

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.

tufan-yoc avatar Sep 02 '14 14:09 tufan-yoc

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.

cloud9amit avatar Sep 02 '14 17:09 cloud9amit

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);

tufan-yoc avatar Sep 02 '14 18:09 tufan-yoc

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:

  1. You have a displayed collection (ie the repeater) which will be the result of sort, filter, slice operations on a base collection.
  2. 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 resolve in the route) it will ease your unit tests
  3. When using the st-safe-src you 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

lorenzofox3 avatar Sep 03 '14 01:09 lorenzofox3

Thanks for explanation.

Closing this issue as It's working as expected.

cloud9amit avatar Sep 03 '14 06:09 cloud9amit

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.

Keshavmca12 avatar Sep 28 '15 07:09 Keshavmca12

Hi,

I am having the same problem as Keshavmca12

calvinKG avatar Oct 08 '15 09:10 calvinKG

Same problem as the two guys above.

starbit avatar Oct 20 '15 04:10 starbit

Me too.

agorm avatar Nov 13 '15 20:11 agorm

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.

proteo5 avatar Dec 09 '15 01:12 proteo5

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?

DeBraid avatar Dec 17 '15 16:12 DeBraid

I don't think there is much configuration needed as discussed in many issues

  1. st-table is just a way to declare a variable name to expose it to the view
  2. 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 ?

lorenzofox3 avatar Dec 17 '15 18:12 lorenzofox3

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.

DeBraid avatar Dec 17 '15 23:12 DeBraid

+1 for proteo5 comment. And thank you for this comment!

rodrigoroma avatar Dec 18 '15 02:12 rodrigoroma

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.

rodrigoroma avatar Dec 18 '15 10:12 rodrigoroma

I had the same problem too, it was not obvious in the documentation, however solved when following the instructions in the pluckr.

viniciuscb avatar Mar 09 '16 00:03 viniciuscb

I did a pull request improving this part of the documentation but it's still in the "opened" status.

rodrigoroma avatar Mar 09 '16 01:03 rodrigoroma

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.

georules avatar Mar 28 '16 16:03 georules

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

bernardoadc avatar Jul 07 '16 20:07 bernardoadc