Idea for a Paginator directive
Hi guys. I recently developed an Angular application (Angular is AWESOME by the way!) and I had a huge list of items that I needed to paginate. I noticed that Angular doesn't provide any directive to do it easily. The only directive is the ng-repeat that can be use to print all the elements of the list.
I thought that this could be a problem felt by other developers as well, so I tried to write a generic directive that could be configured and used by anyone to paginate any list of items, the idea was to write it so that one day it could be part of a framework. Now I think that the day has come!
I'd like to give something back to the wonderful community that gave me Angular, I'd like to continue developing this paginator directive and include it in your project.
Here I wrote a jsFiddle that shows the idea, it's a little bit messy but it's a starting point. In the future I plan to improve it with things like:
- implement unit tests;
- implement a companion service that can be used to save the paginator state (current page, current sorting...) so that it can be retrieved when the view containing the paginator is reloaded;
- cleaning the directive's
$scopeinterface so that I can remove from the template all that messy bindings; - give the user the possibility to define it's own template that can be passed to the paginator as the template to be used when the paginator must render itself;
- ...
Let me know what you guys think about this.
@redaemn It might be useful addition to the angular-ui. Just do the things from your list (especially tests and the code cleanup) and send a pull request so we can review / comment on the code through the GitHub interface.
Also, we are starting the bootstrap efforts in a separate repo so if you directive could be generic enough so it could work with different templates, this would be great.
I'm reviewing the code myself right now. I would suggest the directive to only provide view helper functions, but nothing else. This way people are able to use whatever html structure they wish.
For instance, lets say we use something like ui-repeat which gives you a subset of the original data passed. You would use meta information like $paging.next(), $paging.prev(), etc that would safely be namespaced. You could either put this ui-repeat on a container and provide the meta-data inside this container (but you would need a way to specify which child ng-repeat is the target).
These are assorted suggestions, etc. But ya I would drop the built-in template and just focus on providing helper functions.
On Tue, Oct 16, 2012 at 2:07 PM, Pawel Kozlowski [email protected]:
@redaemn https://github.com/redaemn It might be useful addition to the angular-ui. Just do the things from your list (especially tests and the code cleanup) and send a pull request so we can review / comment on the code through the GitHub interface.
Also, we are starting the bootstrap efforts in a separate repo so if you directive could be generic enough so it could work with different templates, this would be great.
— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/angular-ui/issues/231#issuecomment-9506883.
Dean J Sofer [email protected]
BS Computer Information Systems California State Polytechnic University, Pomona Telephone: 714-900-2254 Website: www.DeanSofer.com Github: github.com/ProLoser
Thanks for your comments guys!!
@ProLoser I agree with you, the built-in template was just an example (taken from what I already developed) to show the idea of the paginator. In the next days (maybe this evening) I'm going to post a new example so that you guys can see what I have in mind... I wouldn't want to start working on something that's completely absurd and lose precious time
Ok, I posted a new version of the example here.
As you can see, you define a <paginator> node setting some configuration parameters; then, as the content of the node, you can write your paginator view. The view can bind to some methods that the paginator exposes to access its data. In the future, I plan to add a templateUrl parameter to the paginator that can be used to set the Url of a partial that the paginator must dynamically load as its view, instead of using the content of the node.
This works as ng-repeat does, this seemed a good approach to me. Also notice in the example that if the list to be paginated changes in the parent scope (try pressing the Regenerate Products List button) the paginator automatically updates itself.
Is this what you guys had in mind?!?
This looks great, redaemn. My main suggestion would be to namespace the API and make the function names simpler, though. Here's my idea, allowing to assign an ng-model to the pager (so you can have 'pager1' and 'pager2' on a page), which defaults to $pager.
I would also simplify the API a bit, and make it match ng-repeat ($pager.$index is current page index, $pager.$first is boolean, $pager.$last is boolean).. working on these ideas in a plunker
Gabriele,
This starts to look good. The only troubling part for me is sorting, should it really be part of this directive? If so we need to make it more flexible.
As mentioned already the way to progress would be to clean up the code, prepare tests and open a PR.
Here's my idea with a simpler API in a gist. I have to go so i didn't have time to test it: https://gist.github.com/3907871
This leaves only what is needed for the api to the developer, and it uses an api similar to ng-repeat (index, first, last).
Also it lets the developer do all the sorting. It is also less computationally intensive as it doesn't do grouping, just basic math and splicing instead.
Nice one Andy! Although we really need to extract the service part of it to make sure that we can plug boostrap on top.
@ajoslin Andy, at first I didn't understand the advantage of giving an ng-model to the paginator, you said because "so you can have 'pager1' and 'pager2' on a page" but you can have multiple paginators inside a page even without using ng-model... every instance of the directive has its own private state after all. The only advantage I could think of is that assigning an ng-model to the directive you can change the state of the directive from inside the controller (for example programmatically change the currently displayed page and things like that) but I was thinking to do this by extracting the paginator state inside a companion paginator service that could be injected and used by controllers. Which one do you guys think could be the better approach?!? Maybe the ng-model approach is cleaner...
@pkozlowski-opensource Pawel, what do you mean by "we really need to extract the service part of it"?!? And about sorting, I was thinking too if it should really be part of the directive, maybe you could sort the list of items outside the directive and then let the paginator update itself... I don't know if that can be done, maybe i'll try writing a fiddle.
My notes about @ajoslin gist:
- We should rename this to
ui-paginatororui-paginate(the second being closer in syntax tong-repeat) - I don't think we should use
ng-modelfor assigning the$pager, I think it should be reserved for something more useful. - What is a group? Is a group a bunch of items or a bunch of pages? Why do we need to have a bunch of pages?
- Maybe we should drop the group syntax and stick to purely using page
- I prefer grammar-friendly variables like
firstGroupvsgroupFirst
- Why not make
setPage()justpage()sinceprev()andnext()are nouns, not verbs - We should try to visualize how people render the numbers
- I feel as though we should have a
$pager.pagescount - We should be able to easily accomplish something like this:
|< << 1, 2, 3 ... 5, [6], 7 ... 10, 11, 12 >> >| - Should we add helpers for generating the numbers and options to specify a radius?
- I feel as though we should have a
- I do not like the use of
pages-per-group="x"anditems-per-group="y"- Instead I think we should pass an object of options to paginator
- Options could be `ui-paginate="{items:10,pages:2,model:'$pager'}"
- Option objects allow people to easily store multiple configurations in 1 variable
- Options more seamlessly integrate with our existing
ui.configapproach
Now usually you want to modify the array of an ng-repeat, but you want to access paging methods from outside this repeater.
Here is my thought on how to do this:
<!-- At this scope, people is the full dataset -->
<div ng-model="people | mySort" ui-paginate="{limit:10,pages:2}">
<a ng-click="$pager.next()">Next</a>
<!-- At this scope, people is a subset of the dataset -->
<ul>
<li ng-repeat="person in people|whatever">...</li>
</ul>
</div>
Or some other variations
<div ui-paginate="people | mySort" ui-options="{limit:10,pages:2, pager: '$myPager' }">
<ul>
<!-- This allows access to both the full dataset AND the subset -->
<li ng-repeat="person in $myPager.data | mySecondarySort">...</li>
</ul>
</div>
Of course I'm still throwing around ideas but this is what's been rolling around in my head. I do not think we should have a separate service and I think we should factor out sorting entirely (let the devs do their own sorting / filtering).
@ProLoser I don't think putting ng-model through a filter is intuitive, because ng-model normally has to be an assignable expression. I like the rest of your ideas, though.
And you're right, we shouldn't give the developer a 'groups' option. Instead as you said we should give him a 'pages' array (just numbers 1-10 or whatever) and maybe a little more info about the pages. Then that's enough information to make the groups option himself if he needs to.
@redaemn Well.. we don't really need an isolated scope for the pager. And I don't really think it's a good idea to require the developer to put all of his paging information instead a <ui-paginator></ui-paginator> tag. The developer might want the next/prev button to be way seperate from the actual pages' information, and he might want to end the ui-paginator before that.
I also start to agree with @pkozlowski-opensource . Why do we need this to be a directive? It can just be a service. This does absolutely nothing with the DOM.
function MyCtrl($scope, uiPager) {
//People can pass in a string (which will be watched for when the reference changes, eg if someone sets $scope.myList = [] later) or an array reference
$scope.pager = new uiPager('myList', {
pageSize: 10
});
}
Ya I was thinking that while writing it. I think I like the combination with ui-options the best. I started working on a fiddle for it but I got a bit mixed up trying to figure out if I should watch or observe and what not and had to take a break. On Oct 18, 2012 11:54 PM, "Gabriele Rabbiosi" [email protected] wrote:
@ajoslin https://github.com/ajoslin Andy, at first I didn't understand the advantage of giving an ng-model to the paginator, you said because "so you can have 'pager1' and 'pager2' on a page" but you can have multiple paginators inside a page even without using ng-model... every instance of the directive has its own private state after all. The only advantage I could think of is that assigning an ng-model to the directive you can change the state of the directive from inside the controller (for example programmatically change the currently displayed page and things like that) but I was thinking to do this by extracting the paginator state inside a companion paginator service that could be injected and used by controllers. Which one do you guys think could be the better approach?!? Maybe the ng-model approach is cleaner...
@pkozlowski-opensource https://github.com/pkozlowski-opensource Pawel, what do you mean by "we really need to extract the service part of it"?!? And about sorting, I was thinking too if it should really be part of the directive, maybe you could sort the list of items outside the directive and then let the paginator update itself... I don't know if that can be done, maybe i'll try writing a fiddle.
— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/angular-ui/issues/231#issuecomment-9591402.
What do you think about making it a service? See end of my last message.
My email did not contain the second half... Interesting point, but what about sorting and filtering? I can understand making a service though because than events outside the scope can still trigger pagination-related functions.
$scope.myPager = paginate(people, { limit: 10 });
...
<li ng-repeat="person in myPager.data">
Paginate as a service could work with filtering. We just $watch the first parameter that's passed in.
//watch with expression
$scope.pager = paginate('filteredPeople', {limit: 10});
/* .. later .. */
$scope.doSomething = function() {
$scope.filteredPeople = filter(people)
}
//watch with an expression + filter
$scope.pager = paginate('people | myFilter', { limit: 10 });
//watch with a function
$scope.pager = paginate(function() {
return $filter('myFilter', people);
}, {limit: 10});
I just don't want to go through the mess that ui-map went through.. with having a $scope object being assigned by a view element. The weird timing of when the $scope object actually exists and doesn't causes lots of confusion when a directive assigns it.
@ajoslin implementing uiPaginate as a service seems a good idea, I started thinking about it but I immediately had a problem. Following your examples, you said that you can $watch the first parameter passed to the service in order to recalculate the pagination when the list of items changes... but I couldn't do it inside a service, I don't have the current $scope (unless the controller that calls the service passes it). Is there a way to retrieve the $scope inside a service that I don't know of?!? I think the $scope is more of a directive thing... maybe that's why you need a directive to implement the paginator...
@redaemn good point. We would have to have the $scope pass in itself to the paginator service. I still like that better than making it a directive. Assigning an object to scope should be done in the controller if possible (it makes it much more testable, also we know exactly when the object has been assigned)
Something like this would work:
$scope.myPager = paginate($scope, 'items | myFilter', {limit: 10});
I haven't been following this at all. Sorry! But it occurs to me that this sort of thing is what directive controllers are for. You create a directive new the top of the page/section that is to be paged that creates the controller. It has access to the scope, etc. Then you can use various directives sprinkled liberally throughout the children of this directive that can "require" the pagination controller and share its state, in the same what that you seem to be talking with the service. Pete
On 20 October 2012 18:39, Andy Joslin [email protected] wrote:
@redaemn https://github.com/redaemn good point. We would have to have the $scope pass in itself to the paginator service. I still like that better than making it a directive. Assigning an object to scope should be done in the controller if possible (much much more testable, also we know exactly when the object has been assigned)
— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/angular-ui/issues/231#issuecomment-9633959.
@petebacondarwin indeed, passing the $scope to a service didn't seem a very clean solution to me... I was trying to find a cleaner way to make everything work. What you say seems a good idea, I'll look into it!!
Take a look at the FormController in AngularJS core ( https://github.com/angular/angular.js/blob/master/src/ng/directive/form.js#L36) or my funky ascending list directive (http://plnkr.co/edit/E3WhgQ) for some ideas about how directives can inter-communicate with controllers. Pete
On 20 October 2012 20:16, Gabriele Rabbiosi [email protected]:
@petebacondarwin https://github.com/petebacondarwin indeed, passing the $scope to a service didn't seem a very clean solution to me... I was trying to find a cleaner way to make everything work. What you say seems a good idea, I'll look into it!!
— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/angular-ui/issues/231#issuecomment-9634992.