Vue buefy table cell class based on cell value - html

I have a buefy table with values between 0-1000, I want to conditionally highlight the cells in the table based on the cell value. For example is the value is above 50 i want to set the cell background as green, 25-50 to yellow and <25 to red. I saw this previous answer: How to apply class to a specific cell in a Buefy Table? But the solution doesnt seem to work.
Here is my current buefy table:
<b-table
:data="selectedProducts.productQuantities"
:row-class="(row, index) => row[0] === 'TOTAL'"
>
<b-table-column
label="quantity"
v-slot="props"
:class="cellObject(props.quantity)"
>{{ props.quantity }}
</b-table-column>
And i have a method to calculate and return the class of the cell:
methods: {
cellObject(quantity){
if(quantity < 25) {
return "is-red";
}
else if (quantity > 50) {
return "is-green";
}
else {
return "is-yellow";
};
},
which returns one of these classes:
<style>
.is-red {
background: #343a40 !important;
font-weight: bold;
}
.is-yellow {
background: #ee3f3f !important;
font-weight: bold;
}
.is-green {
background: #41f841 !important;
font-weight: bold;
}
After fiddling around with it more i found that if i did :cell-class="is-red" it would apply it to the whole column.
<b-table-column
label="quantity"
v-slot="props"
:cell-class="'is-red'"
>{{ props.quantity }}
</b-table-column>

You cannot apply this to the column, it has to be applied directly to the cell itself. Unfortunately, this is not easily done with a conditional unless you put the conditional in the tag however, I doubt this will accomplish what you're after. I would recommend that instead, you use a and just change the background color of the tag based on a conditional. For example, we do something like this:
<b-table-column label="Column Label">
<template v-slot="props">
<b-tag type="is-primary" v-if="props.row.value > 0 && props.row.value < 25">{{ props.row.value }}</b-tag>
<b-tag type="is-success" v-else>{{ props.row.value }}</span>
</template>
</b-table-column>

Related

How to add style in typescript using react?

i want to apply style to span tag using typescript in react. I have a label counter with value next to it. something like below
counter 0
I have a component Label like below,
interface Props {
color?: string;
size?: number;
}
const Label = styled.span<Props>`
color: ${props => props.color || props.theme.colors.text};
`;
and in my other component i use the label like below,
render = () => {
const count_var = 0;
return (
<div>
<Label> counter </Label>
<span style={{ color: counter === 0 ? 'red' : counter === 1 ? 'blue' : 'green' }}>
{count_var} </span>
</div>
)
}
from the above code, as you see i have place count variable within span tag since want to add a class to the count variable based on its value. and the output for above code would be like below,
counter0
How can i use the Label component to apply style to counter variable based on its value...
if count var is 0 then should be in color red
if count var is 1 then should be in color blue
if count var is > 1 then should be in color green
Also i need to have a space between counter text and its value like below
counter 0
I am new to using typescript and not sure how to use styles using it. could someone help me with this. thanks.

Angular material mat option selected value highlight

I want to highlight background colour of the selected value which is clicked. I was going through this example
Here it is highlighting multiple select value. I want to highlight only the selected value when click to choose an option.
https://stackblitz.com/edit/material-selection-list-5-0-0-selection-color-change?file=app%2Fapp.component.css
<hello name="{{ name }}"></hello>
<p>
Area 3 is initially selected. The displayed array below updates as options are selected/deselected.
</p>
<div>
Selected: {{ selectedOptions | json }}
</div>
<mat-selection-list #list [(ngModel)]="selectedOptions" (ngModelChange)="onNgModelChange($event)">
<mat-list-option *ngFor="let tta of taskTypeAreas" [value]="tta.name">
{{tta.name}}
</mat-list-option>
</mat-selection-list>
app.css
p {
font-family: Lato;
}
mat-list-option {
margin: 10px;
}
mat-list-option[aria-selected="true"] {
background: rgba(0, 139, 139, 0.7);
}
mat-selection-list is not suitable for single value selection but still, if you want to you can try something like this.
mat-list-option[aria-selected="true"] {
background: blue;
}
you have to clear your list on every selection
https://stackblitz.com/edit/angular-i3pfu2-kylx8v
UPDATE: Based on current mat-selection-list options: https://material.angular.io/components/list/api
declaring [multiple]="false" and addressing .mat-list-single-selected-option style this will highlight the currently selected mat-list-option
<mat-selection-list [multiple]="false">
<mat-list-option>1</mat-list-option>
<mat-list-option>2</mat-list-option>
<mat-list-option>3</mat-list-option>
</mat-selection-list>
.mat-list-item.mat-list-single-selected-option {
background: #FF0000;
}

material-ui table: how to make style changes to table elements

I'm using 'material-ui' and trying to get a table element to change color when the element has a certain value. Below is the code I tried, if the cell value is "Out of Zone", the background should go red'ish. The table renders fine, but toggling the color change doesn't work, how come (or is my approach all wrong)?
function renderRowColumn(row, column) {
if (row.status == "Out of Zone") {
return (
<TableRowColumn className="ae-zone">
{row[column.name]}
</TableRowColumn>
)
}
return (
<TableRowColumn>
{row[column.name]}
</TableRowColumn>
)
}
In style.css:
.ae-zone {
background-color: '#e57373';
font: "white";
}
Your specificity on the css selector is not high enough. The rendered TD element has an inline style in which the background property is getting inherited which is overriding your class style.
Is there any reason since you already have the logic seperated out you don't just use inline styles for something like this.
<TableRowColumn style={{backgroundColor:'red', color: 'white',}}>{row[column.name]}</TableRowColumn>
This is definitely working well and I tested it.
Your other option would be to add !important to your css.
td.ae-zone {
background-color: '#e57373' !important;
color: "white" !important;
}
I think if I had to chose though I would recommend the first as it is cleaner.
Don't put quotes around color values in css:
.ae-zone {
background-color: #e57373;
color: white;
}
Also, it's color: white, not font: white.
Most of the time the Table takes the default style, so if the styles didn't get applied try appending !important to the style. This worked for me.
.ae-zone {
background-color: '#e57373' !important;
color:red;
}
There is another way to do this :
import { makeStyles } from "#mui/styles";
// Declare this bellow your import
const UseStyles = makeStyles({
root: {
"& .MuiTableBody-root": {
backgroundColor: "#121212",
},
},
});
// Declare this after your declaration page
const classes = UseStyles();
// Now in your Table, use the class :
<Table className={classes.root}>
<TableHead>
{...}
</TableHead>
</Table>
With the inspector you can see each automatic class from Material UI and target them in the makeStyle.
Be carefull to use the same code example :
"& .MuiClassNameYouWantTarget" : {
textAlign: "center",
},

Coloring the text depending on numeric value using css

I want to color text based on its value, using css.
ie. if value is less than 20 --> red ,
if value is between 20 - 60 --> orange ,
if value is greater than 60 to 100--> green.
I don't want to add any class in the template depending on the value.
I found this Link: How do I change the background color with JavaScript? but it doesn't suffice as I have too many values to apply color to.
Also I want it to be more maintainable when adding new values in future.
It is not possible only with CSS.
You have to use JavaScript/jQuery to dynamically add the color, based on an object color match, and test if the value in the data-color HTML attribute is between the range for each element.
The JS code dynamically check if the element attribute is in a color range and apply the matched color.
If you will have to add some color and range in the future, simply add new values in the colorMatch hash, and update your CSS color class list.
##CSS
.red {color:red}
###HTML
<p data-color="19">Lorem 19</p>
###JS
var colorMatch = {
'0-19' : 'red',
'20-59' : 'orange',
'60-100' : 'green'
};
Here is the working fiddle
If you do not consider it cheating to not use the actual innerHTML as a condition, but rather construct it from a CSS variable using content you could do something like this (just as an effort to not use JS in this case):
<num style="--num:1"></num>
<num style="--num:99"></num>
<num style="--num:165"></num>
num {
--breakpoint: 100;
--g: calc((clamp(0, var(--num), var(--breakpoint)) - calc(var(--breakpoint) - 1)) * 255);
color: rgb(0, var(--g), 0);
}
num:after {
counter-reset: variable var(--num);
content: counter(variable);
}
In this scenario I am coloring any of the numbers green if they are above 100, but more rules can be added using the same method to serve your use-case.
With that said, I think there is probably no scenario where this would ever be useful, other than just technical trivia, as it is more readable to simply change the class of the element dynamically using plain JS. Still kinda fun way to use counter-reset and counter though.
Here is the same example on jsfiddle: https://jsfiddle.net/msz1aouc/24/
A simple approach could be
HTML
<div class="content">
<p>high</p>
</div>
<div class="content">
<p>low</p>
</div>
<div class="content">
<p>medium</p>
</div>
<div class="content">
<p>critical</p>
</div>
<div class="content">
<p>high</p>
</div>
Jquery
var content = $(".content p").text();
if (content == "high") {
$(this).css("color", "#ffffff");
}
if (content == "low") {
$(this).css("color", "#ccc");
}
if (content == "critical") {
$(this).css("color", "#000");
}

knockoutjs css binding value when boolean computed property is false/true

I am binding my currently selected item like this:
The selected item gets visible, but how can I give the unselected items an unselected/default background color? When I set a background color to the template class I do not see anymore the background color set from the selection via mouse click.
Is the problem maybe that I have 2 backgrounds set but none is removed?
<div data-bind="foreach: items">
<div class="cellContainer" >
<div class="template" data-bind="click: $parent.selectItem, css: { selected: isSelected }">
<div data-bind="text: number"></div>
</div>
</div>
</div>
.selected {
background-color: #0094ff;
}
Sounds like a cascade issue. Make sure your ".template" style is defined before your ".selected" style in your css file.
Make sure your selectItem method resets isSelected on all elements before setting it to true on the argument. A naive implementation could be:
var ViewModel = function() {
// Skipped the rest for brevity...
self.selectItem = function(item) {
// Deselect all items first
for (var i = 0; i < self.items().length; i++) {
self.items()[i].isSelected(false);
}
// Select the argument
item.isSelected(true);
};
};
See this jsfiddle for a demo.
However, it is often easier to keep a reference on the parent view model to the currently selected item, and change the css binding to something like:
css: { selected: $parent.TheOneSelectedItem().number() == number() }
See this fiddle for the alternate demo.
Something like this might work.
.template {
background-color: #fff;
}
.template.selected {
background-color: #0094ff;
}
It does not look like a knockout issue.