List Move Options - Javascript

>> Thursday, March 22, 2012

/**
* Moves selected list options from "fromList" to "toList".
*
* @param fromList
* @param toList
* @return
*/
function moveListOption(fromList, toList) {
    if (fromList == null || toList == null) {
        return;
    }
    if (fromList.selectedIndex == -1) {
        return;
    }
    var fromID = '', fromText = '', title = '';
    for ( var i = fromList.options.length - 1; i >= 0; i--) {
        if (fromList.options[i].selected == true) {
            fromID = fromList.options[i].value;
            fromText = fromList.options[i].text;
            title = fromList.options[i].title;
            var newRow = new Option(fromText, fromID);
            newRow.title = title;
            toList.options[toList.length] = newRow;
            fromList.options[i] = null;
        }
    }
    sortOptionsList(toList);
}

/**
* Sorts the options in the given list in ascending order.

*
* @param list
* @return
*/
function sortOptionsList(toList) {
    if(typeof toList == 'string') {
        toList = document.getElementById(toList);
    }
    var toValue = '', toText = '', toTitle = '';
    for ( var x = 0; x < toList.length - 1; x++) {
        for ( var y = x + 1; y < toList.length; y++) {
            if (toList[x].text > toList[y].text) {
                toValue = toList[x].value;
                toText = toList[x].text;
                toTitle = toList[x].title;
                toList[x].value = toList[y].value;
                toList[x].text = toList[y].text;
                toList[x].title = toList[y].title;
                toList[y].value = toValue;
                toList[y].text = toText;
                toList[y].title = toTitle;
            }
        }
    }
}

/**
* Select options (all or none) in the given list.
*
* @param list
* @param selectAll
* true/false
* @return
*/
function selectListOptions(list, selectAll) {
    if (typeof list == "string") {
        list = document.getElementById(list);
    }
    if (!list) {
        return;
    }
    var subAreaOptions = list.options;
    if (selectAll) {
        for ( var i = 0; i < subAreaOptions.length; i++) {
            subAreaOptions[i].selected = true;
        }
    } else {
        subAreaOptions.selectedIndex = -1;
    }
}

0 comments:

  © Blogger template Skyblue by Ourblogtemplates.com 2008

Back to TOP