json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

Pre-fill model value - set multiple values #941

Open bcbirkhauser opened 6 years ago

bcbirkhauser commented 6 years ago

Question

I have multiple yes/no radio buttons and I have one that I want to use to set the values for all the others, a "All Yes/All No" radio button. I have a callback on the change to set the model values, but it doesn't actually update the ui. I also have a need to be able to pre-populate the form with saved answers if a user comes back to the form, which I assume will be a similar function.

How do I set the model value and get the ui to update with the correct values?

Here is what I'm doing when the user toggles the all yes/no radio.

//key = ['section', 'field']
toggleAll(key, value) {
    const section = key[0];
    _.each(this.$scope.schema.properties, (obj, k) => {
      if(k !== section + '.all') {
        this.$scope.model[section][k.replace(section + '.', '')] = value;
      }
    });
  }

Here is my form schema for that particular all yes/no field

{
          "key": "groupOne.all",
          "type": "radios",
          "onChange": "toggleAll(form.key, modelValue)",
          "titleMap": [
            {
              "value": "yes",
              "name": "All Yes"
            },
            {
              "value": "no",
              "name": "All No"
            }
          ]
        },
    {
          "key": "groupOne.questionOne",
          "title": "This is a placeholder question?",
          "type": "radios",
          "titleMap": [
            {
              "value": "Yes",
              "name": "Yes"
            },
            {
              "value": "No",
              "name": "No"
            }
          ]
        },
scottux commented 6 years ago

https://github.com/json-schema-form/json-schema-form/wiki/Documentation#copyvalueto

This might help. Other than possibly needing a $scope.$apply() though, I don't see anything wrong with your code or why it wouldn't work like you are expecting.