Sharing object beyond ng-repeat loop

As someone who is new to AngularJs, I am currently working on implementing table filtering and deleting the correct object when the delete button is clicked.

This is my previous implementation before filtering:

$scope.rowIndex = -1;
$scope.selectRow = function(index) {
if (index == $scope.rowIndex)
  $scope.rowIndex = -1;
else
  $scope.rowIndex = index;
}
});

In my HTML:

ng-repeat="session in sessons " ng-class="{'bg-primary':rowIndex == $index }" ng-click="selectRow($index)"

After implementing filtering, I discovered that $index was not working correctly. I had to find another solution. After reading some articles, it seemed like passing the entire object to the function was the way to go. However, every example showed this being done inside the ng-repeat. Unfortunately, I couldn't do that because I have an external div for the Modal.

So how can I pass the current selected session/row of the table to the function inside the modal? {{ deleteSession(session) }}

<div id="deleteSessionModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <form>
        <div class="modal-header">
          <h4 class="modal-title">Delete Session</h4>
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body">
          <p>Are you sure you want to delete these Records?</p>
          <p class="text-warning">
            <small>This action cannot be undone.</small>
          </p>
        </div>
        <div class="modal-footer">
          <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
          <input type="submit" class="btn btn-danger" value="Delete" ng-click="deleteSession(session)">
        </div>
      </form>
    </div>
  </div>
</div>

Here is a preview of how my HTML/table looks like.

Answer №1

If you want to select a specific row, simply pass the session value to the selectRow function in this manner:

ng-repeat="session in sessions" ng-class="{'bg-primary':rowIndex == $index }" ng-click="selectRow(session)"

Within the selectRow function, you can extract the Id from the session and remove it from the sessions list.

Answer №2

Instead of using $index as a parameter for the selectRow function, consider passing session.speakerId or another unique identifier for sessions:

ng-click="selectRow(session.speakerId)"

In your controller, establish and clear the selected session:

$scope.selectedSessionSpeakerId = null;

$scope.selectRow = function(sess) {
  if (sess == $scope.selectedSessionSpeakerId)
    $scope.selectedSessionSpeakerId = null;
  else
    $scope.selectedSessionSpeakerId = sess;
}

The deleteSession function should not take any arguments. It simply checks the selected Session's unique key and removes it from the array:

$scope.deleteSession = function() {
  if($scope.selectedSessionSpeakerId) {
    let index = $scope.sessions.findIndex(function(itm) {
      return itm["speakerId"] == $scope.selectedSessionSpeakerId;
    });
    $scope.sessions.splice(index, 1);
    $scope.selectedSessionSpeakerId = null;
  }
}

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

Retrieve JSON object from dropdown menu

I need to retrieve the object name from a dropdown menu when an item is selected. How can I access the object from the event itemSelect? Thank you for your attention. View Dropdown Menu XML code: <core:FragmentDefinition xmlns="sap.m" xmlns:c ...

How can I retrieve an array of dates for the current month using jQuery?

Is there a way to create an array of dates in YYYY-MM-DD format for the current month using jQuery? For instance, if the current month is August, I am looking to have an array that looks similar to this: [2018-08-01, 2018-08-02, --------- 2018-08-31] ...

Applying CSS classes to a custom AngularJS directive: A step-by-step guide

Need to have a CSS class called tab for the nav HTML element, which will be used as a directive: <nav tab></nav> The expected interpretation is: <nav class="tab"> <a></a> <a></a> <a></a> ...

Displaying empty values as zeros

Currently, I am creating a chart using Morris js. The data for the chart comes from a server and consists of dates and values. However, there are cases where certain dates do not exist in the data set. In such situations, I want to display these non-existe ...

Discovering the presence of a value within nested arrays

let array = [["1", "2], ["3", "4"], ["5", "6"]] My goal is to check whether the digit "4" is present in the given array ...

Can an array in html be accessed using a scope variable?

I am facing a challenge with utilizing a Json array in my template file. To access specific elements of the array, I need to use a scope variable. While I can achieve this in the controller, I also require the same functionality in the HTML file. An excer ...

What is the best way to incorporate sorting functionality into my CRUD table?

Below is the backend code snippet: app.get("/sortedcustomers", (req, res) => { db.query("SELECT * FROM customer_info ORDER BY contacted", (err, result) => { if (err) { console.log(err); } else { res.send(result); } }); }); ...

Issue with peculiar circumstances regarding the JSON object and generating a chart, can you pinpoint what's amiss?

When sending data (values and dates) from a hard-coded function, everything works fine - the JSON file is populated and the chart is displayed. However, when I send data from the database, the JSON file is populated but the chart does not appear. Here is ...

The local server for running AngularJS with npm start is experiencing technical difficulties and is

As a newcomer to AngularJS, I recently embarked on creating a sample project which involved setting up a basic template store using Angular seed. Initially, I encountered some challenges with installing bower components. However, after conducting extensive ...

What is the best way to adjust an image's dimensions to fit a specific screen size without distorting it on a webpage?

Currently working on a website, and the top section has a small column on the right with an image taking up the majority of the space to the left. The problem arises when the image spills over the screen size, messing up the overall layout of the page. I d ...

Webstorm seems to be having trouble identifying Next.js

When I create a Next.js app using the command npx create-next-app my-app --use-npm Everything is successfully installed, but when using WebStorm, I noticed that it does not auto import the <Link> component from Next.js. I have to manually import it ...

Retrieving text that has been HTML-escaped from a textarea using jQuery

Within my HTML file, I have a <textarea id="mytextarea"></textarea>. Suppose a user inputs the following text: <hello>, world! How can I retrieve res = "&lt;hello&gt;, world!"; based on the user's input? The current code s ...

Having trouble with a single GET request not functioning properly on Safari due to an Authorization issue in Angular 6

I've encountered this issue in several locations, yet haven't found a clear solution. Only one GET request is showing as unauthorized (401), but when I check the debugger, everything seems to be fine and all other requests are functioning properl ...

What is the best way to add the ajax response to a specific div element?

The ajax code I'm currently using is designed to retrieve a result from another PHP file. My goal now is to append this result to the corresponding 'preview' div ID. $("#imageform").ajaxForm({ target: '#preview' }).submit(); ...

Error encountered while trying to eliminate array element

I have a project in the works that involves creating a page where users can filter suggestions and vote for their favorites. The screen is divided into 3 tabs: [Top Rated], [Newest], and [My Votes]. Upon loading the screen, a call to the database is made ...

Does a React functional component continuously re-render if it contains a child component?

For the past few days, I've been facing a performance issue in a React app (specifically React Native). The core of the problem is this: Whenever a function component Parent has another function component as its Child, the Parent will consistently re ...

Issues encountered when implementing server-sent events in a project built with Node.js and React

I've been working on implementing server-sent-events into my Node.js and React application. After doing some research and following tutorials online, I found this particular site to be very helpful and straightforward. The main objective is to have a ...

a tool for linking with the central computing api using node.js

Seeking a way to connect to a mainframe API directly from a webpage or browser. The mainframe API is currently accessible by the webserver but not directly exposed to web traffic. This particular API sends back 40,000 bytes of data per packet and utilizes ...

Displaying a div on the click of an image within a for loop using JavaScript

Currently, my website has a blog page that only showcases each individual blog post's photo and title. This is achieved through the use of a FOR loop (shown below). I am interested in implementing a feature where clicking on a blog post's photo o ...

When using .slideup(), the entire ul is removed instead of just the nested li

Encountering some issues with this particular element: I am looking to ensure that when a hidden list collapses, the code checks to see if there are any other 'active' lists already open and closes them before opening a new one. It can be messy ...