翻页实现复选框的选中问题

在选中的时候,点击绑定按钮,执行以下方法:

 1 function bangding(){    
 2                     
 3          checkedIds="";//翻页保存选中的id                             
 4              var oneches=document.getElementsByName("landDkbh");
 5              for(var i=0;i<oneches.length;i++){
 6                  if(oneches[i].checked==true){
 7                      //避免重复累计id (不含该id时进行累加)
 8                      if(checkedIds.indexOf(oneches[i].value)==-1){
 9                          checkedIds=checkedIds+oneches[i].value+",";
10                      
11                      }
12                  }
13                  if(oneches[i].checked==false){
14                      //取消复选框时 含有该id时将id从全局变量中去除
15                      if(checkedIds.indexOf(oneches[i].value)!=-1){
16                          checkedIds=checkedIds.replace((oneches[i].value+","),"");
17                      }
18                  }
19              }
20               for(var i=0;i<oneches.length;i++){                 
21                      if(checkedIds.indexOf(oneches[i].value)!=-1){
22                          oneches[i].checked=true;
23                      }
24                  }
25               
26               location.href="baodan.do?getLandList&currentPage="+currentpage+"&userQygs="+userQygs+"&landName="+landName+"&first=2&str1="+checkedIds;
27         
28           }
29           

后台的getLandList方法:

 1     if (first == 1) {
 2             
 3             // 第一次进入列表页面,需要新建session
 4             Map<Integer, List<LandUser>> flag_map = new HashMap<Integer, List<LandUser>>();
 5             request.getSession().setAttribute("jld_session", flag_map);
 6         }
 7            //將地块编号字符串分成数组,并添加到列表中
 8         if (str1 != "" && str1 != null) {
 9 
10             String[] str = str1.split(",");// 用split()函数直接分割
11             List<LandUser> landList = new ArrayList<LandUser>();
12             for (int i = 0; i < str.length; i++) {
13 
14                 LandUser lu = landUserBiz.getName(str[i]);
15                 landList.add(lu);
16             }
17 
18             if (str != null && str.length > 0) {
19                 //获取session 内容
20                 Map<Integer, List<LandUser>> map = (Map<Integer, List<LandUser>>) request.getSession().getAttribute("jld_session");
21                 //如果map中已经不包含这一页,则添加到map中,否则,移除这一页,将这一页的新内容添加进来。
22                 if (!map.containsKey(page.getCurrentPage())) {
23                     map.put(page.getCurrentPage(), landList);
24                 } else {
25 
26                     map.remove(page.getCurrentPage());
27                     map.put(page.getCurrentPage(), landList);
28                 }
29                 //设置session的map属性
30                 request.getSession().setAttribute("map", map);
31                 //如果map部位空的话,遍历map,并将map中的地块编号的值放到另一个list中
32                 if (map != null) {
33                     List<LandUser> list = new ArrayList<LandUser>();
34                     Iterator iter = map.entrySet().iterator();
35                     while (iter.hasNext()) {
36                         Map.Entry<Integer, List<LandUser>> entry = (Entry<Integer, List<LandUser>>) iter.next();
37                         List<LandUser> landList2 = entry.getValue();
38 
39                         for (int i = 0; i < landList2.size(); i++) {
40 
41                             list.add(landList2.get(i));
42 
43                         }
44                     }
45 
46                     m.addAttribute("list", list);
47                     request.getSession().setAttribute("selectedFiles", list);
48                 }
49             }
50         }

前台将获得的后台的list处理:

 1   <%   List<LandUser> selectedFiles = (List<LandUser>)request.getSession().getAttribute("selectedFiles");
 2             %>
 3      
 4     function initPage(){
 5    
 6            <%
 7               if(selectedFiles!=null){
 8                System.out.println("返回数量"+selectedFiles.size());
 9               for(int i=0;i<selectedFiles.size();i++){
10               %>
11              var inputs = document.getElementsByTagName("input");
12            
13             for(var j= 0;j<inputs.length;j++){
14              var input = inputs[j];
15               if(input.type=="checkbox"){
16                     if(input.value=="<%=(String)selectedFiles.get(i).getLandDkbh()%>"){                     
17                               input.checked = true;                           
18                             }
19                     }
20                  }
21             <%}
22             }%>
23       } 

 

posted @ 2017-06-01 15:30  xiaotian_小天  Views(1416)  Comments(0Edit  收藏  举报