HTML中单击Table表头实现排序
来源:互联网 发布:手机魔术拍照软件 编辑:程序博客网 时间:2024/05/04 20:56
本代码基于jquery,如下:
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"type="text/javascript"></script>
/* * The object TableSort can be used as a tool class */var TableSort = { init: function (TableId) { this.setDataArray(TableId); this.table = $("#" + TableId); $("#" + TableId + " tr:first td").bind("click",this.sort); }, table:null, dataArray: [], htmlArray: {}, setDataArray: function (TableId) { $("#" + TableId + " tr").each(function (i) { var tr = $(this); if (i != 0) { var temp = []; temp.push(tr.attr("rowid")); tr.find("td").each(function () { temp.push($(this).text()); }); TableSort.dataArray.push(temp); TableSort.htmlArray[tr.attr("rowid")] = tr.clone(); } }); }, sort: function () { var td = $(this); var index = td.index(); var sortIndex = index + 1; var currentSort = td.attr("sortType") || "ASC"; if (currentSort == "ASC") { TableSort.dataArray = TableSort.multiArraySort(TableSort.dataArray, "DESC", sortIndex); td.attr("sortType", "DESC"); } else { TableSort.dataArray = TableSort.multiArraySort(TableSort.dataArray, "ASC", sortIndex); td.attr("sortType", "ASC"); } td.siblings().attr("sortType", ""); //refresh the table TableSort.table.find("tr:gt(0)").remove(); for (var i = 0, len = TableSort.dataArray.length; i < len; i++) { var rowid = TableSort.dataArray[i][0]; TableSort.table.children().append(TableSort.htmlArray[rowid]); } }, multiArraySort: function (array, type, index) { var resultArray = array; if(type == "ASC"){ resultArray.sort(function (a, b) { //String is the default type of content,the number should return b[sortIndex]-a[sortIndex]. return a[index].localeCompare(b[index]); }); return resultArray; } else if (type == "DESC") { resultArray.sort(function (a, b) { return b[index].localeCompare(a[index]); }); return resultArray; } else { console.log("The argument of 'multiArraySort' value does not match the type that this argument expects. " + "Check that the argument value is appropriate to the type expected by this argument, and then try again."); return null; } }};
使用时直接如下调用:
TableSort.init("tableId");
0 0
- HTML中单击Table表头实现排序
- javascript 实现单击表头排序
- html table表头排序功能
- 前端笔试题笔记:在HTML中实现table表头点击升序/降序排序
- html Table实现表头固定
- html Table实现表头固定
- html中table固定表头
- ListView单击表头排序
- MFC中CListCtrl单击表头排序+表格大小随窗体大小改变等功能实现
- HTML Table锁定表头(CSS实现)
- html固定table表头的实现思路
- html 中固定表头的 table
- web中table表格点击表头排序
- HTML table表头排序箭头绘制法【不用箭头图片】
- table点击表头排序
- html table css 实现固定表头的办法
- HTML/CSS实现<table>表头固定、表体滚动
- HTML中点击TABLE列头实现排序功能
- 【存储管理】系统调用mmap()
- iPhone-获取网络数据或者路径的文件名以及后缀
- Spring MVC-自定义数据类型转换解决方案
- SVN的文件夹链接太强大了!(目录链接,目录映射,svn:externals)
- 面试题:求全排列和组合
- HTML中单击Table表头实现排序
- 在myeclipse中加上多行注释
- HDOJ-3065 病毒侵袭持续中
- 2014年5月7号
- rapidjson操作指南
- ORA-01536: 超出表空间 'SYSTEM' 的空间限额
- Java中用HttpsURLConnection访问https链接的问题
- GridView ListView 嵌套 ScrollView 解决方案
- Ubuntu12.04开机自动挂载windows分区