Customize the pagination template in ui-bootstrap to better suit your needs

I have customized the pagination template in AngularJS to display pagination. Here is the modified HTML code:

<ul uib-pagination ng-model="source_pagination.current" template-url="pagination.html" total-items="source_pagination.total_pages" items-per-page="1" max-size="source_pagination.max_items" class="pagination-sm" force-ellipses="true" direction-links="false" ng-change="source_page_changed()"></ul>
...
       <script id="pagination.html" type="text/ng-template">
            <ul class="pagination">
                <li ng-if="boundaryLinks" ng-class="{disabled: noPrevious()}">
                    <a href ng-click="selectPage(1)" title="First Page">
                        <span class="glyphicon glyphicon-fast-backward"></span>
                    </a>
                </li>
                <li ng-if="directionLinks" ng-class="{disabled: noPrevious()}">
                    <a href ng-click="selectPage(page - 1)" title="Previous Page">
                        <span class="glyphicon glyphicon-step-backward"></span>
                    </a>
                </li>
                <li ng-repeat="page in pages track by $index" ng-class="{active: page.active}">
                    <a href ng-click="selectPage(page.number)" ng-class="{highlight: ShouldHighlightPage(page.number)}">
                        {{page.text}}
                    </a>
                </li>
                <li ng-if="directionLinks" ng-class="{disabled: noNext()}"><a href ng-click="selectPage(page + 1)" title="Next Page"><span class="glyphicon glyphicon-step-forward"></span></a></li>
                <li ng-if="boundaryLinks" ng-class="{disabled: noNext()}"><a href ng-click="$scope.changePage('last')" title="Last Page"><span class="glyphicon glyphicon-fast-forward"></span> </a></li>
            </ul>
        </script>

I made changes in this part:

<li ng-repeat="page in pages track by $index" ng-class="{active: page.active}">
    <a href ng-click="selectPage(page.number)" ng-class="{highlight: ShouldHighlightPage(page.number)}">
                            {{page.text}}
    </a>
</li>

In addition, I included the class-condition

{highlight: ShouldHighlightPage(page.number)}
. The intention is for this code to invoke the function ShouldHighlightPage(pageNum) found in the controller:

$scope.ShouldHighlightPage = function (pageNum)
{
    return true;
}

Despite implementing this, the function is not being executed (verified by adding a breakpoint in the function). Consequently, all the pages are displayed without the highlight class.

What could be the issue here?

Appreciate your insight.

Answer №1

We have identified the issue at hand.

The uib-pagination directive establishes an isolated scope. You can view the details here: https://github.com/angular-ui/bootstrap/blob/2.1.3/src/pagination/pagination.js#L126

Referencing the documentation:

Scope

{...} (an object hash): A new "isolate" scope is created for the directive's element. The 'isolate' scope differs from normal scope in that it does not prototypically inherit from its parent scope. This is useful when creating reusable components, which should not accidentally read or modify data in the parent scope.

This implies that the ShouldHighlightPage method will not be accessible within the scope of your customized template.

One immediate but not recommended solution is to define your method in the $rootScope:

$rootScope.ShouldHighlightPage = function (pageNum)
{
    return true;
}

To resolve this issue, create a custom directive:

myApp.directive('uibCustomPagination', function () {
    return {
        restrict: 'A',
        require: 'uibPagination',
        link: function ($scope, $element, $attr, uibPaginationCtrl) {
            uibPaginationCtrl.ShouldHighlightPage = function (pageNum) {
                return true;
            };
        }
    }
});

This directive essentially injects your method into the controller of the uib-pagination directive, making it accessible in your template.

Simply apply the uib-custom-pagination directive to your ul element:

<ul uib-pagination uib-custom-pagination ng-model="source_pagination.current"
    template-url="pagination.html" total-items="source_pagination.total_pages"
    items-per-page="1" max-size="source_pagination.max_items" class="pagination-sm"
    force-ellipses="true" direction-links="false" ng-change="source_page_changed()"></ul>

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

Can arrays be utilized as keys for Objects in JavaScript?

I am looking to create a dictionary in Javascript that can return the same result with different keys. Here is an example of what I have tried: var dictionary = { [CHW,CW] : { //CHW and CW refer to the same thing [F ...

What is the process of combining JS functions with PHP echo in code?

I am currently working on creating a popout menu for an array of values that are being displayed from the database. The goal is to show the corresponding popout menu when the svg is clicked, but I am running into an issue where it only works for the first ...

What is the best way to style only numbers using CSS in jQuery?

Here is some HTML code that needs styling: <tr> <td>Test</td> <td>324</td> </tr> The default CSS setting is text-align:left However, I am looking to apply text-align:center to any <td> element that contai ...

Text Separation Within a Singular JSON Object

Currently, I am offering my assistance as a volunteer to develop a fast AngularJS App for a non-profit organization. Although I have successfully set up Angular, the main issue I am encountering has to do with parsing JSON data. The JSON object that I am ...

Utilizing ngRepeat within a custom directive's template

Check out this plunker to see my current progress. angular.module('app', function(){ }) .run(function($rootScope){ $rootScope.values = [1,2,3,4]; }) .directive('outer', function(){ return { restrict:'E', scope:{ ...

Accessing a variable from another HTML using JavaScript

I'm currently attempting to access a variable from another HTML file using JS. Specifically, I have a file (file1.htm) that opens a dialog box, and I want to send the information of the selected file to another file (file2.htm) in order to modify a v ...

DataTables: Personalized Response Management

Recently I delved into AngularJS and DataTables and had a query regarding customizing the response expected by DataTables. The typical format DataTables expects looks like this: { "draw": 1, "recordsTotal": 57, "recordsFiltered": 5, "data" ...

What methods can be used to defer the visibility of a block in HTML?

Is there a way to reveal a block of text (h4) after a delay once the page has finished loading? Would it be necessary to utilize setTimeout for this purpose? ...

Change the code from a for-loop to use the Array#map method

I have been working on a simple JavaScript function and attempting to convert the code from using a for-loop to Array#map. I'm sharing my code in the following fiddle as I am currently learning about array map: http://jsfiddle.net/newtdms2/. function ...

Encountering a 404 error when utilizing ngx-monaco-editor within an Angular application

I have been encountering an issue while attempting to utilize the editor within my Angular 8 application. Despite researching similar errors on Stack Overflow and GitHub discussions, I haven't found a solution yet. Here's how my angular.json asse ...

Can someone guide me on how to personalize a marker icon in Quasar while utilizing Vue2-Leaflet for mapping?

I'm facing an issue with displaying an icon marker image in my Vue2-Leaflet and Quasar project. Instead of the desired image, I am seeing a broken image icon and encountering a 404 error in the console. Despite researching various solutions, I was abl ...

Setting up web pack with flag-icon-css integration

I have successfully set up a webpack project using https://github.com/teroauralinna/webpack-guide. Now, I am looking to integrate https://github.com/lipis/flag-icon-css into my project. To do so, I followed these steps: npm install flag-icon-css --save T ...

Leverage JavaScript to run a snippet of PHP code directly (without utilizing a separate PHP file)

I am looking for a way to integrate PHP into my web page using JavaScript and AJAX. I want the PHP file to be included and executed as if it is part of the native page, allowing me to utilize features like GET requests. <div id="firstAjaxDiv">Defaul ...

Function that yields promise result

I need help figuring out how to make this recursive function return a promise value. I've attempted various approaches, but they all resulted in the search variable ending up as undefined. public search(message: Message) { let searchResult: strin ...

Decrease the jQuery version for a particular view exclusively

Imagine a scenario where I am utilizing jQuery version 1.10 in _Layout.cshtml, but need to downgrade to 1.7.2 for certain pages. Is there a way to achieve this without altering the Layout for these specific views? Can this be done at all? :) ...

Numerous applications of a singular shop within a single webpage

I have a store that uses a fetch function to retrieve graph data from my server using the asyncAction method provided by mobx-utils. The code for the store looks like this: class GraphStore { @observable public loading: boolean; @observable ...

Insert the object into a designated location within a multi-dimensional array

I've integrated a tree view into my Angular project and I'm looking to add an object to a specific position within an array. array const TREE_DATA: TreeNode[] = [{"name":"Demo","id":"demo_1","children ...

Tips for stopping Angular from rendering a directive that is producing incorrect results after an error is thrown

My directives have a template and a compile function that modifies the template. Occasionally, Angular fails to recognize jQuery (and defaults to using jqLite), causing my compile to encounter errors. Despite this, Angular continues to render the directive ...

What sets apart async.map from the async library and Promise.map from bluebird?

Allow me to clarify: My goal is to utilize async/await functionality with either the async or bluebird library. I'm puzzled as to why this code snippet works as expected: const promises = Promise.map (someArray, async item => { ...

How can I create a form in Django where selecting a drop-down option triggers an automatic submission?

I'm attempting to create a form that can pre-fill fields based on user selection from a drop-down menu. To kick things off, I want the form to automatically submit when an option is chosen from the drop-down list. Here is the code snippet I have been ...