Example analysis of js table sorting (support int float date string four data types)
- 2020-06-03 05:52:59
- OfStack
This article gives an example of how to sort js tables. Share to everybody for everybody reference. The details are as follows:
<html>
<head>
<title>SortTable2</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var k=0;
/**//**************************************************************************
The main method for sorting, there are 3 A parameter, STableTd,iCol,sDataType Are the tables to be sorted ID .
The table column number to sort, the data type of the column (supported) int,float,date,string4 Various data types )
**************************************************************************/
function sortTable(sTableId,iCol,sDataType)
{
var oTable=document.getElementById(sTableId);// Get tabular ID
var oTbody=oTable.tBodies[0]; // Get tabular tbody
var colDataRows=oTbody.rows; // To obtain tbody References to all lines in
var aTRs=new Array(); // define aTRs Arrays are used to store tbody In the line
for(var i=0;i<colDataRows.length;i++) // Put all the rows in order aTRs An array of
{
aTRs.push(colDataRows[i]);
}
/**//***********************************************************************
sortCol Attributes are extra table Added property to distinguish between two kinds of sequential sorting
First sort and then ordered reverse
************************************************************************/
if(oTable.sortCol==iCol) // Nonfirst sort
{
aTRs.reverse();
}
else // For the first time ordering
{
if(k%2==0) // ascending
{
aTRs.sort(generateCompareTRs(iCol,sDataType));
}
else if(k%2==1) // Descending order
{
aTRs.sort(generateCompareTRs1(iCol,sDataType));
}
}
var oFragment=document.createDocumentFragment(); // Create document shards
for(var i=0;i<aTRs.length;i++) // I'm going to sort it out aTRs Array members are, in turn, added to the document fragment
{
oFragment.appendChild(aTRs[i]);
}
oTbody.appendChild(oFragment); // Add document fragments to tbody, Display update after sorting is completed
oTable.sortCol=iCol; // Assigns the current column number to sortCol, To distinguish between first sort and non-first sort ,//sortCol The default value is -1
};
// Comparison function, used for sorting between two items
// ascending
function generateCompareTRs(iCol,sDataType)
{
return function compareTRs(oTR1,oTR2)
{
var vValue1=convert(oTR1.cells[iCol].firstChild.nodeValue,sDataType);
var vValue2=convert(oTR2.cells[iCol].firstChild.nodeValue,sDataType);
if(vValue1<vValue2)
{
return -1;
}
else if(vValue1>vValue2)
{
return 1;
}
else
{
return 0;
}
};
};
// Descending order
function generateCompareTRs1(iCol,sDataType)
{
return function compareTRs(oTR1,oTR2)
{
var vValue1=convert(oTR1.cells[iCol].firstChild.nodeValue,sDataType);
var vValue2=convert(oTR2.cells[iCol].firstChild.nodeValue,sDataType);
if(vValue1>vValue2)
{
return -1;
}
else if(vValue1<vValue2)
{
return 1;
}
else
{
return 0;
}
};
};
// Data type conversion function
function convert(sValue,sDataType)
{
switch(sDataType)
{
case "int":return parseInt(sValue);
case "float": return parseFloat(sValue);
case "date":return new Date(Date.parse(sValue));
default:return sValue.toString();
}
};
</script>
</head>
<body>
<form name="f1" id="f1" action="" method="post">
<table border="1" id="tblSort" sortCol="-1">
<thead>
<tr>
<th onClick="sortTable('tblSort',0);" style="cursor:pointer">Last Name</th>
<th onClick="sortTable('tblSort',1)" style="cursor:pointer">First Name</th>
<th onClick="sortTable('tblSort',2,'date')" style="cursor:pointer">Birthday</th>
<th onClick="sortTable('tblSort',3,'int')" style="cursor:pointer">Silbings</th>
</tr>
</thead>
<tbody>
<tr>
<td>Simth</td>
<td>John</td>
<td>7/12/1978</td>
<td>50nGy/h</td>
</tr>
<tr>
<td>Johnson</td>
<td>Betty</td>
<td>5/12/1965</td>
<td>20nGy/h</td>
</tr>
<tr>
<td>Henderson</td>
<td>Nathan</td>
<td>10/15/1977</td>
<td>130nGy/h</td>
</tr>
<tr>
<td>Willianms</td>
<td>James</td>
<td>2/25/1949</td>
<td>10nGy/h</td>
</tr>
<tr>
<td>Gilliam</td>
<td>Michael</td>
<td>7/8/1980</td>
<td>140nGy/h</td>
</tr>
<tr>
<td>Walker</td>
<td>Matthew</td>
<td>6/18/1981</td>
<td>103nGy/h</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Hopefully, this article has been helpful in your javascript programming.