两个listbox实现选项的添加删除和搜索


Posted in Javascript onMarch 01, 2013

两个listbox实现选项的添加,删除和搜索

贴一下主要的js代码,一些资源我就不传了。下面是效果图。

两个listbox实现选项的添加删除和搜索

两个listbox实现选项的添加删除和搜索

两个listbox实现选项的添加删除和搜索

两个listbox实现选项的添加删除和搜索

group.js

function addMember() 
{ 
//右侧选中的项添加到左侧 
var add=$("#newAddMembersId").val(); 
var del=$("#deleteMembersId").val(); 
var ext=$("#existedMemberId").val(); 
var newAddId=""; 
//获取新加的userId 
var addedUsers=$("#enterpriseMember input:checkbox[checked]"); 
for(var i=0;i<addedUsers.length;i++) 
{ 
var checkbox=$(addedUsers[i]); 
var uid=checkbox.next().val(); 
if(add.indexOf(uid)>=0 || (ext.indexOf(uid)>=0 && del.indexOf(uid)<0)) 
{ 
continue; 
} 
if(!(ext.indexOf(uid)>=0 && del.indexOf(uid)>=0)) 
{ 
if(add.indexOf(uid)<0) 
{ 
if(add.length==0) 
add+=uid; 
else 
add+=","+uid; 
} 
} 
//添加数据到左侧列表 
checkbox.removeAttr("checked"); 
var userDiv=checkbox.parent().parent().parent(); 
$("#groupMember").append(userDiv.clone()); 
del=del.replace(uid+",",""); 
del=del.replace(uid,""); 
$("#deleteMembersId").val(del); } 
$("#newAddMembersId").val(add); 
} 
function removeMember() 
{ 
//左侧移除成员 
var add=$("#newAddMembersId").val(); 
var del=$("#deleteMembersId").val(); 
var newRemoveId=""; 
var removedUsers=$("#groupMember input:checkbox[checked]"); 
for(var i=0;i<removedUsers.length;i++) 
{ 
var checkbox=$(removedUsers[i]); 
var uid=checkbox.next().val(); 
if(del.indexOf(uid)>=0) 
{ 
continue; 
} 
if(del.indexOf(uid)<0) 
{ 
if(del.length==0) 
del+=uid; 
else 
del+=","+uid; 
} 
//左侧列表中移除数据 
checkbox.removeAttr("checked"); 
var userDiv=checkbox.parent().parent().parent(); 
userDiv.remove(); 
//$("#enterpriseMember").append(userDiv); 
add=add.replace(uid + ",", ""); 
add=add.replace(uid, ""); 
$("#newAddMembersId").val(add); 
} 
$("#deleteMembersId").val(del); 
} 
//搜索成员,按名字,按部门 
function searchMember() 
{ 
var searchTxt=$.trim($("#searchTxt").val()); 
var memberLIsts=$(".dmlist"); 
//移除上次的结果 
var lastReusts=$(".tempResult"); 
for(var i=0;i<lastReusts.length;i++) 
{ 
$(lastReusts[i]).remove(); 
} 
//为空搜索显示全部列表 
if(searchTxt=="" || searchTxt=="搜索企业成员") 
{ 
//show all hidded ml 
for(var i=0;i<memberLIsts.length;i++) 
{ 
$(memberLIsts[i]).css("display",""); 
} 
return ; 
} 
//hide all memberlist,隐藏所有的列表 
for(var i=0;i<memberLIsts.length;i++) 
{ 
$(memberLIsts[i]).css("display","none"); 
} 
//搜索------------- 
var enterpriseMemberDiv=$("#enterpriseMember"); 
//search by name,add class:tempResult 
var names=$(".sname"); 
for(var i=0;i<names.length;i++) 
{ 
var name=$(names[i]).text(); 
if(name.indexOf(searchTxt)>=0) 
{ 
var result=$(names[i]).parent().parent().parent().clone(); 
result.addClass("tempResult"); 
result.css("display",""); 
enterpriseMemberDiv.append(result); 
} 
} 
//search by departmentName 
var departmentNames=$(".sdepartment"); 
for(var i=0;i<departmentNames.length;i++) 
{ 
var name=$(departmentNames[i]).text(); 
if(name.indexOf(searchTxt)>=0) 
{ 
var result=$(departmentNames[i]).parent().parent().parent().clone(); 
result.addClass("tempResult"); 
result.css("display",""); 
enterpriseMemberDiv.append(result); 
} 
} 
} 
function clearTxt() 
{ 
var txt=$.trim($("#searchTxt").val()); 
if(txt=="搜索企业成员") 
$("#searchTxt").val(""); 
} 
function setsearchTxt() 
{ 
var txt=$.trim($("#searchTxt").val()); 
if(txt=="") 
$("#searchTxt").val("搜索企业成员"); 
} 
//展开折叠器 
function folderToggle(event,basePath) 
{ 
var evnet=event||window.event; 
var arrowDiv=event.target||event.srcElement; 
var closeimg=basePath+"/images/folder_close.PNG"; 
var openimg=basePath+"/images/folder_open.PNG"; 
var srcval=$(arrowDiv).attr("src"); 
if(srcval==closeimg) 
$(arrowDiv).attr("src",openimg); 
if(srcval==openimg) 
$(arrowDiv).attr("src",closeimg); 
var listDiv=$(arrowDiv).parent().next().next().next(); 
listDiv.toggleClass("memberlist"); 
} 
//点击名字折叠 
function folderNameToggle(event,basePath) 
{ 
var evnet=event||window.event; 
var arrowDiv=event.target||event.srcElement; 
var closeimg=basePath+"/images/folder_close.PNG"; 
var openimg=basePath+"/images/folder_open.PNG"; 
var srcval=$(arrowDiv).attr("src"); 
if(srcval==closeimg) 
$(arrowDiv).attr("src",openimg); 
if(srcval==openimg) 
$(arrowDiv).attr("src",closeimg); 
var listDiv=$(arrowDiv).parent().next().next(); 
listDiv.toggleClass("memberlist"); 
} 
function folderNumToggle(event,basePath) 
{ 
var evnet=event||window.event; 
var arrowDiv=event.target||event.srcElement; 
var closeimg=basePath+"/images/folder_close.PNG"; 
var openimg=basePath+"/images/folder_open.PNG"; 
var srcval=$(arrowDiv).attr("src"); 
if(srcval==closeimg) 
$(arrowDiv).attr("src",openimg); 
if(srcval==openimg) 
$(arrowDiv).attr("src",closeimg); 
var listDiv=$(arrowDiv).parent().parent().next().next(); 
listDiv.toggleClass("memberlist"); 
} 

function setCss(event,onoff) 
{ 
var theclass=$.trim(onoff); 
var offclass="addmemberBtnoff"; 
var onclass="addmemberBtnon"; 
var event=event||window.event; 
var thisbtn=event.target||event.srcElement; 
$(thisbtn).removeClass(onclass); 
$(thisbtn).removeClass(offclass); 
if(theclass=="on") 
{ 
$(thisbtn).addClass(onclass); 
} 
else 
{ 
$(thisbtn).addClass(offclass); 
} 
}

test.jsp 
<%@ page language="java" pageEncoding="UTF-8"%> 
<%@ include file="/pages/common/Taglibs.jsp"%> 
<%@ include file="/pages/common/Header.jsp"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>添加群组成员</title> 
<script type="text/javascript" src="<%=basePath%>/script/group.js"></script> 
<script type="text/javascript"> 
$(document).keydown(function (event){ 
if(event && event.keyCode==13){ 
$("#searchBtn").click(); 
} 
}); 
function backpage(group_id){ 
//window.history.back(); 
window.location.href="<%=basePath%>/server/group?group_id="+group_id; 
} 
function closeWin() 
{ 
this.close(); 
} 
function saveChanges() { 
function okFun() { 
var deleteMembersId = $("#deleteMembersId").val(); 
var newAddMembersId = $("#newAddMembersId").val(); 
var group_id = $("#group_id").val(); 
window.location.href = "<%=basePath%>/server/group!saveGroupMember?group_id=" 
+ group_id 
+ "&newAddMembersId=" 
+ newAddMembersId 
+ "&deleteMembersId=" + deleteMembersId; 
}; 
function cancelFun() { 
}; 
//PopuMsgBox("确认提示","是否保存?",okFun,cancelFun,150,100); 
okFun(); 
} 
</script> 
</head> 
<body class="bgClass"> 
<div id="addGroupMembercontainer" style="padding:20px 40px;width:auto;height:auto;"> 
<div class="roundGroup" style="float:left;width:241px;height:380px;"> 
<div style="margin-bottom:5px;margin-top:10px;padding-right:2px;"> 
<span class="spangrouptitle" style="line-height:16px;color:#404754;font-weight:bold;font-size:13px;"> 群组<s:property value="group_name"></s:property> </span> 
<span id="menberCount" style="color:#838a97;"> ${gNumber}人</span><br/> 
<hr style="border: 1px dotted #C0D3DC"/> 
</div> 
<div id="groupMember" style="margin-right:8px;height:306px;overflow-y:auto;overflow-x:hidden;width:237px;"> 
<!-- 内容列表 --> 
<s:if test="#request.groupMemberList!=null && #request.groupMemberList.size!=0"> 
<s:iterator value="#request.groupMemberList" id="gmember"> 
<div style="padding-left:5px;padding-right:1px;"> 
<div style="height:16px;" onclick="toggleCheck(event);"> 
<div style="width:auto;float:left;"> 
<input type="checkbox" /> 
<input type="hidden" value="<s:property value="#gmember.id"/>" name="userId" /> 
<span style="color:#404754;"><s:property value="#gmember.name" /></span> 
</div> 
<div class="positionblue" style="float:right;width:auto;text-align:right;"> 
<span style="color:#838a97;"><s:property value="#gmember.position_name" /></span> 
</div> 
</div> 
<div style="clear:left;"></div> 
</div> 
</s:iterator> 
</s:if> 
</div> 
</div> 
<div style="float:left;width:80px;padding-top:150px;text-align:center;"> 
<div style="height: auto;"> 
<input class="addmemberBtn addmemberBtnon" onmouseover="setCss(event,'on')" onmouseout="setCss(event,'off')" type="button" value="<<添加" onclick="addMember()" /> 
</div> 
<br/> 
<div style="height:auto;"> 
<input class="addmemberBtn addmemberBtnon" onmouseover="setCss(event,'on')" onmouseout="setCss(event,'off')" type="button" value="移除>>" onclick="removeMember()" /> 
</div> 
</div> 
<div class="roundGroup" style="float:left;width:241px;height:380px;"> 
<div style="margin-bottom:5px;margin-top:10px;padding-right:2px;"> 
<span class="spangrouptitle" style="color:#404754;font-weight:bold;font-size:13px;"> 所有企业成员</span> 
<span style="color:#838a97;"> ${eNumber}人</span><br/> 
<hr style="border: 1px dotted #C0D3DC" /> 
</div> 
<div style="height:30px;padding-left:5px;"> 
<div style="height:14px;float:left;"><input id="searchTxt" style="width:215px;line-height:14px;color:#838a97;" type="text" value="搜索企业成员" onfocus="clearTxt()" onblur="setsearchTxt()"/></div> 
<div style="height:14px;float:left;margin-top:4px;"><img id="searchBtn" onclick="searchMember();" style="width:14px;height:14px;cursor:pointer;" src="<%=basePath%>/images/searchBtn.PNG"/></div> 
<div style="clear:both;"></div> 
</div> 
<div id="enterpriseMember" style="height:280px;overflow-y:auto;overflow-x:hidden;width:233px;"> 
<!-- 内容列表 --> 
<s:if test="#request.departmentMemberList!=null && #request.departmentMemberList.size!=0"> 
<s:iterator value="#request.departmentMemberList" id="department"> 
<div class="dmlist" style="padding-bottom:2px;"> 
<div style="float:left;height:13px;padding-top:0px;"><img class="folder" style="" src="<%=basePath%>/images/folder_close.PNG" onclick="folderToggle(event,'<%=basePath%>')"/></div> 
<div style="float:left;height:13px;"><span style="cursor:pointer;" onclick="folderNameToggle(event,'<%=basePath%>')"><s:property value="#department.departmentName"/><span style="color:#838a97;" onclick="folderNumToggle(event,'<%=basePath%>')"> <s:property value="#department.memberCount"/>人</span></span></div> 
<div style="clear:both;"></div> 
<div class="ml memberlist"> 
<s:iterator value="#department.memberList" id="emember"> 
<div style="padding-left:5px;padding-right:1px;"> 
<div style="height:16px;"> 
<div style="float:left;"> 
<input type="checkbox"/> 
<input type="hidden" value="<s:property value="#emember.id"/>" name="userId" /> 
<span class="sname" style="color:#404754;"><s:property value="#emember.name" /></span> 
</div> 
<div class="positionblue" style="float:right;text-align:right;height:16px;"> 
<span class="sdepartment" style="color:#838a97;"><s:property value="#emember.position_name" /></span> 
</div> 
</div> 
<div style="clear:both;"></div> 
</div> 
</s:iterator> 
</div> 
</div> 
</s:iterator> 
</s:if> 
</div> 
</div> 
<div style="clear:both;width:600px;"> 
<div style="text-align:right;"> 
<input class="commonButton" style="" type="button" value="保存" onclick="saveChanges()"/> 
<input type="hidden" id="newAddMembersId" name="newAddMembersId" value=""/> 
<input type="hidden" id="deleteMembersId" name="deleteMembersId" value=""/> 
<input type="hidden" id="existedMemberId" name="existedMemberId" value="${existedMemberId}"/> 
<input type="hidden" id="group_id" name="group_id" value="${group_id}"/> 
</div> 
</div> 
</div> 
</body> 
</html>
Javascript 相关文章推荐
window.showModalDialog使用手册
Jan 11 Javascript
JavaScript Event学习补遗 addEventSimple
Feb 11 Javascript
js操作时间(年-月-日 时-分-秒 星期几)
Jun 20 Javascript
jQuery Clone Bug解决代码
Dec 22 Javascript
Angular1.x复杂指令实例详解
Mar 01 Javascript
jQuery插件echarts实现的单折线图效果示例【附demo源码下载】
Mar 04 Javascript
在vue中封装可复用的组件方法
Mar 01 Javascript
详解如何在vue-cli中使用vuex
Aug 07 Javascript
写gulp遇到的ES6问题详解
Dec 03 Javascript
vue的for循环使用方法
Feb 12 Javascript
Vue 实现把表单form数据 转化成json格式的数据
Oct 29 Javascript
解决vant-UI库修改样式无效的问题
Nov 03 Javascript
DWZ table的原生分页浅谈
Mar 01 #Javascript
jQuery编辑器KindEditor4.1.4代码高亮显示设置教程
Mar 01 #Javascript
循环 vs 递归浅谈
Feb 28 #Javascript
JavaScript对象创建及继承原理实例解剖
Feb 28 #Javascript
jquery显示和隐藏div特效实例
Feb 27 #Javascript
JS等比例缩小图片尺寸的实例
Feb 27 #Javascript
JQuery切换显示的效果实例代码
Feb 27 #Javascript
You might like
全国FM电台频率大全 - 20 广西省
2020/03/11 无线电
dedecms模版制作使用方法
2007/04/03 PHP
php学习 字符串课件
2008/06/15 PHP
Laravel手动返回错误码示例
2019/10/22 PHP
Laravel 自定命令以及生成文件的例子
2019/10/23 PHP
js chrome浏览器判断代码
2010/03/28 Javascript
使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
2010/05/07 Javascript
非常好用的JsonToString 方法 简单实例
2013/07/18 Javascript
浅析jQuery(function(){})与(function(){})(jQuery)之间的区别
2014/01/09 Javascript
jQuery实现列表内容的动态载入特效
2015/08/08 Javascript
JavaScript编写检测用户所使用的浏览器的代码示例
2016/05/05 Javascript
基于JS实现textarea中获取动态剩余字数的方法
2016/05/25 Javascript
ES6新特性六:promise对象实例详解
2017/04/21 Javascript
Vue.js实现一个todo-list的上移下移删除功能
2017/06/26 Javascript
JavaScript中一些特殊的字符运算
2017/08/17 Javascript
webpack 插件html-webpack-plugin的具体使用
2018/04/09 Javascript
Vue 实现从小到大的横向滑动效果详解
2019/10/16 Javascript
Element Dropdown下拉菜单的使用方法
2020/07/26 Javascript
Vue触发input选取文件点击事件操作
2020/08/07 Javascript
[01:08:44]NB vs VP 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[01:46]2018完美盛典章节片——坚守
2018/12/17 DOTA
python实现简单遗传算法
2018/03/19 Python
django一对多模型以及如何在前端实现详解
2019/07/24 Python
python实现静态服务器
2019/09/05 Python
Python使用pymysql模块操作mysql增删改查实例分析
2019/12/19 Python
Python中的整除和取模实例
2020/06/03 Python
解决Pytorch自定义层出现多Variable共享内存错误问题
2020/06/28 Python
python 实现朴素贝叶斯算法的示例
2020/09/30 Python
理肤泉俄罗斯官网:La Roche-Posay俄罗斯
2018/07/24 全球购物
大学本科毕业生的自我鉴定
2013/11/26 职场文书
内勤岗位职责
2015/02/10 职场文书
导游词之山西祁县乔家大院
2019/10/14 职场文书
《学会生存》读后感3篇
2019/12/09 职场文书
PHP命令行与定时任务
2021/04/01 PHP
解决Swagger2返回map复杂结构不能解析的问题
2021/07/02 Java/Android
Java实现HTML转为Word的示例代码
2022/06/28 Java/Android