How do I render custom picklist values?
I have a page where users need to be able to change a picklist to change the status of a record, such as "working", "submitted", etc. However, normal users shouldn't be able to put it into the approved status. I know I can add a validation rule that says "if the user's profile = x and the picklist value = "approved", then throw error message 'you can't select that' ", but the ideal way, of course, is to prevent a normal user from being able to select the "accepted" value in the first place. This seems like a perfect opportunity for a custom Skuid renderer! I'm thinking that in read mode the picklist shows the current value, but in edit mode it shows only values that the current user has access to based on their profile. Something like:
if (field.mode == 'read') { skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value); } else if (field.mode == 'edit') { /* if (profile != admin) { add picklist value 'working'; add picklist value 'submitted'; } else if (profile = 'admin') { add picklist value 'approved'; } render picklist in edit mode */ }
How would I do this (or create an equivalent or similar solution)? Bonus points: Maybe it can also prevent normal users from changing the status from "approved" to something else? 8
Categories
- 7.9K Questions
- 926 Ideas
- 220 Discussion Categories
- 178 General
- 9 Community Feedback
- 3 Community Info
- 18 Knowledge Base
Comments
Reviewed & Approved
. So by using skuid.utils.decodeHTML(), you're converting this back into the actual value. Then, because you're leveraging the skuid.ui.fieldRenderers to actually do your output (as opposed to hand-coding your own picklist), the XSS vulnerability prevention is taken care of. So the lesson to learn here is this: Whenever doing custom Field Renderers for Text/Textarea/Picklist fields, always decode the value passed in via arguments[1], like so:If you want to completely override this behavior, you'll need to roll your own custom picklist renderer using our internal skuid.ui.renderers API, and as J describes in this community post.
If you want to rely on the internal renderer, but adjust the values AFTER the renderer has been run, you might try removing the displayed values directly from the output <select> element AFTER the renderer has been run:
// Run the standard picklist renderer for the given mode
I have a picklist with 4 values, i would like to remove 2 values. I am following the below code, but it is not removing those values.
I was beating my head against the wall trying to figure out why overriding the skuid.metamodel.picklistEntries was not working.
It sounds like your snippet isn't getting passed the field as arguments[0]. How are you launching you snippet? This code will only work if you're running it as a custom field renderer.
Seems so simple but I haven't been able to figure it out :-)
Thank you in advance!
Thank you!
I know based on this post: http://help.skuidify.com/m/11720/l/214170-skuid-ui-basic-renderers that I need to use renderas 'RADIO_BUTTONS' in some way, but not sure how.
my snippet:
add a line before your skuid.ui.fieldRenderers
So Robin you might try
Amazing, thank you for the help! I can now render the options as radio buttons (YAY!). As an added challenge, I would still like to remove unwanted entries. The existing script as suggested earlier does not seem to do the trick any more once the list is rendered as radio buttons. How would I have to modify this code to remove them?
Thank you in advance!
Objective:
If the running user has a sales category of "Retail Sales" then only show the "Retail Sales" option in the picklist. Otherwise render as usual.
Notes:
This is a dependent picklist.
Code:
It looks like your code is removing the value is the value === 'Retail Sales', but from your description, you're looking to do the opposite. Try changing === to !==
Also you might try inserting some console.log lines at various junctures. Like after isRetailSales definition, console.log('Is Retail Sales? '+isRetailSales), and maybe also after select.length to get the length of your picklist and make sure that is coming in right, and maybe also in the $.each loop, log the value of each picklist value so you can see what it's capturing. That might help you find an error somewhere.
Hope that helps!
THANK YOU! the issue was with the "==" was why the picklist was not rendering properly. When I changed to "===" the rendering is honored. I will remember this going forward with JS!
However now, the value is not saved. 'Retail Sales' is the only option therefore chosen by default (I assume). I checked the object data in the console and the value indeed is not saving.
I tried adding this line with no success. Any idea why the value is not being accepted?
I made the field required to see what happens on save and it saves with the first value in the picklist not the value that is being displayed.
So for example I am only showing "Retail Sales" based on the snippet. When I hit save "Account Sales - Institutional Sales" is the value being saved which is the first value in the picklist.
Is it possible in your use case to default the value to Retail Sales on the model via a condition or model action? Or, if you make it required, can you add None as an option so people have to choose one?
Also, I think it might work better to instead of removing all the values that don't equal Retail Sales, use a render snippet that declares these are all the values of my picklist: 'Retail Sales'.
Just had another idea that might be a way around this:
Add two picklists, one that renders if the running user's Sales Category = 'Retail Sales', and one that renders if it doesn't. On the retails sales picklist, you could override the metadata so that the only option is Retail Sales, and make it required.
Unless your field is in a table...