brutusin / json-forms

JSON Schema to HTML form generator, supporting dynamic subschemas (on the fly resolution). Extensible and customizable library with zero dependencies. Bootstrap add-ons provided
http://brutusin.org/json-forms
Apache License 2.0
606 stars 168 forks source link

dependsOn in array is not working properly with resolver #107

Open KiSchulte opened 6 years ago

KiSchulte commented 6 years ago

Hi,

I've created a Form with couple of array's. One of them has three dropdown where the second depends on the first and the third on the second. I've created a resolver for this to get the right values for the dropdowns from a existing hierarchical object with three dimensions. This is working like expected so far. But the datastore is not updated like it should.

After I added the first item in the array, all three values are present in the datastore if I check them with getData(). Then with adding the next item, the issue starts. After adding the item to the array the previous entry in the datastore has no property two and three anymore, booth are deleted by adding the next item to the array.

First item added: { "gender": "Female", "services": [ { "category": "Beauty & Wellness", "service": "Beauty Salon", "specialization": "Nail Salon" } ] }

Next Item added { "gender": "Female", "services": [ { "category": "Beauty & Wellness" }, { "category": "Beauty & Wellness", "service": "Hairdresser" } ] }

And this repeats on every adding. Only the last added item has the properties two and three the previous one are losing the properties.

Here is my resolver: ` const resolver = function(names, data, cb) { let schemas = []; let schema = [];

        let i = data.services.length - 1;
        let category = data.services[i].category;
        let serviceDef = false;
        if (names[0] === '$.services[#].specialization' && data.services[i].hasOwnProperty('service')) {
            serviceDef = data.services[i].service;
        }

        let serviceObj = [];

        if (names[0] === '$.services[#].service' && services[category]) {
            schema.type = "string";
            Object.keys(services[category]["services"]).forEach(function(key, value) {
                serviceObj.push(key);
            },services[category]["services"]);
        } else if (serviceDef && services[category]['services'][serviceDef]) {
            schema.type = "string";
            services[category]['services'][serviceDef]["specializations"].forEach(function(key, value) {
                serviceObj.push(key);
            });
        }
        if (serviceObj.length === 0) schema.type = null;
        schema.enum = serviceObj;
        schemas[names[0]] = schema;
        setTimeout(function(){cb(schemas)},500);
    };

`

This resolver handles booth dropdown dependencies. I tried already to use the selector '$.services[$].service' instead of '$.services[#].service', so I thought this will just catch the last element in the array, but with this the dependencies are not builded correctly anymore.

I hope I can manage to set up a jsfiddle environment with the whole setting, so it will be easier to check it. Maybe it's also a problem in my resolver, maybe I set a wrong schema here?

Any help will be appreciated.

cheers

KiSchulte commented 6 years ago

Ok, I had a deeper look into the topic. Seems that this will not work at all at the moment. As there is no information about the added array Elements in the schemaMap, there is no possibility to address the certain array element. Which results in the issue that for cleanData the id of the array is given and therefore all array elements are cleaned up on dependencyChange and not only the actual chosen one. The only solution I see here, to fix this, is to integrate array Elements in the schemaMap and add the id in the input.

stutteringp0et commented 6 years ago

I'm in the same boat here...

stutteringp0et commented 6 years ago

I altered onDependencyChanged to add the source variable to the shemaresolver output, but it's pretty useless. the source element ID doesn't reflect the heirarchy of the data attributes.

idelvall commented 6 years ago

https://github.com/brutusin/json-forms/blob/master/README.md#status