Select選擇後,刷新頁面保存上一次選擇內容

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!doctype html>
<html class="no-js">
<head>
<base href="<%=basePath%>" />
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>-用戶管理</title>
<meta name="description" content="這是一個 index 頁面">
<meta name="keywords" content="index">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="icon" type="image/png" href="assets/i/favicon.png">
<link rel="apple-touch-icon-precomposed"
href="assets/i/[email protected]">
<meta name="apple-mobile-web-app-title" content="" />
<link rel="stylesheet" href="assets/css/bootstrap-combined.min.css">

<link rel="stylesheet" href="assets/css/amazeui.min.css" />
<link rel="stylesheet" href="assets/css/admin.css">
<link rel="stylesheet" href="assets/css/app.css">

</head>
<body οnlοad="selectIndex();">
<!--[if lte IE 9]>
<p class="browsehappy">你正在使用<strong>過時</strong>的瀏覽器,Amaze UI 暫不支持。 請 <a href="http://browsehappy.com/" target="_blank">升級瀏覽器</a>
  以獲得更好的體驗!</p>
<![endif]-->

<%@ include file="/WEB-INF/include/header.jsp"%>

<div class="am-cf admin-main">
<%@ include file="/WEB-INF/include/menu.jsp"%>

<!-- content start -->
<div class="admin-content">

<div class="am-cf am-padding">
<div class="am-fl am-cf">
<strong class="am-text-primary am-text-lg">用戶管理</strong>
</div>
</div>
<div class="am-g">
<div class="am-u-md-6 am-cf">
<div class="am-fl am-cf">
<form id="selectorForm" action="service/user/index" method="post">
<div class="am-btn-toolbar am-fl">
<div class="am-form-group am-margin-left am-fl">
<select name="device_type_selector" id="device_type_selector" οnchange="saveSelectIndex();">
<option value="0">--所有設備--</option>
<c:forEach items="${deviceTypes }" var="deviceType">
<c:choose>
<c:when
test="${deviceType.typeIndex eq device_type_selector}">
<option value="${deviceType.typeIndex }" selected>${deviceType.typeName }</option>
</c:when>
<c:otherwise>
<option value="${deviceType.typeIndex }">${deviceType.typeName }</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</div>
<div class="am-form-group am-margin-left am-fl">
<select name="register_type_selector" οnchange="saveSelectIndex();"
id="register_type_selector" >
<option  value="" >--註冊渠道--</option>
<option  value="0">正常註冊</option>
<option  value="1">QQ註冊</option>
<option  value="3">微信註冊</option>
</select>
</div>
<c:choose>
<c:when test="${sessionScope.admin.company_id eq 0 }">
<div class="am-form-group am-margin-left am-fl">
<select name="channel_type_selector" οnchange="saveSelectIndex();"
id="channel_type_selector">
<option value="">--channel--</option>
<option value="9999">9999</option>
<option value="9998">9998</option>
</select>
</div>
</c:when>
</c:choose>
<div class="am-form-group am-margin-left am-fl">
查詢條件: <input id="searchKey" />
</div>
<input type="hidden" id="page" name="page">
<div class="am-form-group am-margin-left am-fl">
<input id="searchButten" type="button" value="查詢" />
</div>
</div>
</form>
</div>
</div>
</div>
<div class="am-g">
<div class="am-u-sm-12">
<table
class="am-table am-table-bd am-table-striped admin-content-table">
<thead>
<tr>
<th>ID</th>
<th>用戶名</th>
<th>註冊郵箱</th>
<th>性別</th>
<th>最近登錄時間</th>
<th>最近登錄IP</th>
<th>管理</th>
</tr>
</thead>
<tbody id="ttbody">
<c:forEach items="${allusers}" var="alluser" begin="0"
varStatus="status">
<tr>
<td>${alluser.user_id}</td>
<td><a href="service/user/show?user_id=${alluser.user_id}">${alluser.nickname}</a></td>
<td>${alluser.user_mail}</td>
<td>${alluser.user_sex==0?"女":"男"}</td>
<td>${alluser.latest_login_time}</td>
<td>${alluser.latest_login_ip}</td>
<td>
<div class="am-dropdown" data-am-dropdown>
<button
class="am-btn am-btn-default am-btn-xs am-dropdown-toggle"
data-am-dropdown-toggle>
<span class="am-icon-cog"></span> <span
class="am-icon-caret-down"></span>
</button>
<ul class="am-dropdown-content">
<li><a href="#">1. 編輯</a></li>
<li><a href="#">2. 下載</a></li>
<li><a href="#">3. 刪除</a></li>
</ul>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<div id="pagination" style="margin-left: 22px;"></div>
</div>

<!-- content end -->
</div>

<%@ include file="/WEB-INF/include/footer.jsp"%>

<%@ include file="/WEB-INF/include/headref.jsp"%>

<script src="assets/js/bootstrap-paginator.js"></script>

<script type="text/javascript">
$(function() {

var options = {
currentPage : '${curPage}',
totalPages : '${pages }',
itemContainerClass : function(type, page, current) {
return (page === current) ? "active" : "pointer-cursor";
},
itemTexts : function(type, page, current) {
switch (type) {
case "first":
return "首頁";
case "prev":
return "前一頁";
case "next":
return "下一頁";
case "last":
return "末頁";
case "page":
return page;
}
},
onPageClicked : function(e, originalEvent, type, page) {

e.stopImmediatePropagation();
var currentTarget = $(e.currentTarget);
setTableData(page);
 
currentTarget.bootstrapPaginator("show", page);

}
};

$('#pagination').bootstrapPaginator(options);

function setTableData(page) {
options.currentPage = page;
$("#page").val(page);
$("#selectorForm").submit();
}

$("#searchButten")
.click(
function() {
device_type = document
.getElementById("device_type_selector").value;
register_type = document
.getElementById("register_type_selector").value;
channel_type = document
.getElementById("channel_type_selector").value;
searchKey_1 = document
.getElementById("searchKey").value;
$.ajax({
type : 'post',
dataType : 'html',
data : {
device_type_selector : device_type,
register_type_selector : register_type,
channel_type_selector : channel_type,
searchKey : searchKey_1
},
url : 'service/user/search',
success : function(returnText) {
/* window.parent.CloseMsgBox();
if (returnText.substring(0,1) == "N"){
window.parent.ShowMsgBox(returnText.substring(1));
}
else{
window.parent.ShowMsgBox(returnText);
window.location.href = "service/user/index";
    SwitchObjectShow(objName,1);
} */
},
error : function() {/* window.parent.CloseMsgBox();window.parent.ShowMsgBox(ajaxErrorMsg); */
}
});
});
});

</script>
<script type="text/javascript">
    function saveSelectIndex(){   
        var typeId=document.getElementById("register_type_selector");   
        var typeIdText=typeId.options[typeId.selectedIndex].value;   
        var osId=document.getElementById("device_type_selector");   
        var osIdText=osId.options[osId.selectedIndex].value;   //設置多個cookie   
        var channelId=document.getElementById("channel_type_selector");   
        var channelIdText=channelId.options[channelId.selectedIndex].value;   //設置多個cookie  
        document.cookie="typeIdText="+typeIdText;   
        document.cookie="osIdText="+osIdText;
        document.cookie="channelIdText="+channelIdText;
}
function selectIndex(){   //記得初始化,否則會出現undefined   
        var typeIdText=0;   
        var osIdText=0;   //獲取多個cookie  
        var channelIdText=0;
        var coosStr=document.cookie;//注意此處分隔符是分號加空格   
        var coos=coosStr.split("; ");   
        for(var i=0;i<coos.length;i++){      
                var coo=coos[i].split("=");      
                //alert(coo[0]+":"+coo[1]);      
                if("typeIdText"==coo[0]){ 
                        typeIdText=coo[1];      
                }
                if("osIdText"==coo[0]){ 
                        osIdText=coo[1];      
                }   
                if("channelIdText"==coo[0])
                {
                channelIdText = coo[1];
                }
        }     
        var typeId=document.getElementById("register_type_selector");   
        if(typeIdText==0){      
                typeId.selectedIndex=0;   
        }else{      
                var length=typeId.options.length;      
                for(var i=0;i<length;i++){ 
                        if(typeId.options[i].value==typeIdText){    
                                typeId.selectedIndex=i; 
                                break; 
                        }      
                }   
        }      
        var osId=document.getElementById("device_type_selector");   
        if(osIdText==0){      
                osId.selectedIndex=0;   
        }else{      
                var length=osId.options.length;      
                for(var i=0;i<length;i++){ 
                        if(osId.options[i].value==osIdText){    
                                osId.selectedIndex=i;    
                                break; 
                        }      
                }   
        }  
        
        var channelId=document.getElementById("channel_type_selector");   
        if(channelIdText==0){      
        channelId.selectedIndex=0;   
        }else{      
                var length=channelId.options.length;      
                for(var i=0;i<length;i++){ 
                        if(channelId.options[i].value==channelIdText){    
                        channelId.selectedIndex=i;    
                                break; 
                        }      
                }   
        }  
}
</script>
</body>
</html>
此中有兩個點,一個是基於Bootstrap的分頁插件.第二個是使用cookie以及JS來實現,當selector選擇後,刷新頁面selector自動選擇上次選項。 

網上找的Demo 寫的有點錯誤。 慢慢調試出來  修改後的,親測可用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章