zenid123
asked on
sorting html table within a form
i have a html table. When the user clicks heading of a column the rows get sorted. The problem is when i use this in a form. First, all the form elemets disappear after sorting, second, it gives incorrect results.
Here is my code
<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.util.*" %>
<meta http-equiv="Page-Enter" content="revealtrans(durat ion=1,tran sition=8)" >
<html>
<head>
<META HTTP-EQUIV="Content-Style- Type" CONTENT="text-css" >
<LINK REL="STYLESHEET" HREF="cssApproval.css" >
<style type="text/css">
th
{
color: #000080;
}
</style>
<script type="text/javascript">
function validate(objForm)
{
var bChecked=false;
for (var i=0; i<objForm.elements.length; i++)
{ // check all the form elements
if(objForm.elements[i].typ e == 'radio' && objForm.elements[i].checke d)
{ // is it a radio button and is it selected
bChecked=true; // if radio button has been selected then set variable to true
}
}
if(!bChecked)
{
alert("Please select a Radio Button"); // no radio button selected, output appropriate error message
}
return bChecked; // return the value defined above
}
currentCol = 0
previousCol = -1
function CompareAlpha(a, b) {
if (a[currentCol] < b[currentCol]) { return -1; }
if (a[currentCol] > b[currentCol]) { return 1; }
return 0;
}
function CompareAlphaIgnore(a, b) {
strA = a[currentCol].toLowerCase( );
strB = b[currentCol].toLowerCase( );
if (strA < strB) { return -1; }
else {
if (strA > strB) { return 1; }
else { return 0; }
}
}
function CompareDate(a, b) {
// this one works with date formats conforming to Javascript specifications, e.g. m/d/yyyy
datA = new Date(a[currentCol]);
datB = new Date(b[currentCol]);
if (datA < datB) { return -1; }
else {
if (datA > datB) { return 1; }
else { return 0; }
}
}
function CompareDateEuro(a, b) {
// this one works with european date formats, e.g. d.m.yyyy
strA = a[currentCol].split("-");
strB = b[currentCol].split("-")
datA = new Date(strA[0], strA[1], strA[2]);
datB = new Date(strB[0], strB[1], strB[2]);
if (datA < datB) { return -1; }
else {
if (datA > datB) { return 1; }
else { return 0; }
}
}
function CompareTime(a, b) {
strA = a[currentCol].split(":");
strB = b[currentCol].split(":")
datA = new Date(strA[0], strA[1], strA[2]);
datB = new Date(strB[0], strB[1], strB[2]);
if (datA < datB) { return -1; }
else {
if (datA > datB) { return 1; }
else { return 0; }
}
}
function CompareNumeric(a, b) {
//window.alert ("CompareNumeric");
numA = a[currentCol]
numB = b[currentCol]
if (isNaN(numA)) { return 0;}
else {
if (isNaN(numB)) { return 0; }
else { return numA - numB; }
}
}
function TableSort(myTable, myCol, myType)
{
var tblEl = document.getElementById(my Table);
var oldDsply = tblEl.style.display;
// tblEl.style.display = "none";
// Create a two-dimensional array and fill it with the table's content
var mySource = document.all(myTable);
var myRows = mySource.rows.length;
var myCols = mySource.rows(0).cells.len gth;
currentCol = myCol
myArray = new Array(myRows)
for (i=0; i < myRows; i++) {
myArray[i] = new Array(myCols)
for (j=0; j < myCols; j++) {
myArray[i][j] = document.all(myTable).rows (i).cells( j).innerTe xt
}
}
if (myCol == previousCol) {
myArray.reverse(); // clicked the same column as previously - reverse the sort
}
else { // clicked on a new column - sort as indicated
switch (myType) {
case "a":
myArray.sort(CompareAlpha) ;
break;
case "ai":
myArray.sort(CompareAlphaI gnore);
break;
case "d":
myArray.sort(CompareDate);
break;
case "de":
myArray.sort(CompareDateEu ro);
break;
case "n":
myArray.sort(CompareNumeri c);
break;
default:
myArray.sort()
}
}
// Re-write the table contents
for (i=0; i < myRows; i++) {
for (j=0; j < myCols; j++) {
mySource.rows(i).cells(j). innerText = myArray[i][j]
}
}
tblEl.style.display = oldDsply;
previousCol = myCol; // remember the current sort column for the next pass
return 0;
}
</script>
</head>
<body>
<FORM NAME="form1" action="traceClosedCapital .jsp" onSubmit="return validate(this)">
<TABLE CLASS=tableMain>
<thead>
<TR>
<TH class=tableHead></TH>
<TH onclick="TableSort('invent ory1', 0, 'n')">Indent No. </TH>
<TH onclick="TableSort('invent ory1', 1, 'ai')"> Item</TH>
<TH onclick="TableSort('invent ory1', 2, 'ai')"> Category</TH>
<TH onclick="TableSort('invent ory1', 3, 'ai')"> Specification</TH>
<TH onclick="TableSort('invent ory1', 4, 'ai')" >Qty Ordered</TH>
<TH onclick="TableSort('invent ory1', 5, 'ai')">Qty Received</TH>
<TH onclick="TableSort('invent ory1', 6, 'de')"> Delivery Date</TH>
<TH onclick="TableSort('invent ory1', 7, 'ai')"> Cost</TH>
<TH onclick="TableSort('invent ory1', 8, 'ai')"> Vendor</TH>
<TH onclick="TableSort('invent ory1', 9, 'ai')"> Replacement</TH>
<TH onclick="TableSort('invent ory1', 10, 'ai')"> Details</TH>
<TH onclick="TableSort('invent ory1', 11, 'de')"> Closed On</TH>
<TH onclick="TableSort('invent ory1', 12, 'ai')"> Closed By</TH>
<TH onclick="TableSort('invent ory1', 13, 'ai')"> Dept</TH>
</TR>
</thead>
<tbody id="inventory1">
<% while(some condition)
{
%>
<TR>
<TD><INPUT TYPE="radio" NAME="r" value="<%=indentno%>"></TD >
<TD><%=indentno%></TD>
<TD><%=name%></TD>
<TD><%=category%></TD>
<TD><%=specification%></TD >
<TD><%=noofitems%></TD>
<TD><%=qtyReceived%></TD>
<TD><%=deliveryDate%></TD>
<TD><%=curr%><%=cost%></TD >
<TD><%=vendor%></TD>
<TD><%=replacement%></TD>
<TD><%=details%></TD>
<TD><%=currentDate%>/<%=cu rrentTime% ></TD>
<TD><%=user%></TD>
<TD><%=dept_name%></TD>
</TR>
<%
}
%>
</tbody>
</TABLE>
<table>
</table>
</form>
</body>
</html>
The problem with this code is that all the radio buttons disappear and it returns incorrect result. Same code when used without form tag and elements works fine.
Please Help.
Here is my code
<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.util.*" %>
<meta http-equiv="Page-Enter" content="revealtrans(durat
<html>
<head>
<META HTTP-EQUIV="Content-Style-
<LINK REL="STYLESHEET" HREF="cssApproval.css" >
<style type="text/css">
th
{
color: #000080;
}
</style>
<script type="text/javascript">
function validate(objForm)
{
var bChecked=false;
for (var i=0; i<objForm.elements.length;
{ // check all the form elements
if(objForm.elements[i].typ
{ // is it a radio button and is it selected
bChecked=true; // if radio button has been selected then set variable to true
}
}
if(!bChecked)
{
alert("Please select a Radio Button"); // no radio button selected, output appropriate error message
}
return bChecked; // return the value defined above
}
currentCol = 0
previousCol = -1
function CompareAlpha(a, b) {
if (a[currentCol] < b[currentCol]) { return -1; }
if (a[currentCol] > b[currentCol]) { return 1; }
return 0;
}
function CompareAlphaIgnore(a, b) {
strA = a[currentCol].toLowerCase(
strB = b[currentCol].toLowerCase(
if (strA < strB) { return -1; }
else {
if (strA > strB) { return 1; }
else { return 0; }
}
}
function CompareDate(a, b) {
// this one works with date formats conforming to Javascript specifications, e.g. m/d/yyyy
datA = new Date(a[currentCol]);
datB = new Date(b[currentCol]);
if (datA < datB) { return -1; }
else {
if (datA > datB) { return 1; }
else { return 0; }
}
}
function CompareDateEuro(a, b) {
// this one works with european date formats, e.g. d.m.yyyy
strA = a[currentCol].split("-");
strB = b[currentCol].split("-")
datA = new Date(strA[0], strA[1], strA[2]);
datB = new Date(strB[0], strB[1], strB[2]);
if (datA < datB) { return -1; }
else {
if (datA > datB) { return 1; }
else { return 0; }
}
}
function CompareTime(a, b) {
strA = a[currentCol].split(":");
strB = b[currentCol].split(":")
datA = new Date(strA[0], strA[1], strA[2]);
datB = new Date(strB[0], strB[1], strB[2]);
if (datA < datB) { return -1; }
else {
if (datA > datB) { return 1; }
else { return 0; }
}
}
function CompareNumeric(a, b) {
//window.alert ("CompareNumeric");
numA = a[currentCol]
numB = b[currentCol]
if (isNaN(numA)) { return 0;}
else {
if (isNaN(numB)) { return 0; }
else { return numA - numB; }
}
}
function TableSort(myTable, myCol, myType)
{
var tblEl = document.getElementById(my
var oldDsply = tblEl.style.display;
// tblEl.style.display = "none";
// Create a two-dimensional array and fill it with the table's content
var mySource = document.all(myTable);
var myRows = mySource.rows.length;
var myCols = mySource.rows(0).cells.len
currentCol = myCol
myArray = new Array(myRows)
for (i=0; i < myRows; i++) {
myArray[i] = new Array(myCols)
for (j=0; j < myCols; j++) {
myArray[i][j] = document.all(myTable).rows
}
}
if (myCol == previousCol) {
myArray.reverse(); // clicked the same column as previously - reverse the sort
}
else { // clicked on a new column - sort as indicated
switch (myType) {
case "a":
myArray.sort(CompareAlpha)
break;
case "ai":
myArray.sort(CompareAlphaI
break;
case "d":
myArray.sort(CompareDate);
break;
case "de":
myArray.sort(CompareDateEu
break;
case "n":
myArray.sort(CompareNumeri
break;
default:
myArray.sort()
}
}
// Re-write the table contents
for (i=0; i < myRows; i++) {
for (j=0; j < myCols; j++) {
mySource.rows(i).cells(j).
}
}
tblEl.style.display = oldDsply;
previousCol = myCol; // remember the current sort column for the next pass
return 0;
}
</script>
</head>
<body>
<FORM NAME="form1" action="traceClosedCapital
<TABLE CLASS=tableMain>
<thead>
<TR>
<TH class=tableHead></TH>
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
<TH onclick="TableSort('invent
</TR>
</thead>
<tbody id="inventory1">
<% while(some condition)
{
%>
<TR>
<TD><INPUT TYPE="radio" NAME="r" value="<%=indentno%>"></TD
<TD><%=indentno%></TD>
<TD><%=name%></TD>
<TD><%=category%></TD>
<TD><%=specification%></TD
<TD><%=noofitems%></TD>
<TD><%=qtyReceived%></TD>
<TD><%=deliveryDate%></TD>
<TD><%=curr%><%=cost%></TD
<TD><%=vendor%></TD>
<TD><%=replacement%></TD>
<TD><%=details%></TD>
<TD><%=currentDate%>/<%=cu
<TD><%=user%></TD>
<TD><%=dept_name%></TD>
</TR>
<%
}
%>
</tbody>
</TABLE>
<table>
</table>
</form>
</body>
</html>
The problem with this code is that all the radio buttons disappear and it returns incorrect result. Same code when used without form tag and elements works fine.
Please Help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The problem for radio button is solved. But it's still giving incorrect results. Same code in another page without form tag is working fine. I don't know if its a problem with form tag or something else.
Thanks
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It seems as if the middle row keeps stationery and other rows move.
ASKER
Figured Out the problem . Its because of the radio buttons . I removed the first column which contains the radio button and its working fine. I don't know why is this problem and how to resolve it
ASKER
thanks .
Solved the problem. Started indexing from the frist column and noe its working fine
Solved the problem. Started indexing from the frist column and noe its working fine
ASKER
If I display alert in any function "a" or "b". I get an extra comma before the indentno column. It is because of the radio button I am using.
Plz suggest How to resolve this