您的位置:首页 > 其它

关于分页选中复选框的基本思想(默认全选)

2009-04-01 22:13 267 查看
1、产生一个set集合用于存放页面传来的value,首先初始化set,遍历从后台取得的list并迭代除所有手机号存放在set中。
2、产生一个新的list2,再次遍历list,通过set.contains()方法,判断list中的手机号码是否在set中存在;如果存在则设置bean(这里是BlackEntity)中flag为"1",否则flag为
"0";然后把所有的bean放入list2中,把list2放入request范围内,跳转到页面。页面迭代list2,显示所有信息。其中包括复选框
这里复选框这样设置:
<input type='checkbox' value='${ent.mobileNo }' name='no' onclick="pageSelect(this)"<logic:equal name="ent" property="flag" value="1">checked</logic:equal>>
我用的是struts标记,同样不管用什么标记,只要能判断bean中的属性flag等于"1",那么就checked;
3、迭代产生的每个复选框都有一个onclick事件(用于把复选框的value通过Ajax<这里用的jquery>发送到ACTION中);
函数如下:
function pageSelect(va)
{
$.ajax({
type: "POST",
url: "readBlackFileAction.do?va="+va.value
});
}
4、在action中接受va的值,迭代set中所有元素、定义局部变量boolean flag = true(用于判断set中是否存在va的值),开始判断set中是否有va的值(也就是手机号码,注:set

不能为空,va不能为空,这里要判断一下,因为第一次进action肯定为空),如果set中存在flag = false;break;
如果flag=true(也就是set中不存在va的值),就把va放入set中,否则除去va的值;

不到之处敬请修改!~~~~~~~

实例如下:
Action:

private Set set = new HashSet();;
public ReadBlackFileActionAction() {

}
private Logger logger = Logger.getLogger(ReadBlackFileActionAction.class);
&nb
1939f
sp;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm dynaForm = (DynaActionForm)form;
HttpSession session = request.getSession();
String va = request.getParameter("va");
BlackService service = new BlackService();
FormFile file = (FormFile)dynaForm.get("myfile");
String fileName = file.getFileName();
String fileType = fileName.substring(fileName.lastIndexOf(".")+1).toUpperCase();//获取文件类型
try {

Iterator it = set.iterator();
boolean flag = true;//定义变量,用于判断set中时候存在va的值
if (set.size() > 0) {
while (it.hasNext()) {
String tmp = (String) it.next();
//判断set中是否有va的值
if (tmp != null && !"".equals(tmp) && tmp.equals(va)) {
//va的值存在
flag = false;
break;
} else {
continue;
}
}
}
//如果set中不存在va的值,就把va放入set中,否则除去va的值
if (flag && va != null && !"".equals(va)) {
set.add(va);
} else{
set.remove(va);
}
List resultList = service.checkFileData(file.getInputStream(), fileType);
//1、初始化set,遍历从后台取得的list并迭代除所有手机号存放在set中
if(set.size()<=0)
{
Iterator it2 = resultList.iterator();
while(it2.hasNext())
{
BlackEntity entity = (BlackEntity) it2.next();
set.add(entity.getMobileNo().trim());
}
}

List list2 = new ArrayList();//产生一个新的list
Iterator listIt = resultList.iterator();
// 判断list中的手机号码是否在set中存在;如果存在则设置bean(这里是BlackEntity)中flag为"1",否则flag为
// "0";然后把所有的bean放入list2中
while (listIt.hasNext()) {
BlackEntity dto = (BlackEntity) listIt.next();
System.out.println("到这来了" + set.contains(dto.getMobileNo()));
if (set.contains((dto.getMobileNo()).trim())) {
System.out.println("进来了吗?");
dto.setFlag("1");
} else {
dto.setFlag("0");
}
list2.add(dto);
}
session.setAttribute("set", set);
request.setAttribute("resultList", list2);
return mapping.findForward("success");
}catch (Exception e) {
e.printStackTrace();
logger.error("上传文件校验失败 " + e);
request.setAttribute("error", "上传文件校验失败," + e.getMessage());
return mapping.findForward("error");
}
}

Jsp页面:
<%@ page contentType="text/html; charset=gb2312"%>
<%@ include file="../../common/taglibs.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Expires" CONTENT="0" />
<meta http-equiv="Cache-Control" CONTENT="no-cache" />
<meta http-equiv="Pragma" CONTENT="no-cache" />
<title>导入黑名单</title>
<link href="<%=request.getContextPath()%>/css/styles.css"
rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath()%>/css/extremecomponents.css"
rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath()%>/css/rightStyles.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="<%=request.getContextPath()%>/javascript/dateUtil.js" type="text/javascript"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/javascript/jquery.js"></script>
<style type="text/css">
<!--
body {
background-color: #C7E1F4;
}
.style1 {color: #FF0000}
.style2 {
font-size: 24px;
font-family: "仿宋_GB2312";
font-weight: bold;
}
.test {
position: relative;
height: 23px;
width: 128px;
left: 0px;
top: 0px;
}
.date_text {
font-family: "宋体";
font-size: 12px;
width: 150px;
cursor: hand;
}
-->
</style>

<script type="text/javascript">
function pageSelect(va){
$.ajax({
type: "POST",
url: "readBlackFileAction.do?va="+va.value
});
}

function checkInput(){
var mobileFile = document.getElementById('myfile');
if(mobile.value == "" && mobileFile.value==""){
error.innerHTML = "";
error.innerHTML = "手机号和文件必选其一!";
mobile.focus();
return false;
}
if(mobileFile.value!=""){
if(!checkFileType(mobileFile.value)){
error.innerHTML = "";
error.innerHTML = "文件格式不正确,只能是txt或xls文件";
mobileFile.focus();
return false;
}
}
document.form1.submit();
sumb.disabled =true;
return true;
}
// 检查文件格式
function checkFileType(str){
var len = str.length;
if(len > 4){
var fileType = str.substring(len-4,len).toUpperCase();

if(fileType ==".TXT" || fileType == ".XLS") {
return true;
}
}
return false;
}
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14" height="25" bgcolor="#FFFFFF" class="content_topleft"> </td>
<td width="944" align="left" valign="top" bgcolor="#FFFFFF"
class="content_topright">
<table width="100%" height="20" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="1%"> </td>
<td width="49%" align="left" valign="top">
<table width="35%" height="20" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td> </td>
<td width="10" id="t1" class="piclleft_on"></td>
<td align="center" valign="middle" nowrap="nowrap"
class="picmid_on" id="t2">导入黑名单</td>
<td height="20" width="10" id="t3" class="picrright_on"></td>
</tr>
</table>
</td>
<td width="50%"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#C7E1F4" class="content_left"> </td>
<td align="center" valign="top" bgcolor="#C7E1F4">
<table width="100%" border="0" cellpadding="0" cellspacing="0"
class="content_right">
<tr>
<td align="center" valign="middle">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<html:form action="/view/operationManage/blackRollCall/readBlackFileAction.do" method="post" enctype="multipart/form-data" >
<table align="left" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td align="center" valign="middle" nowrap> 文件名:</td>
<td align="left">
<html:file property="myfile"></html:file> <html:errors property="myfile"/>
</td>
<td align="center" valign="middle" nowrap>  
<input type="submit" name="sumb" value=" 显 示 " id="sumb" class="button" onclick="return checkInput()" />
</td>
<td>
<span align="center" id="error" class="style1">
</span>
</td>
</tr>
</table>
</html:form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="280" bgcolor="#FFFFFF" class="content_left"> </td>
<td height="480" align="center" valign="top" bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<ec:table items="resultList" var="ent" action="readBlackFileAction.do" imagePath="${pageContext.request.contextPath}/images/table/*.gif" title="非签约手机号查询" width="100%" rowsDisplayed="20" style="">
<ec:row highlightRow="">
<ec:column width="200" title="手机号码" property="mobileNo" />
<ec:column title="回复日期" property="date" />
<ec:column title="选择" property="flag" >
<input type='checkbox' value='${ent.mobileNo }' name='no' onclick="pageSelect(this)"
<logic:equal name="ent" property="flag" value="1">checked</logic:equal>
>
</ec:column>
</ec:row>

</ec:table>
</td>
<td class="background_right"></td>
</tr>
<tr>
<td colspan="2" align="left">
<input align="left" type="button" class="button" value="导出文件" onclick="window.location.href=''">
</td>
</tr>
</table>

</td>
</tr>
</table>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息