欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TableSort.js表格排序插件使用方法詳解

 更新時間:2017年02月10日 13:55:59   作者:孫瑞  
這篇文章主要為大家詳細(xì)介紹了TableSort.js表格排序插件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了TableSort.js表格排序的具體代碼,供大家參考,具體內(nèi)容如下

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>TableSort</title> 
<style type="text/css"> 
table { 
  border-collapse: collapse; 
  width: 300px; 
} 
table caption { 
  border-right: 1px solid #abc; 
  border-left: 1px solid #abc; 
  border-top: 2px solid #000; 
  border-bottom: 2px solid #000; 
  background-color: #afd; 
} 
#sales tr, #sales td { 
  border: 1px solid #abc; 
  text-align: center; 
} 
</style> 
</head> 
<body> 
<table id="sales" summary="summary here"> 
 <caption> 
 Main Title 
 </caption> 
 <col/> 
 <col/> 
 <col/> 
 <thead> 
  <tr> 
   <th class="asc">Col1</th> 
   <th>Col2</th> 
   <th>Col3</th> 
  </tr> 
 </thead> 
 <tbody> 
  <tr> 
   <td>A1</td> 
   <td>S2</td> 
   <td>W3</td> 
  </tr> 
  <tr> 
   <td>B1</td> 
   <td>C2</td> 
   <td>V3</td> 
  </tr> 
  <tr> 
   <td>C1</td> 
   <td>X2</td> 
   <td>K3</td> 
  </tr> 
 </tbody> 
 <!-- tfoot><tr><td cols=3 >other description</td></tr></tfoot --> 
</table> 
<button onclick="fn()">Test</button> 
<script language="javascript"> 
function TableSort(id) { 
  this.tbl = document.getElementById(id); 
  this.lastSortedTh = null; 
  if (this.tbl && this.tbl.nodeName == "TABLE") { 
    var headings = this.tbl.tHead.rows[0].cells; 
    for (var i = 0; headings[i]; i++) { 
      if (headings[i].className.match(/asc|dsc/)) { 
        this.lastSortedTh = headings[i]; 
      } 
    } 
    this.makeSortable(); 
  } 
} 
TableSort.prototype.makeSortable = function() { 
  var headings = this.tbl.tHead.rows[0].cells; 
  for (var i = 0; headings[i]; i++) { 
    headings[i].cIdx = i; 
    var a = document.createElement("a"); 
    a.href = "#"; 
    a.innerHTML = headings[i].innerHTML; 
    a.onclick = function(that) { 
      return function() { 
        that.sortCol(this); 
        return false; 
      } 
    }(this); 
    headings[i].innerHTML = ""; 
    headings[i].appendChild(a); 
  } 
} 
TableSort.prototype.sortCol = function(el) { 
  var rows = this.tbl.rows; 
  var alpha = [], numeric = []; 
  var aIdx = 0, nIdx = 0; 
  var th = el.parentNode; 
  var cellIndex = th.cIdx; 
 
  for (var i = 1; rows[i]; i++) { 
    var cell = rows[i].cells[cellIndex]; 
    var content = cell.textContent ? cell.textContent : cell.innerText; 
    var num = content.replace(/(\$|\,|\s)/g, ""); 
    if (parseFloat(num) == num) { 
      numeric[nIdx++] = { 
        value : Number(num), 
        row : rows[i] 
      } 
    } else { 
      alpha[aIdx++] = { 
        value : content, 
        row : rows[i] 
      } 
    } 
  } 
  function bubbleSort(arr, dir) { 
    var start, end; 
    if (dir === 1) { 
      start = 0; 
      end = arr.length; 
    } else if (dir === -1) { 
      start = arr.length - 1; 
      end = -1; 
    } 
    var unsorted = true; 
    while (unsorted) { 
      unsorted = false; 
      for (var i = start; i != end; i = i + dir) { 
        if (arr[i + dir] && arr[i].value > arr[i + dir].value) { 
          var temp = arr[i]; 
          arr[i] = arr[i + dir]; 
          arr[i + dir] = temp; 
          unsorted = true; 
        } 
      } 
    } 
    return arr; 
  } 
 
  var col = [], top, bottom; 
  if (th.className.match("asc")) { 
    top = bubbleSort(alpha, -1); 
    bottom = bubbleSort(numeric, -1); 
    th.className = th.className.replace(/asc/, "dsc"); 
  } else { 
    top = bubbleSort(numeric, 1); 
    bottom = bubbleSort(alpha, 1); 
    if (th.className.match("dsc")) { 
      th.className = th.className.replace(/dsc/, "asc"); 
    } else { 
      th.className += "asc"; 
    } 
  } 
  if (this.lastSortedTh && th != this.lastSortedTh) { 
    this.lastSortedTh.className = this.lastSortedTh.className.replace( 
        /dsc|asc/g, ""); 
  } 
  this.lastSortedTh = th; 
  col = top.concat(bottom); 
  var tBody = this.tbl.tBodies[0]; 
  for (var i = 0; col[i]; i++) { 
    tBody.appendChild(col[i].row); 
  } 
} 
function fn() { 
 
  var sales = document.getElementById('sales'); 
  var sortTable = new TableSort('sales'); 
} 
</script> 
</body> 
</html> 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 微信小程序自定義prompt組件步驟詳解

    微信小程序自定義prompt組件步驟詳解

    本文分步驟給大家介紹了微信小程序自定義prompt組件,文中通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-06-06
  • 為超鏈接加上disabled后的故事

    為超鏈接加上disabled后的故事

    為超鏈接加上disabled后的故事,學(xué)習(xí)js的朋友可以參考下對超鏈接的一些控制。
    2010-12-12
  • 如何利用js給自己照相并修圖

    如何利用js給自己照相并修圖

    在一些瀏覽器里已經(jīng)可以使用web api調(diào)用攝像頭功能了,下面這篇文章主要給大家介紹了關(guān)于如何利用js給自己照相并修圖的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Bootstrap模態(tài)框插件使用詳解

    Bootstrap模態(tài)框插件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Bootstrap模態(tài)框插件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • js實(shí)現(xiàn)防止被iframe的方法

    js實(shí)現(xiàn)防止被iframe的方法

    這篇文章主要介紹了js實(shí)現(xiàn)防止被iframe的方法,實(shí)例分析了兩種比較常用的javascript防止頁面被iframe的技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • 微信小程序自定義yPicker組件實(shí)現(xiàn)省市區(qū)三級聯(lián)動功能

    微信小程序自定義yPicker組件實(shí)現(xiàn)省市區(qū)三級聯(lián)動功能

    這篇文章主要介紹了微信小程序自定義yPicker組件分析及省市區(qū)三級聯(lián)動實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • js打造數(shù)組轉(zhuǎn)json函數(shù)

    js打造數(shù)組轉(zhuǎn)json函數(shù)

    這里給大家分享的是一段使用js實(shí)現(xiàn)數(shù)組轉(zhuǎn)換成json的函數(shù)代碼,代碼簡潔易懂,并附上了使用方法,小伙伴們拿去試試。
    2015-01-01
  • Javascript根據(jù)指定下標(biāo)或?qū)ο髣h除數(shù)組元素

    Javascript根據(jù)指定下標(biāo)或?qū)ο髣h除數(shù)組元素

    刪除數(shù)組元素在工作中經(jīng)常會用到,本文講解一下Javascript根據(jù)下標(biāo)刪除數(shù)組元素的方法,需要了解的朋友可以參考下
    2012-12-12
  • JS中from 表單序列化提交的代碼

    JS中from 表單序列化提交的代碼

    這篇文章主要介紹了javascript中from 表單序列化提交的實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯,需要的朋友參考下吧
    2017-01-01
  • JavaScript 如何實(shí)現(xiàn)同源通信

    JavaScript 如何實(shí)現(xiàn)同源通信

    在日常工作中,你可能會遇到同源頁面間通信的場景。針對這種場景,我們可以使用 localStorage 和 storage 事件來解決同源頁面間通信的問題。除此之外,我們還可以使用 Broadcast Channel API 來解決該問題。接下來,將帶大家一起來認(rèn)識一下 Broadcast Channel API。
    2021-05-05

最新評論