Amid the COVID-19 outbreak, Folio3 aims to help clinics serve patients with telemedicine effectively.

Angular JS Grid With Paging,Sorting,Filtering

Angular JS Grid With Paging,Sorting,Filtering
COMMENTS (0)
Tweet

Hey guys,

In the previous post we have learned How to create User Registration & Login application.

This is a follow up to my earlier posts on Angular JS. In this tutorial I’ll show you how to make your lists/tables rich with features like Sorting, Filtering, and Pagination.

We use lists and tables to display data in numerous Angular JS applications. But sometimes, you need more than just displaying data, like when you want to present data in a manageable way. That’s where you can put the above mentioned features to the best use.

We’ll cover each of these features one at a time so you have a better understanding of them.

Tutorial

You can also download a zipped version of the source code used from this link 

Live Demo

A video demo of this tutorial is available here Live Demo

Step 1: Getting started

As a first step, we’ll set up a new Angular JS application called ‘GridApp’ with a basic controller called ‘GridController’ which we’ll be using throughout this tutorial. I’ve included the following files in the index.html file:

  • Anglular.min.js (version: 1.5.5)
  • App.js (Our app initialization file)
  • Grid.js (This will be our controller file)
  • Bootstrap.min.css (version: 3.3.6)
  • Main.css (For any custom styles)

Your index.html should look similar to the code below.

 

Our app.js file will only contains app initialization code, which is:

 

Step 2: Displaying data

Now that we’ve set up our application, let’s move to adding and displaying some data. In this tutorial, we’ll be using random JSON data that I’ll be adding to the ‘grid.js’ file. Alternatively, you can also use an Ajax request (using $http) to fetch the data to display.

 

Next, we’ll use the ng-repeat directive to display this data in a table format. I’ve added some table styles in the css (as shown below). You should do the same.

 

Now, if you run your application in a browser, you’ll see your data being displayed nicely in a table (depending on how you styled it).

Step 3: Filtering data

Filtering data in Angular JS is also pretty easy. Since Angular JS comes with a number of amazing filters that you can use to transform data. Let’s look at these in detail below.

Before we add any filtering code though, let’s first add an input search field in our index.html file, which we’ll be binding to our controller data using the ng-model directive. To do so, just add the following markup above the markup for your “grid” div (as shown below).

 

You’ll be surprised to see how easily you can filter your data using this method. In the previous step, we used the ng-repeat directive to iterate through our data and display it. In this case, we’ll apply our filter to sort the data. Update the above markup as depicted below.

ng-repeat=”person in people | filter: queryText”

What we’ve done in the code snippet above, is that we’ve applied a data filter (yes, it’s a data filter that is named filter). Filters in Angular JS can be applied by using the | (pipe) character followed by the filter name. The filter used in this case, searches through our data and returns a subset of matched items. The beauty of Angular JS data-binding is that whenever you type anything in the search field, the view will automatically be updated with the new results. No bloated DOM manipulation code is necessary for that in Angular JS.

To learn more about Angular JS filters, head on over to this link. https://code.angularjs.org/1.5.5/docs/guide/filter

Step 4: Sorting data

Now, let’s look at data sorting in Angular JS. Angular JS also offers a pre-defined filter that can help you order and sort data pretty easily. This filter takes just two arguments:

  • Order key
  • Reverse sort (can be either true or false; true in case of descending order)

Let’s look at how we can use this sorting filter in Angular JS. Before we apply this filter though, let’s add some basic sort functionality in our controller (grid.js), as shown below.

 

In the above code snippet, we’ve added default sorting configuration and a function that takes a key as an argument. This function also toggles the sorting order if it’s called twice with the same key passed. Now you just need to apply the orderByfilter and update your table heading markup to call this function upon ng-click (as shown below).

 

Please note that in above code snippet, I’ve added glyphicons to indicate the current sort order. Now, just run the application in a browser to see sorting in action.

Step 5: Paging

The last and final chapter in this tutorial is about using pagination in Angular JS. You might be thinking that there’s a built-in pagination control provided by Angular JS that can speed thing ups in this area too, similar to what we saw for filters and sorting, etc.. Sadly no, this is not the case when it comes to pagination. Angular JS doesn’t come with pagination support out-of-the-box. So what do you do? Do you write the whole pagination logic by yourself? The answer is no, you don’t need to reinvent the wheel since there are quite a few libraries available for it, unless you’re planning on inventing a very special wheel for pagination.

The pagination library I’ll be is the dirPaginate. The reason I chose this library is because it offers an excellent set of features for pagination in Angular JS, and also because it’s really easy to add in any Angular application. Let’s look at this library in detail.
The first thing you need to do is add the dirPaginate library to our project. You can install it with Bower, or npm, or just download it manually from this link.

Bower: bower install angular-utils-pagination

npm: npm install angular-utils-pagination

Next, include the link to the dirPagination.js file to your index.html file. Then all you have to do is add the ‘angularUtils.directives.dirPagination’ module in the controller file. The updated file should appear as shown below:

 

Also, make sure to add a scope variable for the number of items you want to display per page (as depicted below).

 

To make use of dirPaginate, you have to replace your ng-repeat directive with the dir-paginate directive. You don’t have to worry about this breaking any current functionality though. All the filters you’ve used will work just fine.

Another thing you need to add here is a dirPaginate filter, which will restrict the items shown per page. So update your markup to:

 

The thing to keep in mind here is that the itemsPerPagefilter should be applied after all other filters.

Once you’ve done all that, there’s just one more thing left to add, and that is the pagination links. To include those, add the following code right after your grid:

 

This will automatically display/hide the pagination links based on your data. Please do remember that the pagination-idmust match, in order for the links and pagination to work.

You can read more about the dirPaginate library at the link below. https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination

And that’s it. If you’ve performed the steps above correct, the final screenshot of the application will look like this.

angularjs

Wrapping up

In this tutorial, I showed you how to display data in a tabular format and implement filtering, sorting, and paging functionality in Angular JS. These features provide ease of use when working with Angular JS, especially when you are working with a large quantity of tabular data. I would recommend that you go through all the links given in this post, to get a better understanding of all these features.

In the next tutorial we will learn How to Upload File in Angular JS

 

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Latest From Our Blog

Why the Daily Scrum is NOT a Status Meeting?

February 3, 2020
I am here today to debunk, The Myth “The Daily Scrum is the Status Meeting”. This is a key misconception in the industry and often resu...
Read more

A Complete Guide to Custom Caching in Magento 2...

November 29, 2019
In this blog, I will explain how to create your own cache in Magento 2, and how to read and write data from that custom cache. I hope after reading...
Read more

React Hooks

September 3, 2019
React is a javascript framework for building interactive client side interfaces along with the integration with back-end server. React considers th...
Read more