tags:

views:

807

answers:

1

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).

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 ?

A: 

I'll assume YAHOO.example.Data.menu.breakfasts is a reference to an array.

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:

var bDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.menu.breakfasts.concat('add-item'));

See the JavaScript concat function for more details.

Simon Lieschke