Applying ng-class based on conditions to icons within table cells

In my table, I am using ng-repeat to bind cells with data. One cell contains edit, save, and delete icons. When an order is posted, the delete/save icons should be disabled and displayed in a different color. While I can disable the click event of the icons, I am struggling to change the CSS class to reflect this change visually. Currently, the save and delete actions are disabled, but I would like them to appear light gray or in another distinct color.

Here is the HTML:

<tbody>
    <tr ng-repeat="order in vm.orders track by $index"  ng-dblclick="vm.editOrder(order)" style="cursor:pointer" ng-class="{'class':order.invoiceStatus, 'disabled-order': !order.invoiceStatus}">                                            
        <td ng-bind="(order.dtInvoiced | date:'MM/dd/yyyy' )"></td>                                                
        <td ng-bind="order.invoiceNumber"></td>
        <td ng-bind="order.invoiceItems.mdbsPoNumber"></td>
        <td align="center" ng-bind="order.lines"></td>
        <td ng-bind="(order.total | number:2)"></td>
        <td align="center" ng-bind="order.carrier"></td>                                                
        <td>
            <a ng-click="vm.editOrder(order)"><i class="fa fa-pencil fa-2x"></i></a>
            <a ng-click="order.disabledToggle || vm.saveOrder(order)"><i class="fa fa-check fa-2x"></i></a>
            <a ng-click="order.disabledToggle || vm.removeOrder(order)"><i class="fa fa-trash fa-2x link-icon"></i></a>
        </td>
    </tr>
</tbody>

Below is the JS code to handle disabling the save and delete icons based on the invoice status:

function orderItems() {
    var orders;
    if (vm.Criteria != null) {
        orderService.getOrderData(vm.Criteria)
        .then(function (result) {
            vm.data = result.data;
            orders = vm.data;
            for (var i = 0; i < vm.data.length; i++) {
                orders[i].disabledToggle = false;
                if (orders[i].invoiceStatus == "RCV") {
                    orders[i].disabledToggle = true;                    
                }
                else {
                    orders[i].disabledToggle = false;
                }
                vm.orders.push(orders[i]);                
            }

        });
    }
}

I have also attempted to add CSS classes dynamically within the icon element:

<i ng-class="order.invoiceStatus = order.disabledToggle ? 'disabled-order class' : 'class' "></i>

Answer №1

If you want to trigger a class based on the value of a variable in AngularJS, you can use ng-class. The class will be added to the element if the variable evaluates to true:

<a ng-click="order.disabledToggle || vm.saveOrder(order)"><i ng-class="{ 'cssCheckDisabled': jsCheckDisabled } class="fa fa-check fa-2x"></i></a>

In this example, we are checking the value of jsCheckDisabled. When it is true, the cssCheckDisabled class will be applied to the element.

To make the class do what you want, simply define it in your CSS:

cssCheckDisabled {
 color: red;
}

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

Adjust the size of the wrapper/mask as the browser is resized

Is there a way to adjust the size of my wrapper and mask when the browser is resized? Currently, the mask stops once it's loaded, causing the content to be cut off when scrolling. You can view an example on this site. $(document).ready(function() { ...

Attempting to generate a JSON array using JavaScript

As I embark on my journey to learn JSON, I've encountered an issue while attempting to create an array of object Messages. The declaration seems to be error-free, but when I try to access it using the code snippet below: serverReply2.Mesages[0].Date, ...

Enhancing Alerts in Bootstrap 5.1+: Tweaking Text, Background, and Border Colors for Alert-Warning Messages using custom _variables.scss

Incorporating a custom _variables.scss file into my codebase is crucial for my project, and the way I import it is as follows: @import url('https://fonts.googleapis.com/css?family=Nunito'); @import 'variables'; @import 'bootstrap/s ...

Building a React.js application and fetching information with Ajax

In my quest to create a high-speed React.js application that functions as a game, I find myself in need of displaying real-time data. However, the traditional method of loading this data from the server using Ajax doesn't quite align with the reactive ...

What sets npm install apart from manual installation?

I've been exploring requirejs recently. I'm trying to decide between installing it using npm install requirejs or manually downloading it from the website. Are there any differences between the two methods? Are there any advantages or disadvantag ...

jQuery random generator for creating two-dimensional arrays

Why do all rows always have the same numbers? They should be populated with random numbers. Also, how can I fix this issue? I remember seeing a solution here before but now I can't seem to locate it. var mapSizex = 5; var mapSizey = 6; var mapArray ...

Troubleshooting the 'App Already Bootstrapped with this Element' Error in AngularJS

When I try to load my AngularJS app, I encounter the following error: Uncaught Error: [ng:btstrpd] App Already Bootstrapped with this Element '<html lang="en" ng-app="app" class="ng-scope">' I have only placed ng-app once in the html elem ...

Picking an art exhibit to visit

Can you provide recommendations for a gallery suitable for my web project? The project utilizes: jQuery Bootstrap An ideal gallery would be based on jQuery. Here are the requirements and preferences: Compact CSS and JavaScript files. Utilization of the ...

AngularJS carousel slider not functioning properly due to issues with AJAX integration

When I initially add a static array, the slider works fine. However, when I try to load data using AJAX like this: $http.get(baseurl + 'controller/getimagesData/').success(function(data) { $scope.bestDeals = data; ...

What could be causing site container not to respond to height:auto?

I have encountered an issue while developing a website using absolute height values. I am puzzled as to why the height:auto property is not working for me. Can anyone provide some insight on this? HTML Structure <div id="site-content"> <div id=" ...

Calculating the number of duplicate lines in a file with node.js

I am currently working on a project where I need to read a large .csv file line by line, extract the first column (which contains country names), and then count the number of duplicates for each country. For example, if the file contains: USA UK USA The ...

Invoke Selenium using JavaScript

Imagine I have this (fictional) JavaScript snippet: asynchronousOperation.addEventListener("completed", function (event) { if (event.property == "required value") tell Selenium we are good; else tell Selenium the test failed; }); ...

several different objects within the rightIconButton of a ListItem component in MaterialUI

I am currently working on a project where I need to add multiple elements to the rightIconButton of a ListItem. The tools I am using are Material UI v0.20 and [email protected] <ListItem rightIconButton={ <span> ...

Issue with passing reactive property to component in Vue 3 application

I am currently working on a Vue 3 application and I am in the process of setting up a store for state management. Within this application, I have several important files that play different roles: app.vue component.vue main.js store.js These files contai ...

What is the best way to set all "read" values to true for the child messages within the Firebase database?

https://i.sstatic.net/oukpl.png How can I set the read property of all children of a message to true in JavaScript with a single command? I attempted using let ref = yield firebase.database().ref(groups/${groupId}/messages).update({read:true}) but it did ...

Having trouble with Angular JS $scope.$apply due to an interpolation error displaying "TypeError: Converting circular structure to JSON"?

I have created a specialized angular directive shown below: dirs.directive('sectionInfo', function(){ return { restrict: 'E', templateUrl: 'partials/section-home.html', transclude: true, co ...

I have a JavaScript code that smoothly scrolls to different id's on a webpage, but I need assistance in adding an exception for a particular id

I'm looking to incorporate smooth scrolling for all hash links in my JavaScript. However, I need the id=nav and its resulting hash link, /#nav, to be exempt from this functionality as it interferes with the menu responsiveness. Can someone provide gui ...

Despite returning an "OK" status, the jQuery Ajax Codebehind Post fails to function properly

Attempting to call a function in ASP.NET with jQuery Ajax: var params = "{'name':" + "\"" + name + "\"}"; $.ajax({ type: "POST", url: "CreateTopic.aspx/CreateNewTopic", data: params, ...

I am currently engaged in a Portfolio project, where my objective is to ensure that all images are optimized for responsiveness

While Bootstrap ensures that relative images are responsive, absolute images may not be. This can cause alignment issues when the browser window is resized on mobile devices. .title-img { position: relative; width: 100%; height: 100%; } .skill-b ...

What is causing Bxslider to malfunction?

I need help troubleshooting an issue with my HTML code. The slideshow I'm trying to create isn't working as expected; all images are displaying vertically and I'm getting an error message saying that bxslider() is not a function. Can anyone ...