使用yui将新项添加到组合框中
Can anybody help me, please ? (sorry for my english, I'm french) I've a combobox and i want insert an item "add-item" before read an array of data that populate my combobox. To sum-up : 1- Adding to my combobox "add-item" (add before read the array), 2- Adding to my combobox data from my array (it's ok for this part).
请问有人帮帮我吗? (对不起我的英文,我是法国人)我有一个组合框,我想在读取填充我的组合框的数据数组之前插入一个项目“add-item”。总结:1-添加到我的组合框“add-item”(在读取数组之前添加),2-从我的数组中添加我的组合框数据(这部分没问题)。
Here my code :
这是我的代码:
<h3>What would you like for breakfast?</h3>
<div id="bAutoComplete">
<input id="bInput" type="text"> <span id="toggleB"></span>
<div id="bContainer"></div>
<input id="myHidden" type="hidden">
</div>
<script type="text/javascript" src="assets/datafab.js"></script>
YAHOO.namespace("example.container");
YAHOO.example.Combobox = function() {
// Instantiate DataSources
var bDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.menu.breakfasts);
// Optional to define fields for single-dimensional array
bDS.responseSchema = {fields : ["name"]};
// Instantiate AutoCompletes
var oConfigs = {
prehighlightClassName: "yui-ac-prehighlight",
useShadow: true,
queryDelay: 0,
minQueryLength: 0,
animVert: .01
}
var bAC = new YAHOO.widget.AutoComplete("bInput", "bContainer", bDS, oConfigs);
bAC.resultTypeList = false;
// Breakfast combobox
var bToggler = YAHOO.util.Dom.get("toggleB");
var oPushButtonB = new YAHOO.widget.Button({container:bToggler});
var toggleB = function(e) {
//YAHOO.util.Event.stopEvent(e);
if(!YAHOO.util.Dom.hasClass(bToggler, "open")) {
YAHOO.util.Dom.addClass(bToggler, "open")
}
// Is open
if(bAC.isContainerOpen()) {
bAC.collapseContainer();
}
// Is closed
else {
bAC.getInputEl().focus(); // Needed to keep widget active
setTimeout(function() { // For IE
bAC.sendQuery("");
},0);
}
}
oPushButtonB.on("click", toggleB);
bAC.containerCollapseEvent.subscribe(function(){YAHOO.util.Dom.removeClass(bToggler, "open")});
My array datasource come from datafab.js (we have :donuts, yogurt, ...), so i need to add the item "add-item" before this list. The item "add-item" should not be in the array datafab.js Is it possible with a yui function ?
我的数组数据源来自datafab.js(我们有:甜甜圈,酸奶......),所以我需要在此列表前添加“add-item”项。项目“add-item”不应该在数组datafab.js中是否可以使用yui函数?
1 个解决方案
#1
I'll assume YAHOO.example.Data.menu.breakfasts
is a reference to an array.
我假设YAHOO.example.Data.menu.breakfasts是对数组的引用。
Try changing the line:
尝试更改线路:
var bDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.menu.breakfasts);
to:
var bDS = new YAHOO.util.LocalDataSource(['add-item'].concat(YAHOO.example.Data.menu.breakfasts));
If you want add-item
to appear at the end of your combobox, use:
如果您希望在组合框的末尾显示add-item,请使用:
var bDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.menu.breakfasts.concat('add-item'));
See the JavaScript concat
function for more details.
有关更多详细信息,请参阅JavaScript concat函数。
更多相关文章
- 如何通过ajax将javascript数组传递给YII动作
- JavaScript 函数柯里化(参考《JavaScript模式》)
- 在JavaScript中的for循环中调用异步函数
- Javascript学习之匿名函数与自执行详解
- 第三节(JavaScript 对象、日期,函数)
- 用javaScript编写的验证函数只运行一次?
- 无法从按钮onclick事件ASP.NET 4调用Javascript函数
- 在Javascript中将带有空格的字符串数组转换为小写,然后在Webkit中
- 返回指定时间段相同间隔数组