Home > Web Front-end > JS Tutorial > Detailed explanation of js table sorting examples (supports four data types: int, float, date, string)

Detailed explanation of js table sorting examples (supports four data types: int, float, date, string)

怪我咯
Release: 2017-07-04 15:17:56
Original
1536 people have browsed it

This article mainly introduces the analysis of js table sorting examples (supports four data types of int, float, date, string), involving javascriptcommonly used ascending order, descending order and data Type conversion and other related skills, friends in need can refer to the following

The example of this article describes the method of sorting js tables. Share it with everyone for your 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;
/**//**************************************************************************
排序的主方法,有三个形参,STableTd,iCol,sDataType分别为需要排序的表格ID,
需要排序的表格列号,所在列的数据类型(支持int,float,date,string四种数据类型)
**************************************************************************/
function sortTable(sTableId,iCol,sDataType)
{
  var oTable=document.getElementById(sTableId);//获取表格的ID 
  var oTbody=oTable.tBodies[0]; //获取表格的tbody
  var colDataRows=oTbody.rows; //获取tbody里的所有行的引用

  var aTRs=new Array(); //定义aTRs数组用于存放tbody里的行
  for(var i=0;i<colDataRows.length;i++) //依次把所有行放如aTRs数组
  {
    aTRs.push(colDataRows[i]);
  }
  /**//***********************************************************************
  sortCol属性是额外给table添加的属性,用于作顺反两种顺序排序时的判断,区分
  首次排序和后面的有序反转
  ************************************************************************/
  if(oTable.sortCol==iCol) //非首次排序
  {
    aTRs.reverse();
  }
  else  //首次排序
  {
    if(k%2==0) //升序
    {
      aTRs.sort(generateCompareTRs(iCol,sDataType));
    }
    else if(k%2==1) //降序
    {
      aTRs.sort(generateCompareTRs1(iCol,sDataType));
    }
  }
  var oFragment=document.createDocumentFragment();  //创建文档碎片
  for(var i=0;i<aTRs.length;i++)  //把排序过的aTRs数组成员依次添加到文档碎片
  {
    oFragment.appendChild(aTRs[i]);
  }
  oTbody.appendChild(oFragment); //把文档碎片添加到tbody,完成排序后的显示更新 
  oTable.sortCol=iCol;  //把当前列号赋值给sortCol,以此来区分首次排序和非首次排序,//sortCol的默认值为-1
};

//比较函数,用于两项之间的排序
//升序
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;
    }
  };
};
//降序
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;
    }
  };
};
//数据类型转换函数
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(&#39;tblSort&#39;,0);" style="cursor:pointer">Last Name</th>
  <th onClick="sortTable(&#39;tblSort&#39;,1)" style="cursor:pointer">First Name</th>
  <th onClick="sortTable(&#39;tblSort&#39;,2,&#39;date&#39;)" style="cursor:pointer">Birthday</th>
  <th onClick="sortTable(&#39;tblSort&#39;,3,&#39;int&#39;)" 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>
Copy after login

The above is the detailed content of Detailed explanation of js table sorting examples (supports four data types: int, float, date, string). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template