Angular apply css dinamically without knowing the properties - css

i need to apply css to a field, but i don't know which elements the user want to change. I have a module where a user can create forms dinamically, creating fields and so on, and they can set styles too. They will write in the box all the stile they want, like : color: red; background-color: black...etc;
I found many questions about but all the answers require everytime to know the property to change, so my question is : Is possible in angular to pass to a field a variable that contain { property: value;...} and apply it dinamically?
Example of text written by user:
{color:red; background-color: white; border: red 2px solid; opacity: 0.3; position: absolute; top: 0; left: 0; width: 100%;height: 100%;
}
Example of the input field where i need to apply it:
<input matInput [type]="getInputType()" [formControlName]="field.Name" [readonly]="field.Readonly"
[required]="field.Required" [(ngModel)]="record[field.Name]" (ngModelChange)="onRecordValueChange()">
I've tryed [ngStyle]= variable but ngstyle seems requiring the property and then ou can bind a variable to it, but if i have to map all the possible variables in css it will be a wall of code... Hope someone can help me out with this tricky problem. Thanks in advance.

Update correct in the code
const keypar=b.replace(/['"]+/g, '').split(":")
To allow some like
style="{'color':'red'}"
You can create an object based in the string and use ngStyle
style="{color:red; background-color: white; border: red 2px solid; opacity: 0.3; position: absolute; top: 0;left: 0; width: 100%;height: 100%; }"
styleObj=(this.style.replace("{","").replace("}","")).split(";").reduce((a,b)=>{
const keypar=b.replace(/['"]+/g, '').split(":")
if (keypar.length>1)
a[keypar[0].trim()]=keypar[1].trim();
return a
},{})
<div [ngStyle]="styleObj">Hello</div>
NOTE: I choose use split instead of use a complex Rexepr to conver the string to a json object and use JSON.parse
stackblitz

Related

I am trying to use dynamic data to change and trigger classnames but it always only result to true despite data containing false. Any solution?

I am trying to dynamically change the styles based on ternary classnames.
I am trying to change the background to dark or light based on the input on the workWrapper in here:
The CSS:
.normColor{
background-color: #f3f3f3;
width: 100%;
height: 100vh;
margin: 0 auto;
}
.normColor .dark{
background-color: #232A4E;
width: 100%;
height: 100vh;
margin: 0 auto;
}
I already tried messing around with the component, searching for other similar issues to mine and trying to search youtube videos regarding it but I cannot find any solution.
You forget to de-structure your props.
try this :-
const WorkSection = ({workWrapper}) =>{
return(<div className={workWrapper?'normColor':'dark'}></div>)
}
And you checking props, if it is null or not. And it will always give you true, because components always have props

How to change the size of the arrow in vue-tour?

I am not able to change the arrow size in vue-tour. While I am able to change it by inspecting in Chrome, in CSS I am unable to do it as it has some runtime generated ID associated with it.
.v-step__arrow[data-v-e97491e4], .v-step__arrow[data-v-e97491e4]:before {
position: absolute;
width: 10px;
height: 10px;
background: inherit;
}
The v-step GitHub is available here: https://github.com/pulsardev/vue-tour/blob/master/src/components/VStep.vue
You can make use of v-deep. It helps in applying the styles to the child-components directly.
If you are making use of sass then you can apply styling as:
::v-deep .v-step {
background-color: black;
}
If you are simply using css then you can use v-deep as:
>>> .v-step {
background-color: black;
}

Why my ng-class is not working

I am trying to display spinner when the button is clicked. I had one working plunker and I am trying to implement it little bit of tweak. My Plunker is here. I referred this working Plunker but no luck. I know m missing something small here.
ng-class="{true: overlay}[madeCall]"
Ok both answers point correctly one error, but there is also a fault in logic.
In the plunker you copied it applies a class called .grey when it is true but in conjunction with the css, he has e.g: .grey .overlay, the result is the desired.
If you want to copy that exact logic you have to add the .grey class and also change your css.
e.g:
css:
.grey .overlay {
background-color: #e9e9e9;
/* display: none; */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0.5;
}
and html:
<div ng-controller="mainCtrl" ng-class="{true: 'grey'}[madeCall]">
plunker
It should be:
ng-class="{overlay: madeCall}"
The syntax for ng-class is {className: Expression}, if the expression is truthy the class will be applied.
overlay should be there in quotes otherwise angular will lookup for overlay variable in scope.
ng-class="{true: 'overlay'}[madeCall]"
Demo Here

How to update "background-color" property of certain class with each record in extjs

I am working in extjs. I want to show different records with different colors. I am loading store and retriving records in callback function
taskStore.load({
url : URL_TASK,
callback: function(records, operation, success) {
if(success) {
for(var i = 0; i < records.length; i++) {
records[i].set('Cls', 'assignedTasksCls');
}
}
}
});
For each record i am setting Cls= "assignedTasksCls", which i have defined as,
.assignedTasksCls {
background-color: #51c063;
border: 1px solid #8cd191;
border-radius: 5px;
box-shadow: 1px 1px 2px rgba(150, 150, 150, 0.5);
height: 90%;
left: -6px;
line-height: 7px;
position: relative;
}
I want to change this class's background-color property as need to set different colors to differnt records. I can get this css via= records[i].getCls();
So how to update "background-color" of this class with each record?
The way of changing the background-color is
via JQuery
$("#YourSelector").css("background-color", "yellow");
Via Extjs
suppost you have
<div id="div1">My Div 1</div>
then
Ext.onReady(function() {
Ext.get('div1').setStyle('color', 'red');
});
for Class way use
records[i].addCls("assignedTasksCls");
records[i].removeCls("assignedTasksCls")
nice example from here
also, I found good example might be usefull for your case, please check from here
Please go through the following links,
extjs change grid cell background based on value
changing background colors of grid rows dynamically
Change background color of row
See this example, it does exactly what you need.

"Dynamic" CSS styling in asp .net?

I have the following inside a content place holder in my asp .net pages:
<style type="text/css">
#sortable1 { list-style-type: none; margin: 0; padding: 0; zoom: 1; }
#sortable1 li { margin: 3px; padding: 3px; width: 90%; border: 1px solid #000000; background: #000000; color: #FFFFFF; }
#sortable1 li.highlightWorkflow { background: #FFFF00; color: #000000; }
</style>
I would ideally like to swap the #00000's for values held on the page, maybe in hidden fields. Is this possible?
You cannot modify your CSS file directly from asp.net, but you can do this with some creative use with a javascript library, like jQuery.
In theory you would use the following pseudocode
var colorCode = getColorCodeFromHiddenField();
$(field).prop(property, colorCode)
If your taget is to be able to modify the colors as you design, I suggest you take a look att Less Css (http://lesscss.org/). This lets you store a color code in a variable so you only have to change the value once.
If you however, need a way for users to change values, something like #Jan's suggestion is better.
If you want that to be done on the client side, the easy way is to use JQuery or JavaScript as Jan describes above.
If you want to do it on the server side, you can assign an id value to the element that you need to change styles and do something like this:
yourID.Style.Add("color", something);
or
yourID.Attributes.Add("style", "color: yourcolor;");

Resources