今天做了一个jsp表头排序功能,用到了js函数,其中主要一个函数为 sort(tableId,sortColumn,nodeType),在jsp中通过 οnclick="sort(table1,2,'float')" 其中table1为table的id,但是在调用js的时候发现使用方法getdocument.getElementByI(tableId)会产生js错误:缺少对象。由于jsp中含有多个需要排序的table,因此tableId不能写死,于是在页面进行校验,使用alert(tableId)检验发现,tableId为object对象。但是使用alert(tableId.value)校验的时候又出现undefined问题,而使用alert(sortColumn)时能正确弹出 2,使用alert(nodeType)时正确弹出 float,于是知道在js传值时除了数字外不用加单引号,字符串如果不加单引号的话js会自动默认为object ,而不是字符串。最终 改成 οnclick="sort('table1',2,'float')" 问题迎刃而解。
附上点击表头进行table列排序代码仅供参考:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'tabel.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table id="table1" align="center" border="1">
<thead>
<tr>
<td>
标题1
</td>
<td οnclick="sort('table1',1,'int')">
标题2
</td>
<td οnclick="sort('table1',2,'int')">
标题3
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td>
6
</td>
<td>
7
</td>
</tr>
<tr>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
</tr>
<tr>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
</tr>
</tbody>
</table>
       
<table id="table2" align="center" border="1">
<thead>
<tr>
<td οnclick="sort('table2',0,'int')">
标题4
</td>
<td οnclick="sort('table2',1,'int')">
标题5
</td>
<td οnclick="sort('table2',2,'int')">
标题6
</td>
<td οnclick="sort('table2',3,'int')">
标题7
</td>
</tr>
</thead>
<tr>
<td>
4
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
</tr>
<tr>
<td>
5
</td>
<td>
6
</td>
<td>
3
</td>
<td>
4
</td>
</tr>
<tr>
<td>
7
</td>
<td>
4
</td>
<td>
6
</td>
<td>
5
</td>
</tr>
</table>
</body>
<script type="text/javascript">
function sort(tableId, sortColumn,nodeType) {
var table = document.getElementById(tableId);
var tableBody = table.tBodies[0];
var tableRows = tableBody.rows;
var rowArray = new Array();
for (var i = 0; i < tableRows.length; i++) {
rowArray[i] = tableRows[i];
}
if (table.sortColumn == sortColumn) {
rowArray.reverse();
} else {
rowArray.sort(generateCompareTR(sortColumn, nodeType));
}
var tbodyFragment = document.createDocumentFragment();
for (var i = 0; i < rowArray.length; i++) {
tbodyFragment.appendChild(rowArray[i]);
}
tableBody.appendChild(tbodyFragment);
table.sortColumn = sortColumn;
}
function generateCompareTR(sortColumn, nodeType) {
return function compareTR(trLeft, trRight) {
var leftValue = convert(trLeft.cells[sortColumn].firstChild.nodeValue, nodeType);
var rightValue = convert(trRight.cells[sortColumn].firstChild.nodeValue, nodeType);
if (leftValue < rightValue) {
return -1;
} else {
if (leftValue > rightValue) {
return 1;
} else {
return 0;
}
}
};
}
function convert(value, dataType) {
switch (dataType) {
case "int":
return parseInt(value);
case "float":
return parseFloat(value);
case "date":
return new Date(Date.parse(value));
default:
return value.toString();
}
}
</script>
</html>