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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation
  • React Server Components (RSC): The Future of React

Trending

  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  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.

By 
Javier Santos user avatar
Javier Santos
·
Sep. 25, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
8.6K 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.

Related

  • Creating Scrolling Text With HTML, CSS, and JavaScript
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation
  • React Server Components (RSC): The Future of React

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!