The ui-select directive to a select and multi-select control with a search feature, I have to say is a control very useful. You can use it with a static list or dynamically getting data from a server.
In this post, I want to show you how to configure and to use ui-select directive to add a paging behavior (NOTA: by default the ui-select directive have no paging functionality).
The first step is to add the ui-select dependency into your module definition, following the official documentation the ngSanitize is required too.
This is the controller definition (I put it here to help you to understand the complete structure)
NOTE: the valid property will be used to identify when the item is a real option or the item for the paging option.
A very basic example of the HTML code to configure the ui-select control.
OK, at this point we have a complete functional select on the web page. (you can find more advanced examples on https://angular-ui.github.io/ui-select/ if you need more information about this directive).
Now let’s go ahead with the next step. I want to show you how to work with this control when your list has hundreds or thousands of items, you can not show the complete list, you need some paging controls to show a slice of the list to the user, we need to add 2 properties on ui-select-choices tag.
refresh: Define the function will be executed when the user type some character to filter the list.
And the getItems function implementation. This function is used to filter the list based on the text typed by the user (This is a very basic implementation to filter the list, you can put your own implementation with some http call or other one you need).
Now I am going to add the paging buttons to the ui-select control
The load more implementation:
NOTE: The logic inside this method is a basic example to filter an array you can put your custom logic, you can call a http service, or another type of logic you need.
The Complete example:
And the final result
The idea of this post is to show you an approach to how to add a paging feature to ui-select directive. it’s not the only way to implement.