This time I will bring you jquery to sort according to Chinese tables. What are the precautions for jquery to sort according to Chinese tables? The following is a practical case, let's take a look.
//转换器,将列的字段类型转换为可以排序的类型:String,int,float 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(); } } // 汉字排序方法 function chrComp(a,b) { return a.localeCompare(b); } //排序函数产生器 function generateCompareTRs(iCol, sDataType,isinput,sDec) { return function compareTRs(oTR1, oTR2) { if(isinput == 1) { var vValue1 = convert(oTR1.getElementsByTagName("input")[iCol].value); var vValue2 = convert(oTR2.getElementsByTagName("input")[iCol].value); } else { var vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType); var vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType); } if(sDec=='desc') { if(sDataType=='int') { return vValue1 == vValue2 ? 0 :(vValue1 - vValue2 <0 ? 1 : -1); } else if(sDataType =='cn') { if(chrComp(vValue1,vValue2)>0) { return -1; } else if(chrComp(vValue1,vValue2)<0) { return 1; } else { return 0; } } else { if (vValue1 > vValue2) { return -1; } else if (vValue1 < vValue2) { return 1; } else { return 0; } } } else if(sDec=='asc') { if(sDataType=='int') { return vValue1 == vValue2 ? 0 :(vValue1 - vValue2 >0 ? 1 : -1); } else if(sDataType =='cn') { return chrComp(vValue1,vValue2); } else { if (vValue1 > vValue2) { return 1; } else if (vValue1 < vValue2) { return -1; } else { return 0; } } } }; } //重置单元格的classname function ChangeClsName(tr,num) { num = num%2?1:2; num.toString(); for ( var i = 0 ; i < tr.childNodes.length; i ++ ) { tr.childNodes[i].className = "row" + num } } /*排序方法(主函数)sTableID table ID
iCol represents the column index
1, when it is not an input type, iCol represents the td of tr;
2, when it is an input type , then iCol represents the input number in this tr;
sDataType represents the
data type of the cell or the data type of the value of the input. The default is string, but it can also be int, float. cn Is Chinese isinput indicates whether the sorted content is input (1 yes, 0 no)
sDec indicates reverse order or order (desc, default order), to avoid direct reverse order when sorting after the input value changes.
*/ function sortTable(sTableID, iCol, sDataType, isinput, sDec) { var oTable = document .getElementById(sTableID); var oTBody = oTable.tBodies[0]; var colDataRows = oTBody.rows; var aTRs = new Array; //将所有列放入数组 for (var i=0; i < colDataRows.length; i++) { aTRs[i] = colDataRows[i]; } aTRs.sort(generateCompareTRs(iCol, sDataType,isinput, sDec)); var oFragment = document.createDocumentFragment(); for (var i=0; i < aTRs.length; i++) { oFragment.appendChild(aTRs[i]); ChangeClsName(aTRs[i],i); } oTBody.appendChild(oFragment); }This week I finally have time to study jquery, which I have always wanted to learn. It's a pity that the company can't use it. In fact, after learning it, I still feel that it can be used. After knowing jqery, I looked at other people's plug-ins. jquery.tablesorter.js This plug-in is quite powerful. I tried it and found that it does not support Chinese. Look at the source code and it says
/* sorting methods */ function multisort(table,sortList,cache) { if(table.config.debug) { var sortTime = new Date(); } var dynamicExp = "var sortWrapper = function(a,b) {", l = sortList.length; for(var i=0; i < l; i++) { var c = sortList[i][0]; var order = sortList[i][1]; var s = (getCachedSortType(table.config.parsers,c) == "text") ? ((order == 0) ? "sortText" : "sortTextDesc") : ((order == 0) ? "sortNumeric" : "sortNumericDesc"); var e = "e" + i; dynamicExp += "var " + e + " = " + s + "(a[" + c + "],b[" + c + "]); "; dynamicExp += "if(" + e + ") { return " + e + "; } "; dynamicExp += "else { "; } for(var i=0; i < l; i++) { dynamicExp += "}; "; } dynamicExp += "return 0; "; dynamicExp += "}; "; eval(dynamicExp); cache.normalized.sort(sortWrapper); if(table.config.debug) { benchmark("Sorting on " + sortList.toString() + " and dir " + order+ " time:", sortTime); } return cache; }; function sortText(a,b) { return ((a < b) ? -1 : ((a > b) ? 1 : 0)); }; function sortTextDesc(a,b) { return ((b < a) ? -1 : ((b > a) ? 1 : 0)); }; function sortNumeric(a,b) { return a-b; }; function sortNumericDesc(a,b) { return b-a; }; function getCachedSortType(parsers,i) { return parsers[i].type; };At first I thought I would encounter some difficulties. Unexpectedly, I only need to change his sorting function and it will be ok.
function sortText(a,b) { return a.localeCompare(b); }; function sortTextDesc(a,b) { return -a.localeCompare(b); };Test it. Chinese, Chinese and English together, if there are columns in the middle, it will be no problem.
Originally, this plug-in has jquery.tablesorter.pack.js which is only 9kb after compression, but I will only change jquery.tablesorter.js (23kb). I don't know how to turn it into jquery.tablesorter.pack.js.
ps: Of course, this plug-in also has many extended functions, such as multi-column sorting, various examples, etc. There are documents for reference.
jquery implements table paging and sorting
jQuery cross-domain iframe interface call (with code)
The above is the detailed content of jquery implements sorting according to Chinese tables. For more information, please follow other related articles on the PHP Chinese website!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
