json-editor icon indicating copy to clipboard operation
json-editor copied to clipboard

Enumsource with $ref

Open allampalli opened this issue 7 years ago • 5 comments

Hi, im using enumsource with $ref to load data from ajax call. but it is showing empty dropdown. below is my code snippet.

"type": "object", "required": ["value"], "properties": { "valuev": { "type": "string", "enumSource": [{

"source": { "$ref": "ajaxcall"}, "title": "{{item.title}}", "value": "{{item.id}}" }]

} }

$ref is returning below response.

[{"id":1,"title":"value1"},{"id":2,"title":"value2"}]

but empty dropdown is rendering. could you please help me to get rid of this

allampalli avatar Nov 28 '16 07:11 allampalli

I have exactly the same problem.

sebthemonster avatar Dec 28 '16 17:12 sebthemonster

I tried 2 different methods to get right dropdowns.

  • fetching json file of the schema with fetch API : does not work
  • set schema directly in editor declaration : does not work

I created gist with my attempts code.

But the problem, that is this schema working well in demo page

I really don't understand. And I really need this tool for my current project...

Help me please :)

sebthemonster avatar Dec 28 '16 19:12 sebthemonster

I have found my problem. It was my declaration of selectize that could not work because of no integration of selectize resources in <head>.

My schema works well with $ref in schema declaration with select2 but not with selectize.

I done some tests and i confirm that enumSource does not work with selectize.

sebthemonster avatar Dec 29 '16 13:12 sebthemonster

Managed to do so.

if(this.enumSource[i].source && typeof this.enumSource[i].source == "object") {
  var src = this.jsoneditor.expandRefs(this.enumSource[i].source);
  this.enumSource[i].source = [];
  for (var j in src)
  {
    this.enumSource[i].source.push(src[j]);
  }
}

Goes at the end of this for loop.

Unfortunately, $ref ignores three path in external links, so http://example.com/test.json#/nodename will still be evaluated as http://example.com/test.json's root.

And also the order is lost because of conversion to object.

desertkun avatar Jan 31 '17 16:01 desertkun

Thanks @desertkun. To get this to work for properties where the value is "number" type (i.e. not "string") I had to also add a call to self.typecast here

// Rendered value
if (this.enumSource[i].value) {
    item_values[j] = self.typecast(this.enumSource[i].value({
        i: j,
        item: item
    }));
}

mstrong64 avatar Dec 21 '18 05:12 mstrong64