最近做一个机票的系统,要对航班信息进行排序,所以整理了一下,把排序的方法写下来。

首先table的结构大概如下

最后显示的样式为

首先在每一个tr上加一个自定义属性:data-sort-field-ftime,对应的值就是起飞时间去掉":"后的一个字符串

这样,我们在排序的时候,只要将每个tr的data-sort-field-ftime的值,parseInt后,然后冒泡排序,就可以实现我要的效果。

给排序按钮加一个自定义属性:data-sort-type,值为"none"、"asc"、"desc"3个值中的一个,用来标记当前的排序顺序

然后,就可以给排序按钮加单击事件了:

       $('div#flightSearchResultBox').on("click", "#dc_filter_box_div ul#sortUL a[data-sort-type]", function () {
var $this = $(this);
var sortID = $this.attr("id");
var sortType = $(this).dValue("sort-type");
if (sortType=="none") {
sortType = "asc";
$this.children("i").addClass("arrow_up");
}
else if (sortType == "asc") {
sortType = "desc";
$this.children("i").removeClass("arrow_up").addClass("arrow_down");
} else if (sortType == "desc") {
sortType = "asc";
$this.children("i").removeClass("arrow_down").addClass("arrow_up");
}
$this.dValue("sort-type", sortType); //除了当前单击的排序条件外,其他所有的排序条件
var $btnSortListWithOutThis = $("#dc_filter_box_div ul#sortUL a").not($this); if (sortID == "btnSortFlightTime") {
/*按起飞时间排序*/
//去掉排序样式并将排序类型设为"none"
$btnSortListWithOutThis.find("i").removeClass("arrow_down").removeClass("arrow_up").end().dValue("sort-type", "none");
var $trList = sortTableByFlightTime(sortType);
//将原来的tr清空,再将排序后的tr插入到table的dom中
$trList.appendTo($("#dc_Table > tbody").empty());
} else if (sortID == "btnSortPrice") {
/*按价格排序*/
}
});

当点击排序按钮时,取其data-sort-type属性判断当前选中的排序顺序,如果为"none"说明没有排序过,那么按照asc来排序,如果为"asc",则说明已经按照顺序排序了,现在需要按照"desc"排序。

设定好属性后,然后就要调用sortTableByFlightTime 这个方法来排序了。

        //根据起飞时间排序
//参数:sortType=>asc或者desc;
function sortTableByFlightTime(sortType) {
var $trList = getDCTrList();//sort-field-ftime
//冒泡排序
for (var i = 0; i < $trList.length - 1; i++) {
for (var j = 0; j < $trList.length - 1 - i; j++) {
var value1 = parseInt($trList[j].attributes["data-sort-field-ftime"].nodeValue);
var value2 = parseInt($trList[j + 1].attributes["data-sort-field-ftime"].nodeValue);
if (sortType === "asc" ? value1 > value2 : value1 < value2) {
var $temp = $trList[j];
$trList[j] = null;
$trList[j] = $trList[j + 1];
$trList[j + 1] = null;
$trList[j + 1] = $temp;
}
}
}
//返回排序后的tr集合
return $trList;
}

首先,$trList变量时获取了Table下所有的tr,是一个jQuery对象=>$("#tableid tbody > tr");

然后就是经典的冒泡排序了,根据sortType来判断返回的是顺序的还是倒序的jQuery对象。

当返回了jQuery对象后,将原有table的tr清空,将此jQuery对象插入到table中即可。

 

jQuery对Table一个字段排序的更多相关文章

  1. 根据某个字段去重 根据另一个字段排序的一个SQL

    背景:一张表 有 name , crt_time , work_sts 三个字段 这张表里存在这样的数据 'a', '2018-12-18 21:37:24', '未''a', '2018-12-19 ...

  2. sql 多个字段排序,头一个字段排序完,再对第二个字段进行排序(以此类推)

    现根据num排序,num数字相同的根据时间进行排序,都是降序DESC SELECT * FROM counts ORDER BY num DESC,create_time DESC

  3. PHP二维数组按某个字段排序

    //准备 二维数组 //按一个字段排序 foreach($rank as $key=>$val){ $dos[$key] = $val['timelength']; } array_multis ...

  4. php 多维数据根据某个或多个字段排序

    实现多维数组的指定多个字段排序 上面的实例讲解了如何实现多维数组指定一个字段排序,但如果要实现指定多个字段来对数组进行排序该如何思考? 多个字段是几个?2个,3个或更多,所以这个不确定的因素需要排除. ...

  5. List多字段排序,orderBy,ThenBy

    List排序问题,orderBy,ThenBy 1.List中一个字段排序 前几天做的项目中,获取的List<T>需要用某个字段来进行排序,困扰了很久.用OrderBy解决了.具体是这样的 ...

  6. Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作

    Visual Studio 2017中使用正则修改部分内容   最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效 ...

  7. 使用 JQuery 实现将 table 按照列排序

    使用 JQuery 实现将 table 按照列排序 使用 JQuery 实现将 table 按照列排序 代码如下 <!DOCTYPE html> <html> <head ...

  8. List<T>多字段排序的一个通用类

    本文中的方法旨在解决通用的问题,非常注重效率的地方,还应该针对具体的类去写排序方法. 废话不多说,直接上代码 具体使用场景: 要排序的类 1 public class bb 2 { 3 public ...

  9. 非常强大的table根据表头排序,点击表头名称,对其内容排序

    js代码: /** * 通过表头对表列进行排序 * * @param sTableID * 要处理的表ID<table id=''> * @param iCol * 字段列id eg: 0 ...

随机推荐

  1. ubuntu 16.04 U盘多媒体不自动弹出

    如下图: 设置 --> 详细信息 --> 可移动媒体 来自为知笔记(Wiz)

  2. C++矩阵运算库推荐

    最近在几个地方都看到有人问C++下用什么矩阵运算库比较好,顺便做了个调查,做一些相关的推荐吧.主要针对稠密矩阵,有时间会再写一个稀疏矩阵的推荐. Armadillo:C++下的Matlab替代品 地址 ...

  3. Android 解压boot.img

    其实解压.打包boot.img没什么难度一看就会咯!!   1.先下附件:工具. 点击打开链接 6.0 KB, 下载次数: 60)      解压到bin文件夹里,方便以后使用.   2.解压boot ...

  4. 含有Date和Timestamp的Java和Json互相转化

    工程 代码 package com.my.json.helper; import java.text.DateFormat; import java.text.SimpleDateFormat; im ...

  5. MySQL – 导出数据成csv

    方案有很多种,我这里简单说一下: 1.  into outfile SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' FIELDS TERMI ...

  6. 正则指引-量词demo

    class Program { static void Main(string[] args) { string str = "1\"3"; var re1 = Rege ...

  7. 解密程序代写,订制服务qq:928900200

    CS461 MP 1: Due Wednesday 09/17 by 11:59 pm Fall 2014\Anyone, from the most clueless amateur to the ...

  8. Rails: No such file or directory - getcwd

    这个的意思就是你从一个删除的目录里面运行实例:rails s

  9. React Native ——实现一个简单的抓取github上的项目数据列表

    /** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; var React ...

  10. 给animator动态添加事件

    using UnityEngine; using System.Collections; public class setAnimationEvent : MonoBehaviour { public ...