Sorting of array in AngularJS ngRepeat may result in changing the length of the array

Link to JSFiddle: http://jsfiddle.net/WdVDh/101/.

When updating the sorting option for an ng-repeat list, I noticed that the array length changes and in my local version, all items disappear.

My code involves filtering and sorting, with the ability to filter out employees with a completed 'leaveDate' property using a checkbox linked to a 'showLeavers' $filter directive.

In my local version, when the checkbox is unchecked and the second or third sorting option is chosen, the array gets cleared and nothing shows up in the ng-repeat list. However, when the checkbox is checked or the first or fourth sorting options are used, the list displays correctly.

There seems to be a problem in the code, possibly in the filter directive, as I can replicate a similar issue as shown in the JSFiddle link above.


HTML:

<md-list-item ng-repeat="employee in employees | orderBy: sortEmployees | filter: searchEmployees | showLeavers:showLeaver">

<md-input-container class="col-xs-12 col-sm-6 col-md-3">

    <label>Sort by</label>

    <md-select ng-model="allEmployees.sortEmployees">

        <md-option selected value="surname">Surname - A to Z</md-option>

        <md-option value="-surname">Surname - Z to A</md-option>

        <md-option value="forename">Forename - A to Z</md-option>

        <md-option value="-forename">Forename - Z to A</md-option>

    </md-select>

</md-input-container>

<md-checkbox ng-model="showLeaver">Show Leavers?</md-checkbox>

Filter:

app.filter('showLeavers', function() {
return function (employees, showLeaver) {
    var matches = [];

    angular.forEach(employees, function(value) {
        matches.push(value);

        if (value.leaveDate && !showLeaver) {
            matches.splice(value);
        }
        return matches;
    }
})

Answer №1

It is important to update your use of the .splice() function by specifying the index of the element and the number of elements to remove:

matches.splice(matches.indexOf(value), 1);

Alternatively, you can opt for using .filter() in this manner:

var matches = employees.filter(match => match.leaveDate.length <= 0 || showLeaver);

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

Creating a blank webpage after including a component tag for an Angular form

I encountered an issue while developing an Angular form. It seems that using the app-name-editor tag causes my entire HTML page to go blank, and the form does not display. However, removing the tag restores the webpage's functionality. This leads me t ...

A guide on invoking JQuery functions

Exploring the various methods of applying a JQuery function to a variable has been on my mind lately. For example, I am familiar with this approach: $.DoThis(variable); However, is there a way to invoke it at the end similar to traditional Javascript f ...

What could be causing the error message "CSRF token missing or incorrect" to appear?

I am facing an issue with returning a value from a View function in Django. This particular function is called from a JavaScript code using Ajax, but I'm encountering an error that says 'Forbidden (CSRF token missing or incorrect)'. JavaScr ...

How to use addclass in AngularJS?

Can someone assist me with converting this to jQuery? $('.toggle').on('click', function() { $('.container').addClass('active'); }); $('.close').on('click', function() { $('.container&ap ...

What could be causing my HTML button to malfunction when attempting to navigate to a different section of the webpage?

Just starting out and developing my website. My hosting provider is IPage, but I'm running into an issue. When I click on a button to switch to another section of the site, it's not working as expected. Here's the code snippet: <button on ...

Determining if the device orientation matches the given coordinates through a logical process

Exploring the capabilities of browser API's related to geolocation and device orientation. I'm wondering, if I have information on someone's orientation (like a compass angle from 0 to 360 degrees), is it feasible to determine if they are f ...

Solve problems with limitations on ng2-dnd drop zones

I successfully integrated drag and drop capabilities into my Angular 4 application using the ng2-dnd library. Within my application, I have containers that can be sorted, as well as individual items within each container that can also be sorted. My goal i ...

Communicating with Socket.io using the emit function in a separate Node.js module

I've been trying to make this work for the past day, but I could really use some assistance as I haven't had much luck figuring it out on my own. App Objective: My application is designed to take a user's username and password, initiate a m ...

Stable header that jumps to the top when scrolled

I have implemented the JavaScript code below to set the header to a fixed position when it reaches the top of the page so that it remains visible while the user scrolls. Everything appears to be functional, but the header movement is abrupt and not smooth. ...

Retrieve and manage information from a database using node.js and express

I am currently working on developing a jQuery mobile application and have set up a Linux server with node.js and express. The authentication process seems to be working properly, as well as the connection to another database server. However, I am facing a ...

Looking to remove a row with php, html, and ajax?

Trying to remove a row from an HTML table has been my latest challenge while working with Materialize CSS. Here is what I have so far, where the ID corresponds to the employee ID: https://i.stack.imgur.com/rjwba.png Below is the code snippet: <?php ...

Utilizing Kendo for Dynamic HTML Element Connection

Currently, I am facing a situation where I have three HTML elements. The first is a textbox labeled shipToAddress, the second is another textbox called deliverToAddress, and finally, there is a checkbox named sameAsShipToAddress. In the background, there ...

What is the method of aligning content to the left side in a slick slider?

My slider is set up to display three elements, but I'm having trouble aligning one or two elements to the left instead of centering them. jQuery(function () { jQuery('.slider-blog').slick({ arrows: false, dots: true, ...

The system cannot locate the module: Unable to find '@reactchartjs/react-chart-2.js'

I've been working on implementing this chart using the npm module called react-chartjs-2. I followed these steps to install the module: Ran the command: npm install --save react-chartjs-2 chart.js As a result, my package.json file now looks like th ...

Enhance local rotation of objects in Three.js

I am working on creating a spaceship-like object with controls, and I have learned that I need to use quaternions to achieve this. To start, I am attempting to make a cube rotate to the left when the A key is pressed. Here is the code I have written for th ...

What is the best way to hide the jQuery modal I created?

Hello everyone! Currently, I am working on coding a simple JS modal that can be opened and closed smoothly. The issue I am facing is related to adding a click function to (.black-overlay) in order to fade out everything and close the modal. <div class ...

How can I easily send typed text to a server using AngularJS?

I know how to accomplish this in JQuery by checking the event keypress, but how can I do it in Angular JS? I want to make an AJAX request to the server for searching data via ElasticSearch. Do I need to use a timeout for typing text event? Is the example ...

NativeScript element isn't showing up

Being relatively new to application development with NativeScript, I find myself in a situation where I need assistance in finding a solution. Drawing from my experience with PHP, I am now looking to create a template for each "page" of the application. Th ...

Exploring the world of unit testing with Jest in Strapi version 4

In my quest to conduct unit tests using Jest for the recently released version 4 of Strapi, I have encountered some challenges. The previous guide for unit testing no longer functions as expected following the latest documentation updates. Despite my effor ...

Connect my Vue.js model data to the object that will be used for submission

I need help figuring out how to bind data from my input field into my Vue.js model. I've tried putting the UnitName inside the model but it's not working. Can someone provide some guidance on how to achieve this? new Vue({ el: "#app", da ...