angular-schema-form icon indicating copy to clipboard operation
angular-schema-form copied to clipboard

Pre-fill model value - set multiple values

Open bcbirkhauser opened this issue 8 years ago • 1 comments

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"
            }
          ]
        },

bcbirkhauser avatar Nov 22 '17 04:11 bcbirkhauser

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.

scottux avatar Aug 12 '18 14:08 scottux