angular.js selected row in a table

I am facing an issue with a table that includes the ng-class directive in the following format:

<tbody>
      <tr style="cursor: pointer" class="clickable-row" ng-repeat="firm in device.firmwares" ng-class="{'success': firm.vulnScore<= 4,'warning' :5<= firm.vulnScore,'danger' : 8<=firm.vulnScore}">
          <td>{{firm.fileName}}</td>
          <td>{{firm.extracted}}</td>
          <td>{{firm.vulnScore}}</td>
          <td>{{firm.date}}</td>
      </tr>
</tbody>

The purpose of this directive is to color the rows based on the vulnScore value, which is working well. However, I also need to be able to select the rows. I was able to achieve this by adding the following script:

$('#firm_table').on('click', '.clickable-row', function(event) {
    $(this).addClass('bg-primary').siblings().removeClass('bg-primary');
});

While this script successfully selects rows, it only changes the text color due to the existing class styles. What I actually need is to remove the existing class (success, warning, or danger) when a row is selected, and then apply it back when another row is selected. This would be simple if there was only one class, but I'm unsure how to track and reapply the original class.

Currently, this is the output when a row is selected:

What I achieved

What I actually want to achieve looks like this:

What I want to accomplish

If anyone can provide guidance on how to achieve this, I would greatly appreciate it!

Answer №1

give this a shot.

let application = angular.module("application",[]);

application.controller("controller" , function($scope){
  $scope.rowIndex = -1;
  $scope.items = [{"name":"alice","score":2},{"name":"bob","score":4},{"name":"carol","score":5},{"name":"carol","score":7},{"name":"carol","score":5},{"name":"dave","score":10},{"name":"ellen","score":8},{"name":"frank","score":6},{"name":"grace","score":8},{"name":"harry","score":7},{"name":"carol","score":3}];
 
  $scope.selectRow = function(index){
      if(index == $scope.rowIndex)
        $scope.rowIndex = -1;
        else
          $scope.rowIndex = index;
    }
  });
.success{
  background-color:green;
  }

.warning{
   background-color:yellow;
  }

.danger{
  background-color:red;
  }
.bg-primary{
  color:white;
   background-color:blue;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="application" ng-controller="controller" class="panel-group" id="accordion">
  
  <table>
    {{selectedRow}}
     <tr ng-repeat="item in items" ng-class="{'success': item.score<= 4,'warning' :5<= item.score,'danger' : 8<=item.score,'bg-primary':rowIndex == $index }" ng-click="selectRow($index)" >
             <td>{{item.name}}</td>
             <td>{{item.score}}</td>
      </tr>
</table>
            
</div>

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

AngularJS endless scrolling from both top and bottom

I have a question regarding the functionality of ngInfiniteScroll. Currently, when using this plugin, more items are loaded as you scroll to the bottom, similar to how Facebook's timeline works. However, I am in need of a reverse functionality where i ...

Ensure that only one button from the collection is activated

One of the features on my website includes 3 blocks with buttons. These buttons trigger a change in the displayed picture when clicked. However, it is crucial to ensure that when a new track is activated, the previous button returns to its original state. ...

Limit 4 items per row in a flexbox layout with automatic width

https://i.sstatic.net/UaD6R.png I am currently using tables to display my items, but I want to switch to flexbox. However, when I tried using flexbox, the item widths were not dependent on their content. Any suggestions on how I can achieve this? Thank you ...

I'm looking to display images in a designated React component using create react app. What is the best way to accomplish

Currently in the process of revamping an older website using react. Making progress, but encountering an issue with a new component where images are not displaying. Strangely, the images appear in the main class but not within the component class. Even whe ...

Solution: The issue where the children's onChange event was not updating the parent in Ant Design was discovered to be due to the Select and Table components nested inside a Tab not changing according to the pageSize

I'm facing an issue with updating a parent element when the children's onChange event is triggered. Specifically, I have an Ant Design Select and Table inside a Tab that are not reflecting changes in the pageSize value. Although setPageSize func ...

Issue with negative z-index in modal window has not been resolved

I'm currently trying to customize the radio button using my own CSS styles, but I've encountered an issue. For some reason, setting the z-index to -1 is not working when the radio button is within a modal. Below is the code snippet that I am wor ...

Is there an alternative solution to resolving this issue with OTP verification?

Working on a verification system where users are redirected to verify their account with an OTP code after signing up. However, I encountered an issue where the code doesn't seem to work as expected and no error messages are being displayed. <?php ...

Developing a custom CSS for React using clamp and focus attributes

I'm currently working on creating an object to be used in inline style, but I'm having trouble figuring out how to correctly write `clamp` and `after`. const PhoneInputStyle = { fontSize: clamp("13px", "1.111vw", "16px"), /*this particular ...

Revise for embedded frame contents

Is it possible to modify the HTML content of an iframe within a webpage? I currently have this code snippet: <iframe src="sample.html"></iframe> I am looking for a way to edit the contents of sample.html without directly altering the HTML co ...

Do you need clarification on how the RXJS operator combineLatestAll operates?

Despite coding the provided example, I am still puzzled by the output! import { combineLatestAll, of, map } from 'rxjs'; const s1 = of(1, 2, 3).pipe( map(v1 => of(100, 101, 102, 103, 104).pipe( map(v2 => [v1,v2]) ) ), c ...

Launch target _blank within a modal instead of a new tab

I'm currently exploring ways to use vanilla JavaScript in order to display all external links - particularly those that typically open in a new tab or window - within a modal box instead. My goal is to implement a listener on external links (those no ...

Having trouble locating the "karma-mocha-reporter" plugin. Even after running npm install, the issue persists

Whenever I attempt to include plugins: [ 'karma-jasmine', 'karma-mocha-reporter' ], in my karma.config.js file I encounter this error message: C:\git\angularjs\.bin>karma start karma.config.js ...

"Upgrade your uploading experience with Fine Uploader 3.0 to easily customize PHP filenames post-upload

After uploading a file with a PHP script that changes the file name to an md5 value, I am having trouble retrieving the new file name. Fine Uploader, however, displays the original file name that was uploaded from the PC. I am looking for a way to retrieve ...

Show a string on different lines

I'm looking to format my string so that it displays on multiple lines. I attempted to use replaceAll(",", "\n"), but it didn't work as expected. Here's the code snippet: <template> <v-form> <v-container> ...

How can I set up an automatic refresh for an angularjs iframe?

This code utilizes Angularjs and is designed to be run on a localhost server. The first part of the code can be found in index.html: <iframe src="{{Url}}" width="100%" height="500"></iframe> The second part of the code is located in app.js: ...

Transforming a flat TypeScript array into a nested object structure

I'm working on implementing a user interface to offer a comprehensive overview of our LDAP branches. To achieve this, I plan to utilize Angular Materials Tree as it provides a smooth and intuitive browsing experience through all the branches (https:// ...

A 403 error is thrown by the Microsoft Graph API when attempting to delete the authentication method known as "Microsoft Authenticator"

I'm trying to remove a user's Microsoft Authenticator Authentication Method (#microsoft.graph.microsoftAuthenticatorAuthenticationMethod) from their list of authentication methods, but I keep getting a 403 Forbidden error. Below is the code snip ...

Observing modifications in the database is possible after executing the setInterval function

I am using a JavaScript function that runs every 4 seconds with setInterval. Once this function is executed for the first time, it calls an ajax request and changes a column value in the database. I want to know how I can check if this value has been succe ...

NodeJS - Issue: The procedure specified could not be located in the bcrypt_lib.node

After upgrading from Node.js 0.12.7 to 4.2.1, I encountered an error when attempting to start my server: $ node server.js C:\Users\me\documents\github\angular-express-auth\node_modules\bcrypt\node_modules\bindi ...

"Utilize Javascript to upload a picture and showcase it on your website

<!DOCTYPE html> <html> <head> <title>Unique Webpage!</title> <meta charset=utf-8 />                       <link rel="stylesheet" href="styles/customcss.css" /> <script src="j ...