Home >Web Front-end >JS Tutorial >js table sorting example analysis (supports four data types: int, float, date, string)_javascript skills

js table sorting example analysis (supports four data types: int, float, date, string)_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:00:421288browse

The example in 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('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>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
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