Use Ionic to inspect a specific value within a JSON array and then add a CSS class accordingly

I've been delving into the world of the ionic framework, working on a simple app to showcase some json data. I managed to style text strings in my ionic list with no issues (story.user), but now I want to change the background color based on different value ranges. For example:

"less than 2" "between 2 and 5" "greater than 5" for the "story.val"

However, I'm clueless as to how to approach this. Can anyone provide some guidance?

I couldn't find much documentation on using inline conditional expressions...

Below is the code that successfully deals with strings, HTML, and CSS:

HTML:

<ion-content>
    <div class="list">
        <div class="item" ng-repeat="story in stories">
            {{story.val}} - {{story.place}} - {{story.end}}
            <p ng-class="{ 'user1': 'status-okay', 'user2': 'status-medium', 'user3' : 'status-risk'}[story.user]">{{story.user}}</p>
        </div>
    </div>
</ion-content>

CSS:

.status-okay {
    background-color: green;
}

.status-medium {
    background-color: yellow;
}

.status-risk {
    background-color: red;
}

Answer №1

Revise this

 <p ng-class="{ 'user1': 'status-okay', 'user2': 'status-medium', 'user3' : 'status-risk'}[story.user]">{{story.user}}</p>

with

<p ng-class="{'status-okay': story.val<2, 
               'status-medium': (story.val>=2 && story.val<=5), 
               'status-risk': story.val>5} ">
 {{story.user}}
</p> 

Explore more about ngClass here

Answer №2

If you want to incorporate expressions for ionic (angular) to be assessed inside ng-class:

ng-class="{item.num < 2: 'name-1', item.num > 2 && item.num < 5: 'name-2', item.num > 5: 'name-3'}"

Substitute name-1, name-2, and name-3 with the specific class names you desire to employ

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

Is there a method to keep the color of navigation links active even when the cursor is not directly over the element?

In my navbar, I'm facing an issue where the color of the nav links returns to white when the hover cursor leaves the element. Is there a way to keep the color of the nav links even when the cursor is not on the hover elements? To illustrate, I've ...

Calculate the total width of LI elements and apply it to the parent UL or DIV

Is there a way to calculate the Total LI width and then set the width for the parent div? Here is an example of my code: <ul> <li>dsfdsfdsfds</li> <li>dsfdsfdsfds</li> <li>dsfdsfdsfds</li> <li>dsfdsfdsfds&l ...

Tips for converting the ng-model value to uppercase in order to validate it

<div class="form-group" ng-class="{'active': field == 'country', 'has-error has-feedback' :Country.$invalid && submitted}"> <label class="col-xs-2 control-label muted" for="Country">Country</labe ...

The ng-change event in AngularJS is not being activated by IE 11

Hello everyone, I am currently working with the angularjs framework and implementing a datepicker functionality. Unfortunately, the input type date is not functioning correctly on Internet Explorer. As a workaround, I have utilized jquery and css to create ...

The functionality of the function from the controller does not seem to be effective within the Angular directive

Hey everyone! I created a form using dynamic data and in the future, I will need to compile this form, fill it with data, and submit it. Here is how I built the form: <div> <form name="pageForm"> <div> <div class="form-group" ng-repe ...

Save the information in the database using a PHP form

My attempt to save form data into a database using AJAX is not producing any success or error messages. Here is the code I am using: <form method="POST" id="add_user" name='reg' > <fieldset> <legend>Student information:-</ ...

Unpacking a JsonObject with one element while preserving the JsonObject's key with Jackson library:

Within my application, I have a Series with two fields dedicated to representing the texture and .obj file. @Entity public class Series extends Model { public File obj; public File texture; } The structure of my File class is as follows: @Entity ...

Error encountered with auto-generated keys in Angular while using Breeze and MongoDB

Using Breeze with Angular and MongoDB has been my recent project. To ensure seamless integration, I have meticulously added all the required services and scripts for Breeze to work flawlessly with Angular and MongoDB. Despite my efforts, encountering an ...

Interactive Javascript dropdown helper on change

I am currently experimenting with a JavaScript onchange method, as I am relatively new to coding. My goal is that when I select "ID Type," the input text should change to "passport," and when I select "South African ID," the input should switch to "South ...

Why does the Setter Getter class variable in Android's RecyclerView only retrieve the last object from JSON data?

I am having an issue with my Setter Getter class variable only capturing the last object from JSON. The goal is for it to store all JSON objects in the setter getter variable so that they can be transferred to a recyclerview. Below is a snippet of the cod ...

How to align text with an image as the size of the image adjusts

I'm brainstorming creative ways to handle a webpage section where the image will be swapped out with various images (with widths up to 620px) and a text caption is positioned over it. I aim for the text to adjust its absolute position based on the wid ...

Once the Json content is loaded into the array, proceed to append

I am facing an issue with my ajax function that loads thumbnails. The function is being called by another ajax function, and the parameter is retrieved from there in the loop. I have attempted to wait for the ajax call to finish using .done and .ajaxCompl ...

Setting the Minimum Width of Floating Columns in HTML and CSS

Can anyone provide some assistance? I am currently using a design that I want to modify so that the columns do not compress or shrink below a certain size. I have attempted to use "min-width" without success. Any help would be greatly appreciated. Thank y ...

Utilize Boostrap to stylishly incorporate an image within a DIV using CSS

I have a project with numerous HTML pages all containing the same elements. To make updating easier, I decided to call all images from the CSS files so that if an image needs to be changed, I only need to update the CSS file rather than each individual HTM ...

Arranging elements and buttons inside of components to create a cohesive design

My React application consists of two components: Open and Save. The Save component has a button labeled as saveButton. On the other hand, the Open component contains an openButton, as well as a stopButton and executeButton that are conditionally displayed ...

Implementing a universal font-size adjustment feature according to user preferences

Is it possible to allow users to choose the font size for the entire application, offering three options: Big, Medium, Small? If a user selects "Big", all fonts in the application will be resized globally. I have spent time researching this issue but ha ...

Having trouble getting the Grunt server to run on nitrous.io?

I set up a node environment using nitrous.io. While working in their terminal, I installed yeoman. When I attempt to run grunt server, I encounter the following error message: Fatal error: Unable to find local grunt. ...

How much does it typically cost to implement external libraries into a project?

Here are some of the most commonly used front-end web development libraries: jquery-min.js (95.9 kB) angular.min.js (108.0 kB) bootstrap.min.css (113.5 kB) bootstrap-theme.min.css (19.8 kB) bootstrap-fonts (185.7 kB) bootstrap.min.js (35.6 kB) All in al ...

Ways to access a field value in SAPUI5

As I delve into OPENUI5/SAPUI5, I'm faced with the task of accessing data for the controls I've implemented. For instance: <Label text="Amount" /> <Input id="inputAmount" value="{Amount}" /> <Text id="lblCurrency" text="USD" > ...

Creating a custom C# class that corresponds with a JSON attribute mapping

Struggling to map a JSON response to a C# class using RestSharp. Most fields are mapping correctly, but attributes (fields starting with @ or $) are coming up as null. Not sure if there's a special way to handle these cases. The JSON response looks l ...