angular pagination directive malfunctioning

I have developed a custom directive for pagination that is compatible with grids throughout my website. On this particular page, there is a search feature that retrieves new data, and I am looking to update the pagination within my directive with this new set of data. Currently, I only have a link function in place.

Directive HTML

<table-pagination current-page="1" num-per-page="5" max-size="5" pagination-data="membersData" get-filtered-data="getFilteredData(membersData)" returned-paginated-data="returnedPaginatedData(data)"></table-pagination>

Directive.js

myAppModule.directive('tablePagination',
        function () {
            return {
                templateUrl: 'Scripts/app/templates/tmplPagination.html',
                restrict: 'E',
                scope: {
                    currentPage: '=',
                    numPerPage: '=',
                    maxSize: '=',
                    paginationData: '=',
                    getFilteredData: '=',
                    filteredMembersData: '&'
                },
                link: function (scope, elem, attrs) {
                    scope.getFilteredData = function () {
                        $scope.$watch('currentPage + numPerPage', function () {
                            var begin = (($scope.currentPage - 1) * $scope.numPerPage), end = begin + $scope.numPerPage;
                            $scope.filteredMembersData = scope.paginationData.slice(begin, end);
                        });
                    }
                },
                controller: [
                '$scope', function ($scope) {

                }]
            }
        });

Pagination Template

<h4>Total number of records: {{membersData.length}}</h4>
<pagination ng-model="currentPage" total-items="membersData.length" max-size="maxSize"  boundary-links="true"></pagination>

Contact Search Controller

$scope.returnedPaginatedData = function(data) {
                $scope.filteredMembersData = data;
            }

I need help passing $scope.filteredMembersData into my directive to refresh the pagination. Any suggestions on how to accomplish this?

Answer №1

My custom solution:

Custom HTML Directive

 <table-pagination current-page="1" num-per-page="5" max-size="5" source-data="membersData" grid-data="gridData"></table-pagination>

I provide settings for the grid layout and data source (entire dataset). Additionally, I pass an array (gridData).

Custom JavaScript Directive

myAppModule.directive('tablePagination',
        function () {
            return {
                templateUrl: 'Scripts/app/templates/tmplPagination.html',
                restrict: 'E',
                scope: {
                    currentPage: '=',
                    numPerPage: '=',
                    maxSize: '=',
                    sourceData: '=',
                    gridData: '='
                },
                link: function (scope, elem, attrs) { // Used for handling pagination clicks
                        scope.$watch('currentPage + numPerPage', function () {
                            var begin = ((scope.currentPage - 1) * scope.numPerPage), end = begin + scope.numPerPage;
                            scope.gridData = scope.sourceData.slice(begin, end);
                        });

                },
                controller: [
                '$scope', function ($scope) {
                    $scope.$watch('sourceData', function () { // Watch for changes in data
                        var begin = (($scope.currentPage - 1) * $scope.numPerPage), end = begin + $scope.numPerPage;
                        $scope.gridData = $scope.sourceData.slice(begin, end);
                    });

                }]
            }
        });

I have set up watches for data changes as well as current page and number of pages. These functions handle events when navigating through pagination.

Custom HTML Template

<div>
    <h4>Total records: {{sourceData.length}}</h4>
    <pagination ng-model="currentPage" total-items="sourceData.length" max-size="maxSize" boundary-links="true"></pagination> 
</div>

The above code snippet includes pagination html configuration.

This solution can be easily integrated into any grid on the website by providing data and an empty array for displaying the dataset in the grid.

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

What is causing the recursion function to return "NaN" in this scenario?

I'm trying to calculate the total sum of absolute differences between consecutive elements in an array using a function called sumAbsArr, but it seems to be returning NaN. var arr = [1, 5, 2]; var n = 3; var cur = 0; console.log(sumAbsArr(arr, n, ...

Getting an error response with a status of 200 when making a jQuery ajax call

After sending a jQuery ajax request to my express-powered node.js server, everything seemed to be in order. However, to my surprise, the response invoked the error callback instead of the success callback, despite receiving a status code of "200". It was d ...

"Include the 'unsafe' prefix at the start of the URL in AngularJS

Whenever I attempt to access app://csttree?featuretype=cst_issue&verticalid=2132321&l1=3213&l2=3242 within my app, the URL gets parsed as ==> unsafe:app://csttree?featuretype=cst_issue&verticalid=2132321&l1=3213&l2=3242 Is ...

Having trouble retrieving an element's attribute using jQuery

I am facing an issue with the following HTML code: <img src="http://localhost:82/Clone//images/hosts/Kinx_9843a.jpg" data-name="/images/hosts/K_9843a.jpg" alt=""> I am attempting to achieve the following functionality: $('body').on(&apos ...

Guidelines on maintaining an active getSelection with JavaScript

I need help figuring out how to change the font size of selected text within a div without losing the highlight/selection when I click a button. Can someone assist me in keeping the text highlighted while also resizing it upon clicking the button? ...

Storing and Retrieving Multiple Data with localStorage

I need assistance with modifying my code. I have an input field labeled "mail" and I am trying to store email addresses and corresponding IDs in local storage. The email address should be taken from the "mail" input field while the ID should increment each ...

Guide to verifying a value within a JSON object in Ionic 2

Is there a way to check the value of "no_cover" in thumbnail[0] and replace it with asset/sss.jpg in order to display on the listpage? I have attempted to include <img src="{{item.LINKS.thumbnail[0]}}"> in Listpage.html, but it only shows the thumbna ...

Eliminate specific content from within a div tag

Looking for a solution with the following HTML. <div id="textDiv"> <p> MonkeyDonkey is not a bird. Monkey is an animal. </p> </div> In this case, I am trying to delete the word "Donkey". I attempted the code below but it did no ...

AngularJS: Issues with data retrieval in parallel in $http get requests within a $.each loop

Attempting to fetch data for multiple IDs is the goal: (Simplified version, aiming to clarify) Controller: var idList = [39,40,41]; $.each(idList, function(key, value){ var dataObject = { type: "test", id: value }; var getData = DataFactor ...

Engaging with JSON data inputs

Need help! I'm attempting to fetch JSON data using AJAX and load it into a select control. However, the process seems to get stuck at "Downloading the recipes....". Any insights on what might be causing this issue? (Tried a few fixes but nothing has w ...

Learn the process of implementing an SVG icon in your Angular Material project

I'm currently attempting to incorporate a single icon into my Angular Material application. Following the documentation, I have implemented $mdIconProvider in the following manner: app.config(function($stateProvider, $urlRouterProvider, $mdIconProvid ...

The MaterialTable component is indicating that there is no property called 'tableData' on the IPerson type

Incorporated an editable attribute to my MaterialTable component. Currently looking for a way to retrieve the index of updated or deleted items within the onRowUpdate and onRowDelete methods. To replicate the issue, refer to this minimal sandbox example: ...

Is there a way to easily access the automated departure range feature of a date

I apologize if my question seems too simple, but I am having trouble understanding JavaScript. My issue pertains to a range datepicker where I want the departure picker to automatically open when the arrival is selected. Here's the JavaScript code I h ...

Switch the scroll direction in the middle of the page and then switch it back

Yesterday, while browsing online, I stumbled upon this website and was amazed by the unique scroll direction change from vertical to horizontal mid-page. I'm curious about how they managed to achieve that effect. Does anyone have insight into the pro ...

Show the meta-field mp3 and place it in a lightbox container on the portfolio page

My Current Portfolio Gallery Setup: Currently, my portfolio gallery is displayed in a masonry grid format using the JIG plugin. This grid showcases images from my custom post_type. When clicking on a thumbnail, a lightbox pops up showing the full photo. T ...

Automatically submitting a Google Form using the Google Forms API in conjunction with Google Apps Scripts

Is it possible to integrate a third-party app with Google Forms API and Google Apps Script in order to create timed quizzes that automatically submit once the time limit has been reached? ...

What is the correct way to declare this and assign values afterwards? (javascript)

I am having trouble with this code and I can't figure out how to properly declare it. var input = { container: '.slide_container', container_all: '.slide_show', slides: [] }; var $slides = $(&a ...

Object is not compliant with HTMLInputElement interface and cannot execute 'stepUp'

Currently, I am facing an issue with passing the selected value from a dropdown list of countries to a PHP file using AJAX. The goal is to take this value and then transfer it to another input field identified by the ID #tele. Here is the gist of my code: ...

What is the best way to keep a checkbox unchecked after clicking cancel?

I'm working on a bootbox script that triggers a customized alert. I need the checkbox that triggers the alert to be unchecked when the user clicks cancel. Here is the checkbox when it's checked <div class="checkbox"> <label> ...

Is it possible to pass a variable from an Axios Response in the Composition API up to the root level?

I need to fetch the headings array from an axios.get call and utilize it at the root level within my Vue component. However, when I attempt to return it, I encounter this error: ReferenceError: headings is not defined Here is the script element in my Vue3 ...