You need to sign in to do that
Don't have an account?
Marty Bremer
How to dynamically change the background image of a class within a lightning component?
I am using the lightning:fileUpload component and have been successful in changing it's appearance to look similar to the following:
When the user clicks on the image they are able to take a picture and upload it successfully. I was able to accomplish the look of this fileUpload component by changing the .css as:
.THIS .slds-file-selector__body {
background-image: url(/resource/open_bite_right);
height: 84px;
width: 84px;
}
My problem is that I have a dozen other fileUpload components that each have a different background image. Without creating a dozen aura:components is there another way to dynamically change the background image?
The issue I am having is that the slds-file-selector__body class is buried within the inputFile component. Changing the class on the inputFile component itself is no good because that only changes the class of the parent component.
My desire is to have a single aura:component that includes this lightning:fileInput component, and then set attributes on the component to change its look and functionality. My only limitation at this point is being able to somehow get to the slds-file-selector__body within the fileInput component and dynamically change its background.
When the user clicks on the image they are able to take a picture and upload it successfully. I was able to accomplish the look of this fileUpload component by changing the .css as:
.THIS .slds-file-selector__body {
background-image: url(/resource/open_bite_right);
height: 84px;
width: 84px;
}
My problem is that I have a dozen other fileUpload components that each have a different background image. Without creating a dozen aura:components is there another way to dynamically change the background image?
The issue I am having is that the slds-file-selector__body class is buried within the inputFile component. Changing the class on the inputFile component itself is no good because that only changes the class of the parent component.
My desire is to have a single aura:component that includes this lightning:fileInput component, and then set attributes on the component to change its look and functionality. My only limitation at this point is being able to somehow get to the slds-file-selector__body within the fileInput component and dynamically change its background.
Its a wild guess but lets see. If you are using the aura:component at different places that means there would be some way of getting some context variable, or any attribute which distinguishes the aura:component context, based on that context variable or attribute, using <aura:if> or javascript, you can decide an additional class for the lightning:fileInput component and define background image for that class. I can try this in my dev org tomorrow and will provide further inputs. But I think this is achievable.
Cheers,
Dev
You are correct, there is additional context for each aura:component. When the component is insantiated, I am also setting an aura:attribute that contains the context. Based on that attribute I am able to determine which background I want to use, I just can’t figure out anyway of setting that background programmatically since the needed class is buried within the lightning:fileInput component.
I think I got this, instead of changing the lightning:fileInput component class 'slds-file-selector__body', based on the attribute value I added a dynamic class to the component (using jquery) in my render funtion, and in the .css definition of the component, declared classes with the bg image. You can also add the image while adding the class. Below is the code,
In the render function, i fetched the value of the attribute, based on that value added a class to the component. You can also add the image while adding the class (commented lines in the js). To me, this is not the most elegant solution, but it works.
Hope this helps.
Cheers & happy coding,
Dev
I still can't seem to make this work for some reason. I have simplified down the sample code here:
aura:component
css
Notice how I'm using the lightning:input type="file", but have to get at the slds-file-selector__body class to change the background image. How can I change it to dynamically replace the background within that specific lightning:input component?
After doing a bit more research, it seems like it would work best if I could add the image while loading the slds-file-selector__body class. Something like:
$('div.slds-file-selector__body').css('background-image','url(/resource/smilestore_open_bite_right)');
in renderer.js. But what is the correct syntax to get at the slds-file-selector__body within the lightning:input component? I keep getting the following error stating that '$' doesn't exist:
it was my mistake, the jquery wont work in the render function as it is loaded after the render function is processed.
I moved my code to the afterScriptsLoaded="{!c.check}", and it works there. Below is the entire code.
I will test with lightning:input component latter today.
Cheers,
Dev
if that solves your problem, could you please mark the question as green.
Cheers,Dev
Unfortunately it doesn't solve the problem. If you test this using the lightning:input component, you will see the issue.