how to add a css style property dynamically in JSF - css

I am using primeface for the UI components and I have to set the background of the layout unit temporary its done by using the css style,
.layoutCustomStyle.ui-layout-unit-content
{
background-image: url('resources/images/backgrnd.png');
}
The id of the layoutunit is "layoutId" and the styleclass used is "layoutCustomStyle"
in xhtml,
<p:layoutUnit position="top" id= "layoutId" styleClass ="layoutCustomStyle">
</p:layoutUnit>
But what I want is to add the background image dynamically. The image will be chosen by file browser so, I cannot add a separate class for that and use bean.
UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
UIComponent comp= view.findComponent("layoutId");
Map<String, Object> attrMap = comp.getAttributes();
String className = (String)attrMap.get("styleClass");
using this I can set and get class names but how to change the attribute "background-image:" dynamically?
Hope the question is clear.Any help appreciated.
Thanks in advance,
Pegasus

Use style attribute instead of styleClass.

This is an old question but at this time has been viewed 10,354 times. I want to share the way i resolve 'add a css style property dynamically' in primefaces 6.2
In my layout i have a header that i need change dyamically the image every 10|20 secs.
<h:panelGrid id="cabecera" columns="2" cellpadding="1" columnClasses="..."
style="width:100%; background-size: cover; background-position: center; background-image: url('#{request.contextPath}/resources/images/header/Vignette/#{userSelected.headerFile}');">
<h:form id="...." >
I have a list with the names of all the images that i can use and userSelected.headerFile choose one randomly.
Three similar options:
1.- At first i Use p:poll directly to update the panelGrid id 'cabecera':
<p:poll interval="10" update="#([id$=cabecera])" autoStart="true"/>
Of course that works, on every update the background image change. That could be enough in some cases where the update and page blink donĀ“t be problem.
2.- Using a little of JavaScript, a bean method in the listener of p:poll.
Declare a js function to change the background property (or any other):
<script>
function headerBackground(urlBG) {
var laUrl = (urlBG);
document.getElementById('cabecera').style.backgroundImage = laUrl;
}
</script>
In my Bean userSelected i declared a method to call the javascript function via RequestContext.getCurrentInstance().execute(...). I decided received the url only and add the rest of values in the function:
public void callJSheaderBackground(String url) {
String jsFunc="headerBackground(\"".concat(url.trim()).concat("\")");
try{
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.execute(jsFunc);
}catch(Exception ex){
...
}
}
Finally the p:poll
<p:poll interval="20" listener="#{userSelected.callJSheaderBackground('url(\''.concat(request.contextPath).concat('/resources/images/header/Vignette/').concat(userSelected.headerFile).concat('\')'))}" autoStart="true"/>
3.- Calling directly a JS function
My JS function, reciving the contextPath and the image file name as parameters:
function setVignetteAsBackground(contextPath,vignetteName) {
var laUrl = "url('" + (contextPath)+'/resources/images/header/Vignette/'+(vignetteName)+"')";
document.getElementById('cabecera').style.backgroundImage = laUrl;
}
Then directly calling from p:poll on the onstart|oncomplete event:
<p:poll interval="20" onstart="setVignetteAsBackground('#{request.contextPath}','#{userSelected.headerFile}')" autoStart="true"/>
Hopefully be useful for somebody.

Related

Add CSS property in Angualr2 with MetaWidget

I am trying to add CSS when clicked on row or column of table, Following is code
private rowClicked(event: Event): void {
event.srcElement.setAttribute("class", "highlighted");
}
But it's not working as accepted. Am I doing in wrong way, Is there any alternate way to add CSS dynamically?
Note-
Is there any way to add CSS using dom element, my table has thousands of data and to create this table, I have used MetaWidget.
The easiest way to your problem is to assign a unique ID to each included element together with employing another variable to hold selected ID. The logic to turn on my-class CSS class will now be based on the selected ID.
Your new HTML template:
<div (click)="rowClicked(1);" [ngClass]="{'my-class': highlightedDiv === 1}">
> I'm a div that gets styled on click
</div>
Your rowClicked function:
highlightedDiv: number;
rowClicked(newValue: number) {
if (this.highlightedDiv === newValue) {
this.highlightedDiv = 0;
}
else {
this.highlightedDiv = newValue;
}
}
A working demo is here.
More can be found here.
You are using MetaWidget, but you are not mentioning what version you are using.
If you want work with Angular2 and MetaWidget, you should have use a compatible version of MetaWidget, which can be found here-
https://github.com/AmitsBizruntime/MetawidetA2
Using this library will be the best solution for you.
Re-
Angular does not work based on DOM, it works based on Component.
If you like to work on DOM, then you should include jQuery in tour angular project from here-
How to use jQuery with Angular2?
But it is not a good practice.

Dynamically change element styles via custom properties?

For example, you can change the ink colour in paper-tabs by changing --paper-tab-ink: var(--accent-color);. Is it possible to change the value of the CSS custom properties dynamically similar to how you can toggle a class or change the style in JS?
There are different ways to do this, but a simple answer is to use the Polymer.updateStyles() method after making your class changes.
For example, let's say your styles are:
<style>
.yellow x-example {
--light-primary-color: #fdd85f;
}
.red x-example {
--light-primary-color: red;
}
</style>
and you want to make the component use the styles in the .red class. You simply add it as you normally would in javascript, then be sure to also use this function to actually update it on the page.
<div class="yellow" onclick="this.className='red'; Polymer.updateStyles()">
<x-example></x-example>
</div>
Yes, first get the object of your custom element. Then get the customStyle object. Add a style to that object. And then run element.updateStyles();
t.clickListener= function(e) {
var t = Polymer.dom(e).localTarget; //retarget if needed
t.customStyle['--the-color-etc'] = 'pink';
t.updateStyles(); // mandatory for the CSS variables shim
};
See the docs

DataGrid / CellTable styling frustration -- overriding row styles

I'm trying mightily to style my GWT 2.4 DataGrid, and hit roadblocks at every turn. I've added the following row styling to my DataGrid:
dataTable.setRowStyles(new RowStyles<IntegrityItem>() {
#Override
public String getStyleNames(IntegrityItem row, int rowIndex) {
if (row.getSomeValue() >= 100) {
return MyResources.INSTANCE.mystyles().alertRow();
} else {
return "";
}
}
});
The style alertRow is simply this:
.alertEntry {
font-weight: bold;
color: #00ff00;
background-color: #ff0000;
}
More information: I've made a local copy of DataGrid.css and removed ALL "background" elements from all the styles, and I've used this to construct a ClientBundle:
public interface MyDataGridResources extends DataGrid.Resources {
public static final FmeaDataGridResources INSTANCE = GWT.create(MyDataGridResources.class);
#Override
#Source({"../resources/styling/mydatagridstyles.css"})
Style dataGridStyle();
}
I've used this (MyDataGridResources.INSTANCE) in my DataGrid constructor.
When I try it out, the rows that meet the criteria contained green (#00ff00) text, but the background colour remains white or grey depending on whether it is an even row or an odd row. How is it that background-color is ignored the way it is? Where is it getting those colors in the first place?! I've removed background color information from the css file completely.
You can create a custom CSS file and provide this to the DataGrid through defining a new style resource. This is done by creating a type that extends DataGrid.Resources, which knows about your CSS file. You then pass this to the constructor of the datagrid.
To provide a fairly complete example, first create a new type for the DataGrid style. (Defining a new type like this just uniquely identifies your style within GWT).
public interface MyStyle extends DataGrid.Style {
}
Then, define an interface which overrides the dataGridStyle() method stub in DataGrid.Resources. The dataGridStyle method should return the previously defined MyStyle.
Note the two elements given to the #Source annotation - you can just override any of the class names in the default CSS (DataGrid.css) in the second file you provide ("DataGridOverride.css" here).
public interface DataGridResource extends DataGrid.Resources {
#Source({ DataGrid.Style.DEFAULT_CSS, "DataGridOverride.css" })
MyStyle dataGridStyle();
};
To construct your newly-styled datagrid all you need to do is:
DataGridResource resource = GWT.create(DataGridResource.class);
dataGrid = new DataGrid<T>(pageSize, resource)
One subtlety is as you're increasing the precedence of the overridden styles, you may need to override any other styles that require higher precedence, for example the row hover rules need to come after the row styling rules.
See http://code.google.com/p/google-web-toolkit/issues/detail?id=6144#c3 (which is not a bug!)
In short extend the DataGrid.Style (the goal is only to have a new type, you don't have to add anything to it) and have your dataGridStyle overridden method return your own subtype rather than DataGrid.Style (and it'll work because of return-type covariance)

How to use a background image in ASP.Net control <asp:PasswordStrength>

Can a "asp:PasswordStrength" control be customized to show different images for different "StrengthStyles="weak; average; strong" (separate image for each strength).
I am not able to upload a sample image as I am a new user. Sorry for that.
Kindly help,
Thanks,
Eswar
You can use the StrengthStyles property to provide a list of CSS classes that will be applied to the indicator depending on the current password's strength. Then, you can use the background-image CSS property to associate an image with each class:
.weak {
background-image: url(images/weak.png);
}
.average {
background-image: url(images/average.png);
}
.strong {
background-image: url(images/strong.png);
}
<asp:PasswordStrength ID="yourPasswordStrength" runat="server"
TargetControlID="yourTextBox" StrengthStyles="weak;average;strong" />
If you are looking to display it as a bar indicator you can also apply backgrounds to them. See the following article for sample code...
http://www.datasprings.com/resources/articles-information/ajax-password-strength-control

How to skin mx:Image source through CSS?

For example, it is possible to embed an image...
[Embed("myImage.png")]
public var myImage:Class;
and assign it to the mx:Image's source property like this :
<mx:Image source="{myImage}" />
but what if I want to be able to load the image from a compiled CSS file, something like:
.myImage {some-property: Embed("myImage.png"); }
What is the cleanest way to assign this kind of styled image to the mx:Image's source property ?
Alright, I found a solution:
You can load images in a component, spark Panel in this case :
s|Panel {
myImage: Embed(source="assets/images.swf", symbol="simple_image");
myOtherImage: Embed(source="assets/images.swf", symbol="other_image");
}
and then, when you change styles at runtime, you can react on StyleEvent.COMPLETE event:
var css:CSSStyleDeclaration = StyleManager.getStyleManager(null).getStyleDeclaration("spark.components.Panel");
var myImage:Object = css.getStyle("myImage");
var myOtherImage:Object = css.getStyle("myOtherImage");
From there you can easily assign those custom styled images to your mx:Image component's source.

Resources