After a short amount of google, I stumbled on JSColor, which is very straightforward to integrate, as you just include the javascript into your page and add the "color" class to any input that you want the color picker to fire from.
First up, I downloaded the JSColor zip file and uploaded as a static resource named 'jscolor_1_4_0'. For the purposes of this blog post I then created a simple custom object named 'Coloured_Text' with four fields - 'Field_1' and 'Field_2' contained the text to be displayed, while 'Colour_1' and 'Colour_2' defined the colour that the associated text should be displayed in.
I then put together a simple edit/create page for the Coloured_Text custom object:
<apex:page standardcontroller="Coloured_Text__c"> <apex:includeScript value="{!URLFOR($Resource.jscolor_1_4_0, 'jscolor/jscolor.js')}" /> <apex:form > <apex:pageBlock title="Colour Picker Edit"> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}" /> <apex:commandButton value="Cancel" action="{!cancel}" /> </apex:pageBlockButtons> <apex:pageBlockSection > <apex:inputField value="{!Coloured_Text__c.Name}" /> <apex:pageBlockSectionItem /> <apex:inputField value="{!Coloured_Text__c.Field_1__c}" /> <apex:inputField styleClass="color" value="{!Coloured_Text__c.Colour_1__c}" /> <apex:inputField value="{!Coloured_Text__c.Field_2__c}" /> <apex:inputField styleClass="color" value="{!Coloured_Text__c.Colour_2__c}" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
The colour picker is added to the input fields via the "styleClass" attribute in the '<apex:inputField styleClass="color" value="{!Coloured_Text__c.Colour_2__c}" />' components.
After overriding the New and Edit buttons for the custom object, clicking the new button gave me the following page:
I then created the view override visualforce page:
<apex:page standardcontroller="Coloured_Text__c"> <apex:includeScript value="{!URLFOR($Resource.jscolor_1_4_0, 'jscolor/jscolor.js')}" /> <apex:pageBlock title="Colour Picker View"> <apex:pageBlockSection > <apex:outputField value="{!Coloured_Text__c.Name}" /> <apex:pageBlockSectionItem /> <apex:pageBlockSectionItem > <apex:outputLabel value="Field 1" /> <apex:outputText style="color: #{!Coloured_Text__c.Colour_1__c}" value="{!Coloured_Text__c.Field_1__c}" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Field 2" /> <apex:outputText style="color: #{!Coloured_Text__c.Colour_2__c}" value="{!Coloured_Text__c.Field_2__c}" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:page>
The chosen colours are applied to the text via the "<apex:outputText style="color: #{!Coloured_Text__c.Colour_2__c}" value="{!Coloured_Text__c.Field_2__c}" />" component. Viewing my 'Blog Test' record shows the text in the correct colours:
After the Actionfunction is used on the page, this color functionality binding is lost.... What are you doing in that case?
ReplyDeleteThere is no actionfunction on my pages. I'm assuming that this is something you have added, so its difficult to tell what might be happening without knowing what you have done.
DeleteThe Coloured_Text__c record is stored in the controller, so I wouldn't expect a postback to cause problems.
Wow it greate!!! Thanks Bob..
ReplyDeleteHi Bob, Does this still work? Just tried a number of examples and none of them seem to work.
ReplyDeleteCan you please tell me what type of field is Colour_1__c ?
ReplyDeleteHi Bob,
ReplyDeleteThis color picker do not works with "rendered" attribute as i am trying to render a table under and this seems not working with this rendered attribute.
hey Bob,
ReplyDeletecan we use this picker in tag?