Newbie
Newbie

Reputation: 361

populate a form from json (dojo)

I have a json {"Name":"@@","Phone":"9999999999","EMail":"[email protected]"} i want to fill the values from this object to a form...how to do that? here is the form

<form data-dojo-type="dojox.form.Manager" name="form" id="form" method="post">
<tr><td>
<input type="text" required="true" name="Name" id="Name" data-dojo-type="dijit.form.ValidationTextBox"/></tr></td>
<tr><td>
<input type="text" name="Phone" id="Phone" data-dojo-type="dijit.form.ValidationTextBox"/></tr></td>
<tr><td>
<input type="text" name="Email" id="Email" data-dojo-type="dijit.form.ValidationTextBox"/></tr></td>
</form>

Thank you

Upvotes: 1

Views: 2627

Answers (3)

Mithlesh Kumar
Mithlesh Kumar

Reputation: 758

To set value in form try this:-

var obj = {"Name":"@@","Phone":"9999999999","EMail":"[email protected]"};
dijit.byId('form').set('value',obj);

I know its late but its working:-

Upvotes: 2

Royston Shufflebotham
Royston Shufflebotham

Reputation: 970

dojox.form.Manager includes dijit.form._FormMixin which provides a .setValues function. You should be able to call form.setValues with your JSON object. The advantage of this is that it'll correctly call .set("value", ...) on each of the widgets properly to ensure that the Dijit widgets have the new values. Simply assigning the '.value' property on the DOM node may well bypass the Dijit logic.

There's an equivalent .getValues on _FormMixin too, which does the reverse.

Upvotes: 0

Jeremy Lawson
Jeremy Lawson

Reputation: 483

I'd use a simple javascript loop over the json.

var data = JSON.parse(jsonDataString);
for (index in data) {
    var el = document.getElementById(index);
    if (el)
        el.value = data[el];
}

Upvotes: 0

Related Questions