There is an issue with the sorting function: [orderBy:notarray]. The expected input was an array

Looking to incorporate pagination functionality from this source: http://jsfiddle.net/SAWsA/11/

[
  {
    "name": "Micro biology",
    "id": "2747c7ecdbf85700bde15901cf961998",
    "category": "Other",
    "type": "Mandatory - No Certification",
    "categoryid": "808ff269db7cd700bde15901cf9619f3"
  },
  {
    "name": "Java Fundamentals",
    "id": "5475f2a0dbf85700bde15901cf961964",
    "category": "IT",
    "type": "Mandatory - No Certification",
    "categoryid": "2b5e7e29db7cd700bde15901cf961917"
  }
]

An object like the one above is what I've been working with.

Whenever I use ng-repeat

<tr ng-repeat="skillTestsData in data.skillTests[currentPage] | orderBy:sortingOrder:reverse">

I encounter the following error:

Error: [orderBy:notarray] Expected array but received: {"name":"Micro biology","id":"2747c7ecdbf85700bde15901cf961998","category":"Other","type":"Mandatory - No Certification","categoryid":"808ff269db7cd700bde15901cf9619f3"}

Below is my HTML:

<script type="text/javascript">
    var sortingOrder = 'name';
</script>

<tr ng-repeat="skillTestsData in data.skillTests[currentPage] | orderBy:sortingOrder:reverse"> ------- </tr>

Client script snippet:

function($scope, $filter) {
  c.data.skillTests //contains object data

/*Pagination functionality begins */

// Initialization
$scope.sortingOrder = sortingOrder;
$scope.reverse = false;
$scope.filteredItems = [];
$scope.groupedItems = [];
$scope.itemsPerPage = 5;
$scope.pagedItems = [];
$scope.currentPage = 0;

// Initialize items
$scope.items = c.data.skillTests;
// Search and initialize start
var searchMatch = function (haystack, needle) {
    if (!needle) {
        return true;
    }
    return haystack.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
};
// Initialize filtered items
$scope.search = function () {
    $scope.filteredItems = $filter('filter')($scope.items, function (item) {
        for(var attr in item) {
            if (searchMatch(item[attr], $scope.query))
            return true;
        }
        return false;
    });
    // Sorting order handling
    if ($scope.sortingOrder !== '') {
        $scope.filteredItems = $filter('orderBy')($scope.filteredItems, $scope.sortingOrder, $scope.reverse);
    }
    $scope.currentPage = 0;
    // Group by pages
    $scope.groupToPages();
};
// Search and initialize end

// Calculate page in place
$scope.groupToPages = function () {
    $scope.pagedItems = [];
    for (var i = 0; i < $scope.filteredItems.length; i++) {
        if (i % $scope.itemsPerPage === 0) {
            $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)] = [ $scope.filteredItems[i] ];
            } else {
            $scope.pagedItems[Math.floor(i / $scope.itemsPerPage)].push($scope.filteredItems[i]);
        }
    }
};
$scope.range = function (start, end) {
    var ret = [];
    if (!end) {
        end = start;
        start = 0;
    }
    for (var i = start; i < end; i++) {
        ret.push(i);
    }
    return ret;
};
$scope.prevPage = function () {
    if ($scope.currentPage > 0) {
        $scope.currentPage--;
    }
};
$scope.nextPage = function () {
    if ($scope.currentPage < $scope.pagedItems.length - 1) {
        $scope.currentPage++;
    }
};
$scope.setPage = function () {
    $scope.currentPage = this.n;
};
// Process data for display
$scope.search();
// Change sorting order
$scope.sort_by = function(newSortingOrder) {
    if ($scope.sortingOrder == newSortingOrder)
    $scope.reverse = !$scope.reverse;
    $scope.sortingOrder = newSortingOrder;
    // Icon setup
    $('th i').each(function(){
        // Icon reset
        $(this).removeClass().addClass('icon-sort');
    });
    if ($scope.reverse)
    $('th.'+new_sorting_order+' i').removeClass().addClass('icon-chevron-up');
    else
    $('th.'+new_sorting_order+' i').removeClass().addClass('icon-chevron-down');
};
/*Pagination functionality ends */

}

Answer №1

Give this a shot: eliminate currentPage from an array.

<tr ng-repeat="skillTestsData in data.skillTests | orderBy:sortingOrder:reverse"> ------- </tr>

Here's another approach utilizing limit and skip with limitTo:limit:skip

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<button ng-click="next()">Next</button>
Page :  {{(skip/limit)+1}}
Limit :  {{::limit}}
<ul>
<li ng-repeat="item in items | limitTo:limit:skip">{{item}}</li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.items = [1,2,3,4,5,6,7,8,9,10];
   $scope.limit = 2;
   $scope.skip= 0;
   $scope.next = function(){
         $scope.skip= $scope.skip+$scope.limit;
    }
   
});
</script>

</body>
</html>

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 AJAX request and UPDATE query are not working as expected

Currently, I am attempting to use an UPDATE query with an AJAX call to update a player's division by sending it to the update_divisions.php file. The process involves selecting a user from one select box and choosing the desired division from another ...

Best practices for utilizing child component methods within a parent component in VUE

I am working with the ImageUpload.vue component, which is a straightforward Bootstrap modal containing a few methods. I am currently exploring the best approach to utilize one of these methods. Could it be implemented like this: const app = new Vue({ ...

The json_encode() function returned a JSON that is not valid

In my PHP file (although more complex, even with the simplest code it fails), I have: <?php $salida = array(1,2,3,4,5); echo json_encode($salida); ?> The response received is: [1,2,3,4,5] While this appears valid, it's actually not. When pas ...

When the button is clicked, redirect to a new page and submit a form using AJAX with data obtained from the click event

I have a page called 2.html where inputting an ID number results in some output being displayed. This functionality is achieved using Ajax post method, sending data along with the data parameter. I also have another page named 1.html that contains a list o ...

Try rotating a triangle in 3D using either CSS or JavaScript

I want to create a right-angle triangle that looks like this . . . . . . . . . . . . . . . . . . but I also want to add a 3D animation that will transform it into a right angle like this . . . . . ...

How to retrieve the filename from a URL using Node.js

In my Node.js script, I have the following type of link: 148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f& ...

Iterate over Observable data, add to an array, and showcase all outcomes from the array in typescript

Is there a way to iterate through the data I've subscribed to as an Observable, store it in an array, and then display the entire dataset from the array rather than just page by page? Currently, my code only shows data from each individual "page" but ...

Navigating the realm of promises within Angular

I have been working on a project where I wanted to modify a NodeJs/AngularJs tutorial to use Postgres and the Sequelize ORM instead of MongoDB. The ORM transitioned to Bluebird promises, so I had to update my code accordingly. Previously, I was fetching T ...

How to retrieve the value of a checkbox in AngularJS instead of getting a boolean value

The following code snippet shows a checkbox with the value "Athlete" and I want to retrieve that value when it is selected. <input type='checkbox' value="Athlete" ng-model="p.selected"> Currently, in the console, p.selected displays true ...

Wordpress is experiencing a recurring issue with scripts being loaded multiple times

Currently, I am attempting to load some of my scripts from CDNs such as CDNjs and Google. While the scripts are loading correctly, I have noticed a strange issue where each script seems to generate two or even three HTTP requests (for the same script). You ...

When dealing with ReactJS, utilize the onChange event for handling updates to nested

I am currently learning ReactJS and struggling with a simple logic problem (I'm not very good at JS). I have a form that includes fields for name, email, and message. @ContactUsNew = React.createClass getInitialState: -> message: @props.mess ...

Angular 2 repeatedly pushes elements into an array during ngDoCheck

I need assistance with updating my 'filelistArray' array. It is currently being populated with duplicate items whenever content is available in the 'this.uploadCopy.queue' array, which happens multiple times. However, I want to update ...

Maximize Rotation - JavaScript Rotation

Currently tackling a Codewars challenge. The task involves finding the maximum possible result after performing a rotation on a given number. This rotation is unique in that 'n' number of digits will remain fixed after each rotation, with &apos ...

Is there a proper method in AngularJS for factories to return objects?

I am facing an issue in retrieving POST data in an Angular.js project. This is the factory I am using: angular.module('mtApp').factory('getKey', function($http) { return { getData: function(data) { var key=&apos ...

"Utilize Selenium to navigate and select a menu option with a

In my dropdown menu, I am trying to use Selenium to move the mouse to the Documentation menu item and click on the App Configuration option within it. The mouse hover function is working properly, but I am unable to click on the App Configuration element. ...

Navigating Three.js coordinate systems

While working with vectors in three.js, I noticed that the axes seem to be mixed up. It's confusing because Y is the vertical axis, but X and Z appear "mirrored" causing objects to only look right when viewed upside-down. How can this issue be resolv ...

Mastering the management of various events within React Material UI components

I am working with a Material UI Switch Component and need to detect click events on it. In certain situations, I want to prevent the change event from triggering. What is the most effective way to achieve this? While I have previously used event.preventD ...

Saving Arrays through an Input Form in JavaScript

I am working with an HTML form that looks like the following: <div class="form-group"> <label for="first_name">1st Name</label> <input type="text" id="first_name" placeholder="First Name" class="form-control"/> </div> ...

What is causing this code to display an error message?

An issue has been identified in the second line. "ReferenceError: specialTrick is not defined at CoolGuy.showoff (<anonymous>:23:40) at <anonymous>:31:5 at Object.InjectedScript._evaluateOn (<anonymous>:875:140) at Object ...

Enhancing the model using Sequelize JS

Currently, I am in the process of developing a basic API using Express and Sequelize JS. Recently, I encountered an issue while attempting to update a record with req.School, but no changes seemed to occur. Despite checking the code below thoroughly, I did ...