Angularjs orderBy filter causing deletion of incorrect row

When creating a table with AngularJS, I utilized the orderBy filter. However, after implementing this filter, my delete function started removing rows unexpectedly without me clicking to delete.

Below is the code for the filter:

<tr class = "table-row isActive-{{task.active}} rowNumber-{{$index + 1}}" ng-repeat = "task in tasks | filter:search:strict | orderBy: '-priority':true">
    <td>
        <span class="delete-link">
            <input type="button" data-ng-click="removeRow($index)"/>
        </span>
    </td>
</tr>

And here is the delete function:

$scope.removeRow = function (productIndex) {
    $scope.tasks.splice(productIndex, 1);
    productIndex=0
};

It seems there may be an issue with my setup. What am I missing?

Answer №1

When working with the $index, it's important to note that it represents the index in the rendered table, not the original array. Using orderBy: does not actually sort the original array; instead, it passes an ordered copy to ng-repeat.

Here is a potential solution: You have two options to resolve this issue:

  1. You can choose to sort the original array and avoid using orderBy:

  2. Instead of deleting items based on their index, you can identify them by their id or actual entry. For example:

    $scope.removeRow = function (task) {
        $scope.tasks.splice($scope.tasks.indexOf(task), 1);
    };
    

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

Conceal the value when selecting from the datalist

In my current project, I am utilizing datalist to present a list of choices. The issue I am facing is that both the value and text are being displayed in the dropdown, as expected with datalist. However, I specifically need to hide the value and only show ...

Personalize Bootstrap 5 slider for mobile (displaying multiple items without being arranged in a single row)

Having trouble customizing the carousel layout for mobile devices. I'm trying to display all items of the carousel on mobile instead of just one. I've been experimenting with CSS and JS, specifically with the display, float, or max-width properti ...

Issues with the Dropdown Menu

I'm currently working on a dropdown menu and I've encountered two issues. When I hover over the links in the main navigation bar (such as 'about' and 'connect'), only the actual words are clickable, not the entire area tha ...

Ways to showcase JSON data with jQuery

My code displays movie data from a JSON variable and populates it on a dropdown list based on the selected city. I aim to include show timings along with other details from the JSON content. The code snippet is as follows: $(document).ready(function() ...

What could be causing certain JS files to fail to load in HTML?

I'm facing an issue with loading multiple JavaScript files in my HTML code. Only the jQuery file is loading, while the other two files are not. Could someone please help me identify the mistake I am making? When I check the network tab using the inspe ...

What is the most effective way to implement multiple ng-models within a single function?

Hello everyone! Currently, I am working on developing a database using indexed db in AngularJS. My main task is to save data into the database and I have a query - Can we utilize multiple ng-models in a single function? Let me provide you with a snippet of ...

Utilizing multiple interactive dialog boxes with content pulled from JSON source

Welcome to the Mission Control Panel! On this interactive map, you can view mission markers and access detailed information for each mission by clicking on buttons inside the popups. Everything is dynamically generated from JSON data, so there are multiple ...

Enhance User Experience with a Multi Step Form incorporating a Progress Bar, designed with jQuery and CSS3. Resolve issues with

I downloaded a similar "Multi Step Form with Progress Bar using jQuery and CSS3". I managed to display the fieldsets with divs for additional forms or images, so showing the content in the fieldset is not an issue. However, the problem lies with the actua ...

"do not make use of inline labels in bootstrap version 4

I'm working on displaying a bootstrap 4 label for a dropdown hyperlink next to some text. Here is the HTML code I have: <a href="#" class="dropdown-toggle" data-toggle="dropdown"> <img src="http://exampl ...

Concealing Components using Angular's ng-change Feature

I need help displaying or hiding an element in a form using ng-change or any other method you suggest. Here is the HTML snippet I am working with: <div ng-app ng-controller="Controller"> <select ng-model="myDropDown" ng-change="changeState( ...

Choose an element on a webpage by leveraging the power of Selenium

When working with Selenium in FireFox, I encountered an issue while trying to select specific fields on a webpage. The HTML pages I am referring to are available at the following links: Sample HTML Page, another sample page. The code snippet I used is sh ...

Breaking or wrapping lines in Visual Studio Code

While working in Visual Studio Code, I often encounter the issue of long lines extending beyond the screen edge instead of breaking and wrapping to the next line. This lack of text wrapping can be quite bothersome. I utilize a split-screen setup on my co ...

Struggling with Input Padding Across Different Browsers?

I am facing an issue with my search form that is supposed to look like this: </p> This is the sample HTML code for the search form: <form method="post" action="#" class="search-form"> <input type="text" class="nav-search-input input ...

How can we filter a complex Django query to prioritize items at the end of the list based on time out, while also ordering those with the lowest time at the beginning?

When samples are placed in the refrigerator, they need time to rest and eventually need to be checked and rotated. So far, so good. In this scenario, the sample below has the shortest time until it times out: samples = Sample.objects.order_by('end_ ...

Can the recaptcha icon be customized with a new hue?

I have been attempting to alter the captcha icon/logo in recaptcha v2. Initially, I tried to modify it using regular CSS without success. Then, I experimented with jQuery to determine if the iframe had loaded completely before proceeding with the change. ...

JavaScript: Adding elements to an array is ineffective

I'm currently working on a function to loop through an array and perform specific actions based on whether an item already exists: If the item is found, remove it If the item is not found, add it to the array However, I've encountered a proble ...

How to make an image expand to fit the screen when clicked using HTML and CSS

Is there a way to make the images on this page grow to screen size when clicked, rather than extending past the boundaries of the screen? Currently, the images enlarge to twice their original size on click. How can I ensure that they expand responsively to ...

Ways to trigger a function solely upon user input for field updates, excluding any automatic updates

In my view form.html, I have the following code: Size <select ng-model="size" ng-options...>[1..20]</select> Limiter <select ng-model="limiter">[1..20]</select> Then, in the controller form.js, I added: $scope.$watch("limiter", f ...

What is the best way to achieve a horizontally compact layout in Bootstrap by centering the content?

I am looking to center my content on the page and maintain a close proximity between items within a div. However, with Bootstrap 5, when I resize the page wider, additional space is added which pushes the items apart. Is there a way to keep them compacted ...

Background image of block element overlaps with float element

I am facing a minor issue with positioning a background image on a block element that comes after a floating element. When I float an image to the left, followed by an H1, the H1 flows behind the image, but its actual title appears next to the image due t ...