Skip to content Skip to sidebar Skip to footer

How To Add A `HTML Label` Before A `ValidationTextBox` Dynamically?

I need to add dynamically HTML labels before each ValidationTextBox. ValidationTextBox and HTML label are created accordingly to the number of property present for object data. I w

Solution 1:

Using a home made widget is the proper approach for that.

require(['dijit/form/ValidationTextBox', 'dijit/layout/ContentPane', 'dijit/_WidgetBase', 'dijit/_TemplatedMixin', 'dojo/_base/declare', 'dojo/domReady!'], function(ValidationTextBox, ContentPane, _WidgetBase, _TemplatedMixin, declare, domReady) {
     var data = {
        name: 'a',
        surname: 'b',
        age: 'c'
    },
    layout = new ContentPane(),
    LabelTextbox = declare([_WidgetBase, _TemplatedMixin], {
        label: '',
        textboxId: '',
        name: '',
        value: '',
        
        templateString: '<div><label>${label}</label><div data-dojo-attach-point="textboxNode"></div></div>',
        
        postCreate: function() {
            this.inherited(arguments);
        
            this.own(new ValidationTextBox({
                id: this.textboxId,
                name: this.name,
                value: this.value
            }, this.textboxNode));
        }
    });

    Object.keys(data).forEach(function (prop, index) {
        layout.addChild(new LabelTextbox({
            textboxId: prop + '-'+ index,
            name: prop,
            value: data[prop],
            label: 'foo'
        }));
    }.bind(this));

    
    layout.placeAt('layout');
    layout.startup();

});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css" media="screen">
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<div id="layout"></div>

Solution 2:

I found a solution using dojo.place().

Still interested to understand are there alternative ways to achieve the same result.

http://jsfiddle.net/F2qAN/101/

dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.layout.ContentPane");
dojo.require("dojo");

function build() {
    var data = {
        name: 'a',
        surname: 'b',
        age: 'c'
    },
    validationTextBox,
    layout = new dijit.layout.ContentPane({});
    Object.keys(data).forEach(function (prop, index) {
        validationTextBox = new dijit.form.ValidationTextBox({
            id: prop + '-'+ index,
            name: prop,
            value: data[prop]
        });
        dojo.place("<label>MY LABEL</label>", layout.containerNode);
        layout.addChild(validationTextBox);
    }.bind(this))
    layout.placeAt('layout');
    layout.startup();
}

dojo.addOnLoad(build);

Post a Comment for "How To Add A `HTML Label` Before A `ValidationTextBox` Dynamically?"