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

[Request] get data as javascript object #318

Closed
llvasconcellos opened this issue Oct 30, 2016 · 6 comments · Fixed by #414
Closed

[Request] get data as javascript object #318

llvasconcellos opened this issue Oct 30, 2016 · 6 comments · Fixed by #414

Comments

@llvasconcellos
Copy link

formData is xml or json String but I really needed the javascript object itself. I'm using node.js / mongodb and stringfy / parse the json data string is pointless.
My advise for the next version is add getters and setters for all data in the object / class / component.
For now I altered form-builder.js and added a getData action:

// Make actions accessible
    formBuilder.actions = {
      clearFields: _helpers.removeAllfields,
      showData: _helpers.showData,
      save: _helpers.save,
      addField: function addField(field, index) {
        _helpers.stopIndex = $sortableFields[0].children.length ? index : undefined;
        prepFieldVars(field);
        document.dispatchEvent(formBuilder.events.fieldAdded);
      },
      removeField: _helpers.removeField,
      setData: function setData(formData) {
        _helpers.removeAllfields();
        _helpers.getData(formData);
        loadFields();
      },
///////////  RETURN DATA AS JAVASCRIPT OBJECT ////////////////
      getData: function(){
        return _helpers.prepData(document.getElementById(opts.formID))
      } 
    };
@kevinchappell
Copy link
Owner

Internally formBuilder uses an array of objects as the formData. You can get access to this from jQuery's data.

const formBuilder = $('.editor').formBuilder().data('formBuilder');

console.log(formBuilder.formData);

@llvasconcellos
Copy link
Author

@kevinchappell formData is not an Array of objects. It's a String representation of this array. That's the point I'm talking about. I should be able to get this array and not the String. You should at least have both. Something like getData() and getJsonData().

@kevinchappell
Copy link
Owner

How about getData(type)? Where type is either xml, json, or js.

@llvasconcellos
Copy link
Author

@kevinchappell That's great! Maybe let default to js since it's is the only that you don't need to apply any transformation to the data. Like:

getData()  => js array of objects
getData('xml') => xml
getData('json') => json

@nareshananthoju
Copy link

I downloaded j query form builder project.In this i am getting code xml format .but i need code in html format .i replaced xml to html.but getting below error.What i have to do? can u solve fast?

Uncaught TypeError: fb.actions.html is not a function
at Object.getData (demo.js:184)

@llvasconcellos
Copy link
Author

@nareshananthoju if you want HTML can't you just do getElementById('something').innerHTML?

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

Successfully merging a pull request may close this issue.

3 participants