3

我正在尝试通过 JSON 声明声明标记声明的 dijit.form.Select 小部件的选项。根据我在 API 文档中阅读的内容,您似乎可以使用 setStore() 传递一个新商店,然后它应该更新,但除了具有完整商店的空 Select 小部件之外,这并没有产生任何有用的结果。我想知道我的对象是否缺少某些声明,是否使用了错误的方法,或者是否有任何其他方法来声明这些选项。

我一直在使用的测试标记打印在下面:

<!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>
4

2 回答 2

7

正如@missingno 所提到的,为此使用商店可能是矫枉过正。你可以使用

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

从您的示例来看,它将是:

var listOfOptions = [{
    label: 'this',
    value: '01'
  },
  {
    label: 'that',
    value: '02'
  },
  {
    label: 'the other',
    value: '03'
  },
  {
    label: 'banana',
    value: '04',
  },
  ];
dijit.byId("option").addOption(listOfOptions);
于 2011-08-25T12:23:44.747 回答
2

我认为你错过了初始化你的商店。这些选项看起来像用于初始化普通选择的选项。尝试做类似的事情:

http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html

var store_options = {
    identifier: 'label',
    label: 'label',
    items: [
        {label: 'this', value:'01'},
        //...
    ]
}

var store = dojo.data.ItemFileWriteStore({data: store_options})

OTOH,您确定需要为此使用商店吗?除非您想使用特定的商店功能(如通知),否则您可以在以编程方式创建选择时直接传递数据:http: //dojotoolkit.org/reference-guide/dijit/form/Select.html

于 2011-08-23T21:23:41.240 回答