DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • SRE vs. DevOps
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Database Integration Tests With Spring Boot and Testcontainers
  • Reactive Programming

Trending

  • SRE vs. DevOps
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Database Integration Tests With Spring Boot and Testcontainers
  • Reactive Programming
  1. DZone
  2. Coding
  3. Languages
  4. Improving ui-select Control

Improving ui-select Control

The idea of this post is to show you an approach to how add a paging feature to ui-select directive. it's not the only way to implement.

Javier Santos user avatar by
Javier Santos
CORE ·
Sep. 25, 20 · Tutorial
Like (4)
Save
Tweet
Share
7.32K Views

Join the DZone community and get the full member experience.

Join For Free

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.

JavaScript
 




x


 
1
var app = angular.module('Paging-Functionalty-Demo', ['ngSanitize', 'ui.select']);



This is the controller definition (I put it here to help you to understand the complete structure)

JavaScript
 




x
11


 
1
app.controller('DemoCtrl', function ($scope) {
2
  var ctr = this;
3
  ctr.itemSelected = undefined;
4
  ctr.list = [ 
5
    {text: 'First Item',valid:true},
6
    {text: 'Second Item',valid:true},
7
    {text: 'Third Item',valid:true},
8
    {text: 'Fourth Item',valid:true},
9
    {text: 'Last Item',valid:true}
10
  ];
11
});


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.

HTML
 




x


 
1
<body class="ng-cloak" ng-controller="DemoCtrl as ctrl">
2

          
3
<ui-select ng-model="ctrl.itemSelected" >
4
  <ui-select-match placeholder="Search...">
5
    {{$select.selected.name}}
6
  </ui-select-match>
7
  <ui-select-choices repeat="item in ctrl.list | filter: {text:$select.search}">
8
    <div ng-bind-html="item.text | highlight: $select.search" ></div>
9
  </ui-select-choices>
10
  <ui-select-no-choice>
11
    <div>There is no items</div>
12
  </ui-select-no-choice>
13
</ui-select>
14

          
15

          
16
</body>



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.

  1. refresh: Define the function will be executed when the user type some character to filter the list.
HTML
 




x


 
1
<body class="ng-cloak" ng-controller="DemoCtrl as ctrl">
2

          
3
<ui-select ng-model="ctrl.itemSelected" >
4
  <ui-select-match placeholder="Search...">
5
    {{$select.selected.name}}
6
  </ui-select-match>
7
  <ui-select-choices repeat="item in ctrl.listFilter" refresh="ctrl.getItems($select.search)">
8
    <div ng-bind-html="item.text | highlight: $select.search" ></div>
9
  </ui-select-choices>
10
  <ui-select-no-choice>
11
    <div>There is no items</div>
12
  </ui-select-no-choice>
13
</ui-select>
14

          
15
</body>



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

JavaScript
 




x


 
1
ctr.getItems = function (search){
2
      ctr.listFilter = ctr.list.filter(item => item.text.includes(search));
3
 };



Now I am going to add the paging buttons to the ui-select control

HTML
 




x


 
1
<body class="ng-cloak" ng-controller="DemoCtrl as ctrl">
2

          
3
<ui-select ng-model="ctrl.itemSelected" >
4
  <ui-select-match placeholder="Search...">
5
    {{$select.selected.name}}
6
  </ui-select-match>
7
  <ui-select-choices repeat="item in ctrl.listFilter" refresh="ctrl.getItems($select.search)">
8
 <div ng-if="item.valid" ng-bind-html="item.text | highlight: $select.search" ></div>
9
    <div ng-if="!item.valid">
10
        <button style="width:100%;" ng-click="ctrl.loadMore($select.search,$event)">Load more...</button>
11
    </div>
12
  </ui-select-choices>
13
  <ui-select-no-choice>
14
    <div>There is no items</div>
15
  </ui-select-no-choice>
16
</ui-select>
17

          
18
</body>



The load more implementation:

JavaScript
 




x


 
1
  ctr.loadMore = function(search,$event){
2
       $event.stopPropagation();
3
       $event.preventDefault();
4
       ctr.paging++;
5
       ctr.listFilter = ctr.list.filter(item =>item.text.includes(search)).slice(0,(ctr.paging*2) +2);
6
      ctr.listFilter.push({text:'Load more Items',valid:false});
7
  };


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:

HTML
 




x
17


 
1
<body  ng-controller="DemoCtrl as ctrl">
2
<ui-select ng-model="ctrl.itemSelected" >
3
  <ui-select-match placeholder="Search...">
4
    {{$select.selected.text}}
5
  </ui-select-match>
6
  <ui-select-choices repeat="item in ctrl.listFilter" 
7
  refresh="ctrl.getItems($select.search)">
8
    <div ng-if="item.valid" ng-bind-html="item.text | highlight: $select.search" ></div>
9
    <div ng-if="!item.valid">
10
        <button style="width:100%;" ng-click="ctrl.loadMore($select.search,$event)">Load more...</button>
11
    </div>
12
  </ui-select-choices>
13
  <ui-select-no-choice>
14
    <div>There is no items</div>
15
  </ui-select-no-choice>
16
</ui-select>
17
</body>


HTML
 




x


 
1
<html>
2
<body  ng-controller="DemoCtrl as ctrl">
3
<ui-select ng-model="ctrl.itemSelected" >
4
  <ui-select-match placeholder="Search...">
5
    {{$select.selected.text}}
6
  </ui-select-match>
7
  <ui-select-choices repeat="item in ctrl.listFilter" 
8
  refresh="ctrl.getItems($select.search)">
9
    <div ng-if="item.valid" ng-bind-html="item.text | highlight: $select.search" ></div>
10
    <div ng-if="!item.valid">
11
        <button style="width:100%;" ng-click="ctrl.loadMore($select.search,$event)">Load more...</button>
12
    </div>
13
  </ui-select-choices>
14
  <ui-select-no-choice>
15
    <div>There is no items</div>
16
  </ui-select-no-choice>
17
</ui-select>
18
</body>
19
</html>



JavaScript
 




xxxxxxxxxx
1
30


 
1
var app = angular.module('demo', ['ngSanitize', 'ui.select']);
2

          
3
app.controller('DemoCtrl', function ($scope) {
4
  var ctr = this;
5
  ctr.itemSelected = undefined;
6

          
7

          
8
  ctr.list = [ 
9
    {text: 'First Item',valid:true},
10
      {text: 'Second Item',valid:true},
11
    {text: 'Third Item',valid:true},
12
    {text: 'Fourth Item',valid:true},
13
    {text: 'Last Item',valid:true}
14
  ];
15

          
16
  ctr.getItems = function (search){
17
      ctr.paging = 0;
18
      ctr.listFilter = ctr.list.filter(item => item.text.includes(search)).slice(ctr.paging,2);
19
      ctr.listFilter.push({text:'Load more Items',valid:false});
20
  };
21

          
22
  ctr.loadMore = function(search,$event){
23
                    $event.stopPropagation();
24
                $event.preventDefault();
25
      ctr.paging++;
26
      ctr.listFilter = ctr.list.filter(item => item.text.includes(search)).slice(0,(ctr.paging*2) +2);
27
      ctr.listFilter.push({text:'Load more Items',valid:false});
28
  };
29

          
30
});



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.

HTML JavaScript Implementation Directive (programming) Filter (software) POST (HTTP) Property (programming) Web Service

Opinions expressed by DZone contributors are their own.

Trending

  • SRE vs. DevOps
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Database Integration Tests With Spring Boot and Testcontainers
  • Reactive Programming

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: