【问题标题】:Programmatically declaring the values of a dijit.form.select以编程方式声明 dijit.form.select 的值
【发布时间】:2011-08-23 20:08:36
【问题描述】:

我正在尝试通过 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>

【问题讨论】:

    标签: javascript dojo dijit.form


    【解决方案1】:

    正如@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);
    

    【讨论】:

    • 这是我最终使用的。谢谢。
    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多