Javascript控制Listbox左右/上下移动方法集合

以下方法是我用于自己项目中的一个例子, 方法实现左右移动, 移动之后是否排序? 以及上下移动功能.

同时支持左边内容包括字符前缀情况, 移到右边后需要去掉前缀.

 

function moveItemsLeftRight(src, des, orderInd, prefix, addPrefixInd){
    var source = document.getElementById(src);
    var destination = document.getElementById(des);
    var arraySelected = new Array();
   
    for(var i = 0; i < source.length; i++){
       
        if(source.options[i].selected){
            var opt = null;
           
            if(prefix != null){
                var prefixLen = prefix.length;
               
                if(addPrefixInd == true){
                    // Add Prefix
                    opt = new Option(prefix + source.options[i].text, source.options[i].value);
                }
                else{
                    // Remove Prefix
                    if(source.options[i].text.substr(0, prefixLen) == prefix){                   
                        opt = new Option(source.options[i].text.substr(prefixLen, source.options[i].text.length - prefixLen), source.options[i].value);
                    }
                }
               
            }
            else{
                opt = new Option(source.options[i].text, source.options[i].value);
            }
           
            if(opt != null){
                destination.options.add(opt);               
                arraySelected[arraySelected.length] = [source.options[i].value, source.options[i].text];
            }
        }
       
    }
   
    deleteItems(source, arraySelected);
   
    if(orderInd == true){
        sortItems(destination, prefix);
    }
}

function deleteItems(src, arraySelected){
    for(var i = 0; i < arraySelected.length; i++){
        for(var j = 0; j < src.length; j++){
            if(src.options[j].value == arraySelected[i][0]){
                src.remove(j);
            }
        }
    }
}

function sortItems(src, prefix){
    var arrayTemp = new Array();
   
    for(var j = 0; j < src.length; j++){
        arrayTemp[j] = [src.options[j].value, src.options[j].text];
    }
   
    arrayTemp.sort(sortOption);
   
    removeAllItems(src);
   
    for(var m = 0; m < arrayTemp.length; m++){
        src.options.add(new Option(arrayTemp[m][1], arrayTemp[m][0]));
       
        if(prefix != null){
            if(src.options[m].text.substr(0, prefix.length) != prefix){
                src.options[m].style.background = "#cccccc";
                src.options[m].style.color = "#000099";
            }
        }
    }
}

function sortOption(a, b){
    return a[0] - b[0];
}

function removeAllItems(src){
    for(var j = src.options.length - 1; j >= 0; j--){
        src.remove(j);
    }
}

function removeAllSelectedItems(src){
    document.SelectClientID(src).find("option:selected").each(function(){
            $(this).removeAttr("selected","false");});
}

function moveItemsUpDown(src, direction){
    var source = document.getElementById(src);
    var arrayTemp = new Array();
    var arrayIndex = new Array();
   
    if(direction == "UP"){   
        for(var i = 0; i < source.length; i++){       
            if(source.options[i].selected && i != 0){
                var opt = arrayTemp[i - 1];
                arrayTemp[i - 1] = new Option(source.options[i].text, source.options[i].value);
                arrayTemp[i] = opt;
                arrayIndex[i-1] = 1;
                arrayIndex[i] = 0;
            }
            else{
                arrayIndex[i] = 0;
                arrayTemp[i] = new Option(source.options[i].text, source.options[i].value);
            }
        }
    } else {
        for(var i = source.length - 1; i >= 0; i--){
            if(source.options[i].selected && i != source.length - 1){
                var opt = arrayTemp[i + 1];
                arrayTemp[i + 1] = new Option(source.options[i].text, source.options[i].value);
                arrayTemp[i] = opt;
                arrayIndex[i+1] = 1;
                arrayIndex[i] = 0;
            }
            else{
                arrayIndex[i] = 0;
                arrayTemp[i] = new Option(source.options[i].text, source.options[i].value);
            }
        }
    }
   
    removeAllItems(source);
   
    for(var m = 0; m < arrayTemp.length; m++){
        source.options.add(new Option(arrayTemp[m].text, arrayTemp[m].value));
       
        if(arrayIndex[m] == 1){
            source.options[m].selected = true;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值