How to display elements inside an arrayList with two CSS style? - css

I'm writing a code where there's an arrayList having two names in it i.e.Bob & Steve and i want to display them such that if Bob is displayed it should be green in color and if Steve is displayed it should be REd in color.
Component.CSS
.Bob{
font-weight:bold;
color:green;
}
.Steve{
color:red;
}
Component.HTML
<div class="container">
<div class="row" *ngFor="let st of Names;">
<div class="col-2">
<p class="Bob">{{st}}</p>
</div>
<div class="col-2">
<p class="Steve">{{st}}</p>
</div>
</div>
</div>
in Component.Ts
Names:string[]=['Bob','Bob','Steve','Bob','Steve']; in Component.Ts

You can provide class based on condition .
Modify your code like below :
<div class="container">
<div class="row" *ngFor="let st of Names;">
<div class="col-2">
<p [ngClass]="(st=='Bob')?'Bob':'Steve'">{{st}}</p>
</div>
</div>
</div>
Here is the working example :
Working Stackblitz Example

When we have a large number of elements and we can showed it with different background we can adopt several approaches.
Has an array of styles/background and use the index
colors=['red','yellow','green'...]
<div *ngFor="let item of items;let index=i>
<div [style.background-color]="colors[i]">item.data</div>
</div>
The background was a "property" of "items"
items=[{data:...,color:'Red'},{data:...,color:'yellow'},...]
<div *ngFor="let item of items;let index=i>
<div [style.background-color]="item.background">item.data</div>
</div>
We can use
[style.css_property]="value"
//or
[className]="class"
//or
[ngClass]="{'class':condition}"
In your case, Aman, I think that it's better that your "items" has a property "class" that was, e.g.
{data:...,class:'bold-true green-true'}
So, when you want add a item, you can make a function
getClass()
{
let class="";
if (this isBold)
class="isBlod-true";
if (this.isCut)
class=class+" isCut-true";
if (this.isGreen)
class=class+" isGreen-true";
...
return class
}
And when you add an item, you can do
items.push({data:...,class:this.getClass()})
Then the code
<div *ngFor="let item of items;let index=i>
<div [className]="item.class">item.data</div>
</div>
make the trick

Related

How to put div tag to put all buttons on one line?

Code for Filter Button
import React from "react";
function FilterButton(props){
return (
<div className="box">
<button
type = "submit"
className="button is-info"
onClick={()=>props.setFilter(props.name)}
>
<span>{props.name}</span>
</button>
</div>
);
}
export default FilterButton;
Unfortunately, the div tag is not able to put the following buttons(All, Active, Completed, Important) on the same line.
Is there any solution for this problem?
If you want to use the grid system of BULMA, you can do the following things:
see https://bulma.io/documentation/columns/basics/:
<div class="columns">
<div class="column">
First column
</div>
<div class="column">
Second column
</div>
<div class="column">
Third column
</div>
<div class="column">
Fourth column
</div>
</div>
If you want to use css in order to do this, you can use the css property display: inline-block,as it is shown here:
https://www.w3schools.com/css/css_inline-block.asp

How to a use bootstrap grid for an array created by a map method in react?

I am pulling data from an api and mapping over the JSON array to display the results, how do I use bootstrap grid to make the elements show one beside another instead of like a list?
//App.js
<div>
<FileHeader/>
{this.state.films.map(film=>(
<div className="container">
<div key={film.id} id='cardItem' className=''>
<MovieCard film={film}/>
</div>
</div>
))}
</div>
And this is the MovieCard Component
render() {
var film = this.props.film;
return(
<div className='card' style={{width:'18rem'}}>
<div className="card-body">
<h5 className="card-title">{film.title}</h5>
<h6 className='card-subtitle mb-2 text-muted'>{film.release_date}</h6>
<p className='card-text'>{film.description}</p>
</div>
</div>
)
}
How do I use the grid element to display the cards side by side?
You should only have 1 container for your items, so get it out of the map(), then insert a row in which you'll inject the card elements. You'll have to decide how much items you want on each row. In the example below you'll have 12 items per row as a row is made of 12 columns and col-xs-1 means each item will take 1 column in the row.
You can decide it per each screen size using col-xs, col-sm etc...
<div className="container">
<div className="row">
{this.state.films.map(film => (
<div key={film.id} id="cardItem" className="col-xs-1">
<MovieCard film={film} />
</div>
))}
</div>
</div>
Please try this....
<div className="container">
<div className='row'>
<FileHeader/>
{this.state.films.map(film=>(
<div key={film.id} id='cardItem' className="col-sm-3"><MovieCard film={film}/>
</div>
))}
</div>
</div>
Moviecard component...
render() {
var film = this.props.film;
return(
<div className='card' style={{width:'18rem'}}>
<div className="card-body">
<h5 className="card-title">{film.title}</h5>
<h6 className='card-subtitle mb-2 text-muted'>{film.release_date}</h6>
<p className='card-text'>{film.description}</p>
</div>
</div>
)
}

Want to click on a delete an element after searching for text from another div element

I need to search for a keyword in the span element (having unique id) from class Z and then based on the matching criteria, I want to click on the delete icon under div of class A for id 'ucDaily_rptAMRoutineList_ctl01_icon_1'
Assuming I am searching for "Test", I would like to see working code on how to achieve this.
I have multiple class X here.
<div class = "X">
<div class="A">
<div id="ucDaily_rptAMRoutineList_ctl01_1">
<span class="B">
<div id = "ucDaily_rptAMRoutineList_ctl01_icon_1">
<a>...</a>
</div>
</span>
</div>
</div>
<div class = "Z">
<strong>
<span> Test </span>
</strong>
</div>
</div>

Find the biggest height of a div in ng-repeat

I have a set of data which is an array of objects. Some objects have more data than the other. But each object will display in each col-md-4 div. Therefore, the height of each div will be different based on how much data an object has.
Here is my code:
<div class="col-md-12 eventlog-info-container">
<div class="row eventlogs-single-container">
<div class="col-md-4 eventlog-1-container" ng-repeat="record in records track by $index">
<h4>Event Log {{$index}}</h4>
<ul>
<li ng-repeat="(key, value) in record">{{::key}}: {{::value}}</li>
</ul>
</div>
</div>
</div>
My question is after ng-repeat, I want to find the biggest height of the element. And then apply the biggest height to each of the element using ng-style.
I have an idea to approach that using JQuery. However, I want to use Angular to do that? Any suggestion? Anuglar is not good for DOM manipulation?
Thank you in advanced.
After I did some research yesterday. I found an open source angular directive to solve my problem - angularJS Vertilize Directive An AngularJS directive to vertically equalize a group of elements with varying heights. In other words, it dynamically makes a group of elements the same height. Thank you Chris Collins who made this directive.
<div vertilize-container class="row">
<div ng-repeat="col in columns" class="col-sm-3">
<div class="well">
<div vertilize>
<h3>{{ col.title }}</h3>
<p>{{ col.body }}</p>
</div>
</div>
</div>
</div>
This answer should get you started on the right path.
<div class="col-md-12 eventlog-info-container">
<div class="row eventlogs-single-container">
<div class="col-md-4 eventlog-1-container" ng-repeat="record in records track by $index">
<h4>Event Log {{$index}}</h4>
<ul>
<li outer-height ng-repeat="(key, value) in record">{{::key}}: {{::value}}</li>
</ul>
</div>
</div>
</div>
app.directive('outerHeight', function(){
return{
restrict:'A',
link: function(scope, element){
//using outerHeight() assumes you have jQuery
// if not using jQuery
console.log(element.outerHeight());
}
};
});

Want to click on a input element after searching for text from another div element

I need to search for a keyword in the span element (class "D") and then based on the matching criteria, I want to click on the Choose input element button.
Assuming I am searching for "Test", I would like to see working code on how to achieve this.
Example markup:
<div class="A">
<div class="B">
<input class="C" type="submit" onclick="AddFacility(this)" data-facilityid="300075" value="Choose">
</div>
<div class="D">
<span class="js-open-more-info">Test</span>
</div>
</div>
Below is an code sample in java
//Get the text from span
String text = driver
.findElement( By.xpath("//span[#class='js-open-more-info']") )
.getText();
//if the text is equal to Test then click on the button
if( text.equals("Test") ){
driver.findElement( By.xpath("//input[#class='C']") ).click();
}
EDIT
For dealing with multiple divs of same structure sample html(just tweaked ur html and added multiple divs)
HTML
<html>
<body>
<div class="A">
<div class="B">
<input class="C" type="submit" onclick="AddFacility(this)" data-facilityid="300075" value="Choose">
</div>
<div class="D">
<span class="js-open-more-info">Test1</span>
</div>
</div>
<div class="A">
<div class="B">
<input class="C" type="submit" onclick="AddFacility(this)" data-facilityid="300075" value="Choose">
</div>
<div class="D">
<span class="js-open-more-info">Test2</span>
</div>
</div>
<div class="A">
<div class="B">
<input class="C" type="submit" onclick="AddFacility(this)" data-facilityid="300075" value="Choose">
</div>
<div class="D">
<span class="js-open-more-info">Test3</span>
</div>
</div>
</body>
<script>
document.getElementsByClassName('C')[2].addEventListener("click", myFunction);
function myFunction() {
alert('deleted');
}
</script>
</html>
Code in Java
//get all the divs into a list
List<WebElement> divs = driver.findElements(By.xpath("//div[#class='A']"));
//loop through the list
for(int i=0;i<divs.size();i++)
{
//get text of all the span elements inside multiple divs
String text=divs.get(i).findElement(By.xpath(".//span[#class='js-open-more-info']")).getText();
System.out.println(text);
//if text of span element is equal to Test3 click on the respective delete button
if(text.contentEquals("Test3")){
divs.get(i).findElement(By.xpath(".//input[#class='C']")).click();
}
}
Hope this helps you....Kindly get back if you need any further help

Resources