Skip to content

how to add enum in templates , its not showing combo box for templates #473

Closed
@sumancholleti

Description

@sumancholleti
No description provided.

Activity

josdejong

josdejong commented on Oct 7, 2017

@josdejong
Owner

These enum dropdowns are a feature that you can use by providing a JSON Schema enumeration, so you could use it in combination with a template.

sumancholleti

sumancholleti commented on Oct 8, 2017

@sumancholleti
Author
var schema = {
	    "title": "Example Composite Schema",
	    "oneOf": [
	    	{
	      	"type": "object",
	      	"properties": {
	        	"enumerable_prop": {
	          		"type": "string",
	              "enum": ["first", "second", "third"]
	          }
	        }
	      },
	      {
	      	"type": "array"
	      }
	    ]
	  };
	  
var options = {
name: 'UconnectMessage',
schema: schema,
templates: [
    {
        text: 'text',
        title: 'Insert a Message',
        className: 'any',
        field: 'notify,
        value: {
        	enumerable_prop:null
        }
    }

Can you give an example, I tried this , its not showing dropdown

josdejong

josdejong commented on Oct 9, 2017

@josdejong
Owner
sumancholleti

sumancholleti commented on Oct 10, 2017

@sumancholleti
Author

Can json be an object instead?

josdejong

josdejong commented on Oct 10, 2017

@josdejong
Owner

yes. just play around with it and you will see

sumancholleti

sumancholleti commented on Oct 10, 2017

@sumancholleti
Author

I tried and it didn't work

josdejong

josdejong commented on Oct 10, 2017

@josdejong
Owner

Keep trying :)

You will have to adjust both JSON and JSON schema, these need to be aligned with each other.

sumancholleti

sumancholleti commented on Oct 10, 2017

@sumancholleti
Author

No luck yet
var schema = {
"type": "object",
"properties": {
"gender": {
"enum": ["male", "female"]
}
}
};

var json = {};
  

var templates = [
  {
    text: 'Employee',
    title: 'Insert a new employee',
    className: 'any',
    field: 'employee',
    value: {
      gender: '',
    }
  }
]

var options = {
  schema: schema,
  templates: templates
};

// create the editor
var container = document.getElementById('jsoneditor');
var editor = new JSONEditor(container, options, json);
josdejong

josdejong commented on Oct 11, 2017

@josdejong
Owner

I think the difficulty here is that the enum dropdown only works when the JSON matches the JSONSchema, whilst a template can be inserted everywhere. In your example code the enum for gender only works for a property in the root object, and not for nested objects. When inserting an employee via the template, you create a nested object - which is not matching the JSON Schema enum.

sumancholleti

sumancholleti commented on Oct 11, 2017

@sumancholleti
Author

I didn't get you

josdejong

josdejong commented on Oct 16, 2017

@josdejong
Owner

The schema that you post:

var schema = {
  "type": "object",
    "properties": {
    "gender": {
      "enum": ["male", "female"]
    }
  }
};

will only create a dropdown for the field gender in the root of the JSON object like:

{
  "gender": "male"
}

It will NOT create a dropdown not for nested objects like here:

{
  "someNestedObject": {
    "gender": "male"
  }
}

if you need that, you will need to adjust your JSON Schema to have the definition of gender there where it will be created in your JSON object. If you want a dropdown for gender everywhere in the object (on all levels), you will have to define a JSON Schema with recursion like discussed here.

ortrud

ortrud commented on Jan 17, 2018

@ortrud

I have followed this conversation and tried everything you suggested, but I can not get a dropdown to show up anywhere but on the top level of the schema for an enum property like gender.

let demographicsSchema = {
	"type": "object",
       	"properties": {
       		"firstName": { "type": "string", "minLength" : 1 },
          	//"lastName": { "type" : ["string","null"] },
       		//"gender": { "enum": ["Male", "Female"] },
          	"lastName": { "type" : "string" },
		"gender" : {"$ref" : "gender"  } ,
       		"age": { "type": "number", "minimum" : 0, "default" : 40},
       		"maritalStatus": { "type": "string", "enum" : ["M","S","DV","WD","MA","NM","SE"] },
       		"cin": { "type": "string" },
       		"sufficientInfoForCIN": { "type": "string" },
       		"middleNameOrInitial": { "type": "string" },
       		"nameSuffix": { "type": "string" },
       		"nameVerified": { "type": "string" },
       		"ssn": { "type": "string", minLength:9, maxLength:9 },
       		"ssnStatus": { "type": "string", "default" : "VR" },
	},
	"required": ["firstName", "gender", "age"]
};

let genderSchema = {
	"type": "string",
       	"enum": ["Male", "Female"] 
};

Do you have plans to make dropdown available on any level?
Also, what is the relation between jsoneditor and json-editor projects. I like jsoneditir, but json-editor does handle enums a sdropdowns on any level of the schema

josdejong

josdejong commented on Jan 18, 2018

@josdejong
Owner

@ortrud have you tried the example that I posted here? http://jsbin.com/ciqifub/edit?html,output

There is no relation between jsoneditor and json-editor except that they are both editors for JSON :)

If your json schema defines enums on any level, then JSONEditor will display them on any level - that's a matter of how you define your json schema.

SteveNeithardt

SteveNeithardt commented on Aug 22, 2018

@SteveNeithardt

screenshot_2018-08-22 js bin

I have the same issue, where the enums drop downs never seem to appear within objects in arrays.

Here's the screenshot of the example you posted on jsbin.

22 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @josdejong@ortrud@sumancholleti@chriscarreau@SteveNeithardt

        Issue actions

          how to add enum in templates , its not showing combo box for templates · Issue #473 · josdejong/jsoneditor