[Answer]-Dojo. How to add attributes in form elements?

1πŸ‘

βœ…

I found two ways to do this. I hope they help someone else.

First the dojo way

<script>
    require(["dojo/parser",
    "dijit/form/Select",
    "dojo/dom"], function (parser, Select, dom) {
        dojo.attr(dom.byId("id_birth_date_month"), {"data-dojo-type":dijit/form/Select",       
                                                    "data-dojo-id": "monthSelect"});
    // similar lines for day and year
     });
</script>

    <!-- ...later in the code-->   
    <select  id="id_birth_date_month">
    ...
    </select>

    <!-- two other similar selects for day and year-->

This is a form that it is loaded inside a floating pane so parser.parse() etc have already run. I am not sure why declarative syntax works after the parsing has been done but this approach worked.

The second way is from django. Define the attributes in the form field itself. This is the approach I took.

class SignupForm(Form):

# other form fields
    birth_date = DateField(widget=SelectDateWidget(years=range(yearNow - 13, yearNow - 100, -1),attrs={"data-dojo-type": "dijit/form/Select", "data-dojo-id": "birthDateSelect"}), required=True, initial="", label=_('Birth Date'))
# other form fields

Unfortunately all three fields (year, month day) get the same data-dojo-id and I don’t know if this is a problem. for the time being it works and I don’t see anything strange.

πŸ‘€voger

Leave a comment