Open t27 opened 9 years ago
What is your JSON Schema code?
the schema for the default case is something like this
"properties" : {
"sport" : {
"type" : "string",
"enum": [
"football",
"formula1",
]
},
"keywords" : {
"type" : "string"
}
}
The "keywords" element is defined separately for each "sport" element, for example
Now i want to dynamically set the schema for the keywords element to be an enum with the relevant keywords depending on the "sport" value.
I have looked around the library code and have figured out a solution(more of a workaround actually) for this as detailed in the below steps.
removeObjectProperty('keywords');
on the parent object. Then delete the 'sport' element from the cached_editors as well.A sample code snippet
//For step 1 and 2 there's already a ton of good and easy to understand documentation on the readme page with code so i'll skip them
//the object which defines the dynamic enum values I have to populate//step4
var keyword_mapping={ "football":[ "teams","ball","goal"],"formula1":["car","racing","driver"]};
$("select[name='root[parent][sport]']").on("change",function(evt){//event listener in Step 3 and 4
var current_sport=evt.target.value;
var fc=editor.getEditor("root.parent");//editor is the main Json_editor object//Step 5
fc.original_schema.properties.keywords["enum"]= keyword_mapping[current_sport];//Step 6
fc.schema.properties.keywords["enum"]= keyword_mapping[current_sport];//Step 6
fc.removeObjectProperty('keywords');//Step 7
//delete the cache
delete fc.cached_editors.keywords;//Step 7 // IMP!!
fc.addObjectProperty('keywords');//Step 8
}
Do let me know if there's a better way to do this, as of now the above method works according to what i require, I hope it can help others too
I have two fields 1.Enum by default 2.String by default I want to dynamically convert field 2 to an enum depending on the value of field1. I am currently attempting to get the editor for field2 using getEditor('path.to.field2') and then changing the schema variable json, but since the field has already been rendered as text, i cant get it to convert to a enum in the UI, please can anybody help me here? Thanks!