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 = newContentPane(),
    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(newValidationTextBox({
                id: this.textboxId,
                name: this.name,
                value: this.value
            }, this.textboxNode));
        }
    });

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

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

});
<linkrel="stylesheet"href="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css"media="screen"><scriptsrc="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script><divid="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");

functionbuild() {
    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?"