0

我正在使用dojo ajax 编写一些用于级联下拉列表的代码,第一个下拉列表是静态的,第二个是从servlet 获取数据.. 我正在使用dijit.form.ComboBox 进行下拉列表。Dojo 提供了 Store 属性,他在其中存储数据,然后将其放入组合框中。在servlet中,我通过数组列表到ajax函数..在ajax函数中,我用逗号分隔数组并在变量中存储,然后存储在dojo的存储属性中但我无法填充整个字符串..它只填充最后一个我正在使用以下代码的字符串的值

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

        <link rel="stylesheet" href="dojo/dijit/themes/claro/document.css">
        <link rel="stylesheet" href="dojo/dijit/themes/claro/claro.css" />
        <script src='dojo/dojo/dojo.js' data-dojo-config=' parseOnLoad: true'></script>
        <script>



            require(["dojo/parser", "dijit/form/ComboBox","dijit/form/TextBox"]);

      function abc(){    

          var j = document.getElementById('state').value
          dojo.xhrPost({
    // The URL to request
    url: "populate",   //servlet name
    timeout :  3000 ,
    content: {
        username: dojo.byId("state").value

    },

    load: function(result) {    // the value in result is like=[Abas Store, Accounts ]

    require([
    "dojo/ready", "dojo/store/Memory", "dijit/form/ComboBox"
], function(ready, Memory, ComboBox){

     var ss=result.split(",");

     var i;
     for (i=1;i< ss.length ;i++){


    var stateStore = new Memory({

        data: [ {name:ss[i], id: ss[i]}  ]
    });
     }

    ready(function(){
        var comboBox = new ComboBox({
            id: "stateSelect",
           name:"select",
            value: "Select",
            store: stateStore,
            searchAttr: "name"
        }, "stateSelect");
    });
});

      }
});
      }

        </script>
    </head>
    <body class="claro">

     <select data-dojo-type="dijit.form.ComboBox" id="state" name="state"  onchange="abc();">
    <option selected >Andaman Nicobar</option>
    <option>Andhra Pradesh</option>        
 <option>Tripura</option>
 <option>Uttar Pradesh</option>
 <option>Uttaranchal</option>
 <option>West Bengal</option>

</select>
        <input id="stateSelect"  >
                   </select>


    </body>
</html>

请给我解决方案..填充组合框中的所有值,这是我从数组列表中获得的

4

1 回答 1

0

您正在 for 循环中构建商店。相反,您应该构建传递给 MemoryStore 构造函数的数据数组。

require(["dojo/store/Memory", "dijit/form/ComboBox", "dojo/ready"], 
    function(Memory, ComboBox, ready){

        ready(function) {

           dojo.connect(dijit.byId('state'), 'onChange', function() {

               var stateSelect = dijit.byId('stateSelect');
               if (!stateSelect) {
                   stateSelect = new ComboBox({
                     id: "stateSelect",
                     name:  "select",
                     value: "Select...",
                     searchAttr: "name"
                   }, "stateSelect");
               }

               var ss = 'Abas Store, Accounts';
               ss = ss.split(',');

               var data = [];
               dojo.forEach(ss, function(item, idx) {
                   data.push({
                       id: idx,
                       name: item                    
               });
               stateSelect.set('store', new Memory({data: data}));

          }); // dojo.connect
       }); // ready     
}); // require

​</p>

The working example is at http://jsfiddle.net/cswing/DLNNc/

于 2012-07-17T17:00:14.267 回答