Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use dynamic schemas for multiple fields #22

Closed
bernhardh opened this issue Mar 30, 2016 · 6 comments
Closed

How to use dynamic schemas for multiple fields #22

bernhardh opened this issue Mar 30, 2016 · 6 comments

Comments

@bernhardh
Copy link

I am just wondering, how to add multiple fields based on one other field using the schemaResolver.

Lets say, I have a selectbox "type" with 2 options type1 and type2. Depending on this selectbox, I want to show 2 different sets of additional fields. I have tried the following:

var typeSchemas = {
    "type1": {
        "title": {
            "type": "string",
            "title": "Title 1"
        },
        "gender": {
            "type": "string",
            "title": "Gender",
            "enum": [
                "m",
                "f"
             ],
        },
    },
    "type2": {
        "title": {
            "type": "string",
            "title": "Title 2"
        },
        "otherfield": {
            "type": "string",
            "title": "Some other otherfield"
        },
    }
};
var myDiv = document.getElementById("#myDiv");
var bf = brutusin["json-forms"].create({
    "$schema": "http://json-schema.org/draft-03/schema#",
    "type": "object",
    "properties": properties = {
    "type": {
        "type": "string",
        "title": "Plugin-Typ",
        "description": "Choose the type of the next formfields",
        "required": true,
        "enum": [
            "type1",
            "type2"
        ]
    },
    "nextfields": {
        "dependsOn": [
            "type"
        ]
    }
}
});
bf.schemaResolver = function (names, data, cb) {
    var schemas = new Object();

    schemas["$.data"] = typeSchemas[data.type];
    setTimeout(function(){cb(schemas)},500);
};
bf.render(myDiv);

But thats not working, since the schemas["$.data"] has to be a single property and not an set of properties. Is there any possibility to solve this?

@idelvall
Copy link
Member

Maybe this works for you:

  1. Set type1and type2values as schemas in themselves:
var typeSchemas = {
  "type1": {
    "type": "object",
    "properties": {
      "title": {
        "type": "string",
        "title": "Title 1"
      },
      "gender": {
        "type": "string",
        "title": "Gender",
        "enum": [
          "m",
          "f"
        ]
      }
    },

  },
  "type2": {
    "type": "object",
    "properties": {
      "title": {
        "type": "string",
        "title": "Title 2"
      },
      "otherfield": {
        "type": "string",
        "title": "Some other otherfield"
      }
    }
  }
};

and change the resolver to:

function (names, data, cb) {
    var schemas = new Object();  
    schemas["$.nextfields"] = typeSchemas[data.type];
    cb(schemas);
}

@bernhardh
Copy link
Author

Thank you for your reply!
Ok, maybe I am stupid and can't transfer your suggestions to the code, but please have a look at:
https://jsfiddle.net/ajzmgkh4/

@idelvall
Copy link
Member

The reason is that this schema is different to the previous one (it does not have a nextfields property)

@bernhardh
Copy link
Author

Ok, the short answer is: Yes I am too stupid to transfer your example. :)

Just for documentation purpose: https://jsfiddle.net/ajzmgkh4/1/

Thank you for your help!

@idelvall
Copy link
Member

:)

@hxh-robb
Copy link

hxh-robb commented Jan 18, 2018

https://jsfiddle.net/hxh_robb/xk663z01/

I've added a new feature called "appendedProperties" to meet this requirement without using schemaResovler. Since schemaResovler is a kind of behavior for "dynamic" additional properties rendering(What I mean "dynamic" is that the additional properties definition is a CRUD record, the schemaResovler might made an ajax call for that and generate a returned schema). "appendedProperties" is much more suit for that if one just need a "fixed" additional properties definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants