以下方法是我用于自己项目中的一个例子, 方法实现左右移动, 移动之后是否排序? 以及上下移动功能.
同时支持左边内容包括字符前缀情况, 移到右边后需要去掉前缀.
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;
}
}
}