Search code examples
javascriptdojodijit.form

Programmatically declaring the values of a dijit.form.select


I'm trying to declare through a JSON declaration the options for a markup-declared dijit.form.Select widget. Based on what i've read through in the API documentation, it appears that you could pass a new store using setStore() and then it should update, but that has not produced any useful results besides an empty Select widget with a full store. I am wondering if there's some declaration missing from my object, if i'm using the wrong methods, or if there are any other ways to declare these options.

The test markup I've been using is printed below:

<!DOCTYPE HTML>
<html dir="ltr">

    <head>
        <style type="text/css">
            body, html { font-family:helvetica,arial,sans-serif; font-size:90%; }
        </style>
        <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js"
        djConfig="parseOnLoad: true">
        </script>
        <script>
            dojo.require("dijit.form.Select");
            dojo.require("dojo.data.ItemFileReadStore");
            dojo.require("dojo.data.ItemFileWriteStore");
        </script>
        <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/claro/claro.css" />
    </head>

    <body class=" claro ">

        <div class="option" id="option" dojoType="dijit.form.Select"></div>

        <script type="text/javascript">
            dojo.addOnLoad(function() {
                if (document.pub) {
                    document.pub();
                }

                var selectOptions = {
                    name:'selection', 
                    identifier: 'label',
                    options: [{
                        label: 'this',
                        value: '01'
                    },
                    {
                        label: 'that',
                        value: '02'
                    },
                    {
                        label: 'the other',
                        value: '03'
                    },
                    {
                        label: 'banana',
                        value: '04',
                    },
                    ]};

                    var aStore = dojo.data.ItemFileReadStore({data: selectOptions});
                    dijit.byId("option").setStore(aStore, 01);
                    dijit.byId("option").startup();

            });
        </script>
    </body>

</html>

Solution

  • As @missingno mentioned, using a store for this is probably overkill. You can just use

    dijit.form.select.addOption(/*array of options*/).
    

    From your example it would be:

    var listOfOptions = [{
        label: 'this',
        value: '01'
      },
      {
        label: 'that',
        value: '02'
      },
      {
        label: 'the other',
        value: '03'
      },
      {
        label: 'banana',
        value: '04',
      },
      ];
    dijit.byId("option").addOption(listOfOptions);