Angular ng-repeat for paginating with filters

I've been grappling with implementing pagination using filters in my project. The task involves filtering a list of names and countries based on certain criteria, such as country and alphabetical range, and then generating pagination numbers. I'm facing some challenges with this task and any assistance would be greatly appreciated.

The alphabetical filter is meant to retrieve names that fall within a specific range of letters. For instance, selecting the first option [A - M] should return names starting with those letters.

Here is a link to the code: http://jsbin.com/cifowatuzu/edit?html,js,output

angular.module('app',['angular.filter'])
.controller('MainController', function($scope) { 

$scope.selectedCountry = '';
$scope.currentPage = 1;
$scope.pageSize = 3;
$scope.pages = [];

//This should store {StartFrom and To from selected Range}
$scope.selectedRange = '';

$scope.AlphabethicalRange = [
  {StartFrom: 'A', To: 'M'},
  {StartFrom: 'N', To: 'Z'}
];

$scope.Countries = [
{ Name : 'USA'},
{ Name : 'Japan'},
{ Name : 'France'},
{ Name : 'Canada'},
{ Name : 'China'},
];

$scope.People = [
{ Id: 1, Name: 'Will', Country: 'USA'},
{ Id: 2, Name: 'Ed', Country: 'USA' },
{ Id: 3, Name: 'Peter', Country: 'China'},
{ Id: 4, Name: 'John', Country: 'Japan'},
{ Id: 5, Name: 'Alex', Country: 'France'},
{ Id: 6, Name: 'Jim', Country: 'France'},
{ Id: 7, Name: 'Austin', Country: 'Italy'},
{ Id: 8, Name: 'Men', Country: 'France'},
{ Id: 9, Name: 'Zike', Country: 'Canada'},
];

$scope.numberPages = Math.ceil($scope.People.length / $scope.pageSize);

$scope.init = function () {
 for (i = 1; i < $scope.numberPages; i++) {
      $scope.pages.push(i);
   }

    };

    $scope.init();
});

Answer №1

If you're looking to customize your filtering options, I've created a custom filter that allows you to specify the range you want to filter.

Here's an example of how this custom filter can be implemented:

var app = angular.module('app', ['angular.filter']);

// Controller
app.controller('mainCtrl', function ($scope) {
  // Setup variables
});

// Filter
app.filter('rangeAlphaFilter', function() {
  return function(items, search) {
    // Filtering logic
  };
});
<!DOCTYPE html>
<html ng-app="app">

<head>
  // Scripts included
</head>

<body ng-controller="mainCtrl">
  <div>
    // Filters and data binding
  </div>
</body>
</html>

Note: Manually controlling pagination can be a lot of work. Instead, I recommend utilizing a pagination library like angularUtils-pagination. Check out this answer for a tutorial on using it effectively.

I hope this solution proves helpful to you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The function `jQuery .html('<img>')` is not functional in Firefox and Opera browsers

This particular code snippet jq("#description" + tourId).html('<b>Opis: </b> '+ data); has been tested and functions correctly in Internet Explorer, Firefox, and Opera. However, when it comes to this specific piece of code jq("#i ...

The One-Click File Upload Dilemma

I have replaced the regular upload input + submit button with an icon. My goal is to automatically start the upload process when a file is chosen by the user. However, currently nothing happens after the file selection. <form action="upload.php" method ...

"Press this button to empty the input fields and clear the text in the textarea

Is there a way to implement a button that clears/deletes the content of all textareas on a form? I've attempted using the following code: <input type="reset" value="restart" /> However, since the textareas are populated with data from the dat ...

Issue with parsing JSON object in JavaScript: Comma still present after deletion operation

I'm currently working on converting a string to a JavaScript object. I've been using the JSON.parse method successfully, but I've encountered an issue when trying to delete items from the object - it seems to be missing or not deleting the c ...

Retrieving the object ID of an Open Graph from a page's URL

Is there a way to obtain the Facebook Object ID associated with a page directly from the page itself? For instance, if I requested the Object ID 325186347604922, this is what I received: <pre> { "url": ".../ricetta-bigne_salati.htm", "type": " ...

At what point is it appropriate to instantiate a new object using Javascript?

Utilizing the mailgun-js package has been instrumental in sending emails. Initially, a new instance of mailgun needs to be created for the email sending process. Below is a simplified version of the code snippet: const mgInstance = mailgun({ apiKey: &ap ...

Design an array in PHP that is compatible with checkboxes using jQuery

Seeking assistance for a unique challenge, as I am unable to modify the PHP file and require an alternative solution. Despite searching extensively, I have been unsuccessful in finding a suitable answer. My question is: I have a form containing a hidden v ...

Radio button value failing to display accurately

I seem to be encountering a problem with the radio buttons. I am pulling all the necessary information such as labels, components, etc. from JSON and displaying it on the view. For example, when I retrieve two values for the component of a radio button fr ...

An easy guide to using validators to update the border color of form control names in Angular

I'm working on a form control and attempting to change the color when the field is invalid. I've experimented with various methods, but haven't had success so far. Here's what I've tried: <input formControlName="pe ...

Comparing two Objects in JavaScript results in automatic updates for the second Object when changes are made to the first

Can someone please assist me with a hash map issue I'm encountering in my for loop? When resetting the second object, it unintentionally alters the Map values of the previous Key in the Hash Map. Any guidance on how to prevent this behavior would be g ...

Show the login page first before allowing the comment to be submitted in PHP

My objective is to show the login page before allowing a user to submit a comment. If the user is not logged in and tries to click on the submit button, they should be redirected to the login page. However, if the user is already logged in, the comment s ...

I am attempting to separate this "for" loop in order to generate five distinct DIV elements

Hello there! I am a beginner and I am attempting to create 5 different players by using some code that I found. Here is the code I have been working with: https://codepen.io/katzkode/pen/ZbxYYG My goal is to divide the loop below into 5 separate divs for ...

Unable to retrieve the value of a selected table cell from dynamically generated HTML tables

I've gone through a plethora of Google and Stack Overflow examples To showcase my problem, I have set up a fiddle. Issue: "Upon clicking on a person's name, I want to retrieve their name from the first column." While I managed to create a click ...

Encountering a problem when trying to send an API request to the server in the latest version of

I am a newcomer to NextJS 13 and I am facing an issue with my files in the app. Despite my efforts to look for solutions online and study tutorials to keep up with the latest updates, I have not been successful in resolving the problem. The endpoint I am ...

Challenges with arrays that occur asynchronously

At the end of this code snippet where console.log(authors) is called, I noticed that the array seems to be constantly overwriting itself. Typically, in a loop like this, the async part should be outside the loop. To solve the issue of getting an array full ...

When attempting to decrypt with a password using CryptoJS, AES decryption returns an empty result

Example The code snippet below is what I am currently using: <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <div id="decrypted">Please wait...</div> Insert new note:<input type="te ...

Error: The Angular-Summernote editor is unable to upload images, as the object

I'm having difficulty understanding how to utilize the angular-summernote callback on-image-upload. Here's the code in my view: <summernote data-editable="editable" data-on-image-upload="imageUpload(files, editor)"></summernote> My ...

What is the best way to assign a data attribute value based on an element's index using jQuery?

I am dealing with multiple sibling elements that are added dynamically or manually using JS. Each element has a data attribute called "data-sec_number" with a number value that increments by one for each subsequent element down the page. Currently, I am ma ...

Combining Vue.js for handling both enter key and blur events simultaneously

I have been working on a solution where pressing the enter key or losing focus on an element will hide it and display a message. However, I am facing an issue where when I press the enter key to hide the element, it also triggers the blur event. I only wan ...

Web design that adapts to different screen sizes and the ability to

As a beginner in both responsive web design and Angular, I am currently working on creating a webpage that will retrieve data from a SQL Server table to display on the site. Can anyone suggest how I can access this data from the database? Would I require ...