gpt4 book ai didi

javascript - 如何使用 ajax 调用填充选择框,该调用使用 dojo 返回字符串列表?

转载 作者:行者123 更新时间:2023-11-28 06:51:12 25 4
gpt4 key购买 nike

我想使用dojo填充选择框。我的 Spring Controller 返回一个 stringarraylist,我想将所有字符串放入我的选择框中。

    var currencyStore = new RequestMemory({
target: "getCurrency"
});
var os = new ObjectStore({ objectStore: currencyStore });
var currencyCombo = new Select({
store: os
}, "currency").startup();

但是上面的代码选择框是空的。我做错了什么?

最佳答案

使用 dojo/Storedijit/form/Select

首先我要声明的是,我对 Spring 没有真正的经验,所以我将放弃你的说法,即你有一个 ArrayListString s 并且您希望它们成为 dijit/form/Select 中的选项。如果您不想使用 dijit 小部件或者您想使用常规 <select>标签没有 dojo 支持,那么您正在处理另一个解决方案。

话虽这么说,您可以使用我在上面的评论中链接的示例:A Select Fed By A Store为了更轻松地完成将您的数据集放入 <select>并能够利用小部件的其他优点。

require([
"dijit/form/Select",
"dojo/data/ObjectStore",
"dojo/store/Memory",
"dojo/_base/array",
"dojo/dom",
"dojo/html",
"dojo/domReady!"
], function(
Select,
ObjectStore,
Memory,
array,
dom,
html
) {
"use strict";

/*
We used the domReady! plugin so this code runs
after the DOM has finished loading
*/

// Predefine
var strings, idStrings, store, objectStore, select;

/*
Strings is going to be the "arrayList of Strings" that you
mention that you receive from your spring controller
*/
strings = [
"hello",
"world",
"foo",
"bar"
];

/*
We are going to map your String array so that it has an
"id" property and a "label" property which will get used
by the Store and the Select
*/
idStrings = array.map(strings, function(str, index) {
return {
id: index,
label: str
};
});

// Make a Memory Store
store = new Memory({
data: idStrings
});

// Make an ObjectStore from the Memory
objectStore = new ObjectStore({
objectStore: store
});

// Create the Select and set its store to the one we just made
select = new Select({
name: "testSelect",
store: objectStore
}, "testSelect");

// We need to run startup on programatically created widgets
select.startup();

// Add change listener for funzies
select.on("change", function() {
// Get the label from the store by the value of this Select
var label = store.get(this.get("value")).label;

// Set the innerHTML of our logger
html.set(dom.byId("logger"), label);
});
});
<!-- Include the dijit claro theme -->
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css">

<!-- Set our dojoConfig -->
<script>
var dojoConfig = {
parseOnLoad: false,
isDebug: true,
async: 1
};
</script>
<!-- Include the dojo 1.10.4 CDN -->
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>


<body class="claro">
<main>
<!-- This Select is going to be replaced by our widget -->
<select id="testSelect"></select>

<!-- We're going to put output in this div -->
<div role="log" id="logger"></div>
</main>
</body>

此外,如果您不想使用 dijit/form/Select您可以将选项插入 <select>您自己可以使用 native JavaScript 方法,如 document.createElement("option");然后手动调整属性,但由于您已经使用带宽来加载 dojo,您不妨通过使用其 DOM 操作方法(例如 dojo/domConstruct.create())来使事情变得更容易。 .

没有 dijit/form/Select

require([
"dojo/_base/array",
"dojo/dom",
"dojo/dom-construct",
"dojo/domReady!"
], function (
array,
dom,
domConstruct,
) {
"use strict";

var strings, select;

// We have our strings
strings = [
"hello",
"world",
"foo",
"bar"
];

// Get our select
select = dom.byId("testSelect");

// Iterate each string and put it into an option under our select
array.forEach(strings, function (str, index) {
domConstruct.create("option", {
innerHTML: str,
value: index
}, select);
});
});
<小时/>

旁注

您可能不需要使用 dojo/_base/array 。我这样做是出于支持旧浏览器的习惯,而且我们已经在使用 dojo。另外,您可能对模块的去向有自己的设置,并且希望使用某种构建层而不是使用 CDN,因此您应该重构它,以便它不仅仅是像我在本示例中所做的一些内联脚本。

关于javascript - 如何使用 ajax 调用填充选择框,该调用使用 dojo 返回字符串列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32952214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com