Search code examples
jsonrestvue.jsvuejs2vue-resource

Load form data via REST into vue-form-generator


I am building a form, that needs to get data dynamically via a JSON request that needs to be made while loading the form. I don't see a way to load this data. Anybody out here who can help?

JSON calls are being done via vue-resource, and the forms are being generated via vue-form-generator.

export default Vue.extend({
  template,

  data() {
    return {
      model: {
        id: 1,
        password: 'J0hnD03!x4',
        skills: ['Javascript', 'VueJS'],
        email: 'john.doe@gmail.com',
        status: true
      },

      schema: {
        fields: [
          {
            type: 'input',
            inputType: 'text',
            label: 'Website',
            model: 'name',
            maxlength: 50,
            required: true,
            placeholder: companyList
          },
        ]
      },

      formOptions: {
        validateAfterLoad: true,
        validateAfterChanged: true
      },
      companies: []
    };
  },

  created(){
    this.fetchCompanyData();
  },

  methods: {
    fetchCompanyData(){
      this.$http.get('http://echo.jsontest.com/key/value/load/dynamicly').then((response) => {
        console.log(response.data.company);
        let companyList = response.data.company; // Use this var above
      }, (response) => {
        console.log(response);
      });
    }
  }

}); 

Solution

  • You can just assign this.schema.fields.placeholder to the value returned by the API like following:

      methods: {
        fetchCompanyData(){
          this.$http.get('http://echo.jsontest.com/key/value/load/dynamicly').then((response) => {
            console.log(response.data.company);
            this.schema.fields.placeholder = response.data.company
          }, (response) => {
            console.log(response);
          });
        }
      }