Javascript控制Listbox左右/上下移动方法集合
2010-08-12 17:14
561 查看
以下方法是我用于自己项目中的一个例子, 方法实现左右移动, 移动之后是否排序? 以及上下移动功能.
同时支持左边内容包括字符前缀情况, 移到右边后需要去掉前缀.
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;
}
}
}
同时支持左边内容包括字符前缀情况, 移到右边后需要去掉前缀.
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;
}
}
}
相关文章推荐
- JavaScript控制listbox列表框的项目上下移动的方法
- jQuery实现通过方向键控制div块上下左右移动的方法【测试可用】
- JavaScript控制两个列表框listbox左右交换数据的方法
- javascript实现listbox左右移动;
- javascript上下方向键控制表格行选中并高亮显示的方法
- 【JavaScript】列表元素上下左右移动:Select和Option的应用
- JavaScript强化教程——DOM编程(两种控制div移动的方法)
- vs2005制做左右上下移动的Listbox ( asp.net , C#)
- 【JavaScript】列表(Select)选项(Option)的移动(上下左右)
- java事件机制之通过上下左右键来控制小球为位置的移动演示
- js实现按键球,小球随键盘上下左右控制移动
- JavaScript强化教程——DOM编程(两种控制div移动的方法)
- 上下左右 键控制人移动
- Javascript控制DIV上下移动(仿265移动特效)
- <Unity3D>上下左右 键控制人移动
- 使用javascript操作多选列表框,实现动态增加删除,左右移动,上下排序移动等功能。
- 上下左右 键控制人移动
- js 键盘控制表格input上下左右移动
- JavaScript弹出新窗口并控制窗口移动到指定位置的方法
- jQuery实现用方向键控制层的上下左右移动