2011-08-23 75 views
3

我試圖通過JSON聲明聲明標記聲明的dijit.form.Select小部件的選項。根據我在API文檔中讀到的內容,看起來你可以使用setStore()傳遞一個新的商店,然後它應該更新,但除了帶有完整商店的空選擇小部件外,還沒有產生任何有用的結果。我想知道是否有一些聲明從我的對象丟失,如果我使用了錯誤的方法,或者如果有任何其他方式來聲明這些選項。以編程方式聲明dijit.form.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> 

回答

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); 
+0

這是最終我最終使用。謝謝。 –

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,你確定你需要使用一個商店呢?除非你想使用特定的商店功能(比如通知),否則只要在編程時創建select,就可以直接傳遞數據:http://dojotoolkit.org/reference-guide/dijit/form/Select.html

相關問題