Javascript “构建”菜单或组合框,其中数据存储在;“动态”;排列
我想在我的界面上添加一个组合框或菜单按钮,其中包含来自数组的数据。数组中的数据并不总是相同的,所以我需要将按钮设置为“动态”。我怎样才能做到这一点?如果数组更改,我无法指定模型 编辑:我使用的是ExtJS 6,所以我需要能够输入菜单参数:Javascript “构建”菜单或组合框,其中数据存储在;“动态”;排列,javascript,arrays,extjs,combobox,Javascript,Arrays,Extjs,Combobox,我想在我的界面上添加一个组合框或菜单按钮,其中包含来自数组的数据。数组中的数据并不总是相同的,所以我需要将按钮设置为“动态”。我怎样才能做到这一点?如果数组更改,我无法指定模型 编辑:我使用的是ExtJS 6,所以我需要能够输入菜单参数: menu: [{ text:'Menu Item 1' },{ text:'Menu Item 2' },{ text:'Menu Item 3' }] 例如: 1) 用户选择菜单项并单击发送按钮 2) 根据用户
menu: [{
text:'Menu Item 1'
},{
text:'Menu Item 2'
},{
text:'Menu Item 3'
}]
例如:
1) 用户选择菜单项并单击发送按钮
2) 根据用户单击的项目的值,创建一个javascript数组,其中有时包含2个字段,有时包含10个字段
3) 字段显示在新菜单按钮或组合框中
谢谢你的帮助 HTML
<input type="text" name="example" list="exampleList">
<datalist id="exampleList">
<option value="A">A</option>
<option value="B">B</option>
</datalist>
A.
B
Javascript
function generateOptionsFromDynamicArray (arr) {
// Get the datalist by id, cache option variable
var datalist = document.querySelector('#exampleList'),
option;
// Remove old options
while( datalist.firstChild ) {
datalist.removeChild(datalist.firstChild);
}
// Loop through array
for (var i = 0, l = arr.length; i < l; ++i) {
option = document.createElement('option');
option.setAttribute('value', arr[i]);
option.innerText = arr[i];
datalist.appendChild(option);
}
}
从DynamicCarray(arr)生成函数{
//按id获取数据列表,缓存选项变量
var datalist=document.querySelector(“#exampleList”),
选项
//删除旧选项
while(datalist.firstChild){
datalist.removeChild(datalist.firstChild);
}
//循环阵列
对于(变量i=0,l=arr.length;i
我创建了一个函数,您可以将动态数组传递到该函数中,以更新组合框,在本例中,我使用的是数据列表。
关于这段代码的几点,您应该添加一个检查,以确保arr是一个数组或“类似数组”(我正在看您的typedarrays)。在进行这些更改时,还应该从DOM中删除数据列表,这样就不会在每次迭代时重新绘制DOM
我已经为测试创建了一个
<input type="text" name="example" list="exampleList">
<datalist id="exampleList">
<option value="A">A</option>
<option value="B">B</option>
</datalist>
A.
B
Javascript
function generateOptionsFromDynamicArray (arr) {
// Get the datalist by id, cache option variable
var datalist = document.querySelector('#exampleList'),
option;
// Remove old options
while( datalist.firstChild ) {
datalist.removeChild(datalist.firstChild);
}
// Loop through array
for (var i = 0, l = arr.length; i < l; ++i) {
option = document.createElement('option');
option.setAttribute('value', arr[i]);
option.innerText = arr[i];
datalist.appendChild(option);
}
}
从DynamicCarray(arr)生成函数{
//按id获取数据列表,缓存选项变量
var datalist=document.querySelector(“#exampleList”),
选项
//删除旧选项
while(datalist.firstChild){
datalist.removeChild(datalist.firstChild);
}
//循环阵列
对于(变量i=0,l=arr.length;i
我创建了一个函数,您可以将动态数组传递到该函数中,以更新组合框,在本例中,我使用的是数据列表。
关于这段代码的几点,您应该添加一个检查,以确保arr是一个数组或“类似数组”(我正在看您的typedarrays)。在进行这些更改时,还应该从DOM中删除数据列表,这样就不会在每次迭代时重新绘制DOM
我创建了一个用于测试的函数。您无法侦听数组
更改事件,因此每当您更改数组时,您还必须调用以下函数:
function arrayToStore(array) {
var store = Ext.getCmp("myComboId").getStore();
//var store = Ext.getStore("myComboStore");
store.removeAll();
store.add(array.map(function(item) {
// This maps the array entry into a model you can add to the store.
// Instead of "field1", use your field name.
// If your array contains an object, you can preprocess it here.
// "field1" would be the default value if you have defined
// an implicit store by providing an array in the combo config:
// store:['item1','item2','item3'],
return {
field1:item
};
});
}
您无法侦听数组change
事件,因此无论何时更改数组,都必须调用以下函数:
function arrayToStore(array) {
var store = Ext.getCmp("myComboId").getStore();
//var store = Ext.getStore("myComboStore");
store.removeAll();
store.add(array.map(function(item) {
// This maps the array entry into a model you can add to the store.
// Instead of "field1", use your field name.
// If your array contains an object, you can preprocess it here.
// "field1" would be the default value if you have defined
// an implicit store by providing an array in the combo config:
// store:['item1','item2','item3'],
return {
field1:item
};
});
}
谢谢你的回答!我忘了指定使用ExtJS创建按钮和界面。我会更新我的问题。谢谢你的回答!我忘了指定使用ExtJS创建按钮和界面。我会更新我的问题。