用Jquery实现的一个Table的帮助js

主要实现的table的辅助功能有:获得当前td的上,下,左,右的td,获得上下tr,获得当前td所处的行列等。
注意:
    表格中最好不要再套表格,否则可能导致方法失效。
    每个table必须有一个id以便有些方法使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//***********************************************************
//* author:scott qian
//* date:2011/07/23
//* description:a table helper js based on JQuery
//***********************************************************
 
/// <reference path="jquery-1.4.1.min.js" />
 
//Get the current row index(start from 0)
function GetCurrentRowIndex(currentTd) {
    return $(currentTd).parent().prevAll("tr").length;
}
 
//Get the current column index(start from 0)
function GetCurrentColumnIndex(currentTd) {
    var num = 0;
    while (true) {
        currentTd = $(currentTd).prev("td");
        if (currentTd.html() == null) {
            break;
        }
        num++;
    }
    return num;
}
 
//Get sum row count of the table
function GetTableRowCount(tableId) {
    return $("#" + tableId + " tr").length;
}
 
//Get sum column count of current row
function GetCurrentRowColumnCount(currentTd) {
    var tempTd = currentTd;
    var num = 0;
 
    //previous count of currentTd
    var prevCount = GetCurrentColumnIndex(currentTd);
    num += prevCount;
 
    //next count of currentId
    while (true) {
        tempTd = $(tempTd).next("td");
        if (tempTd.html() == null) {
            break;
        }
        num++;
    }
 
    //include currentTd itself
    return num + 1;
}
 
//Get the previous td element of current td element
function GetPrevTd(currentTd) {
    return $(currentTd).prev("td");
}
 
function GetPrevTr(currentTd) {
    return $(currentTd).parent().prev("tr");
}
 
//Get the last td element of current td element
function GetNextTd(currentTd) {
    return $(currentTd).next("td");
}
 
function GetNextTr(currentTd) {
    return $(currentTd).parent().next("tr");
}
 
//Get the td element above current td element
function GetAboveTd(currentTd) {
    var currentColumn = GetCurrentColumnIndex(currentTd);
    return GetPrevTr(currentTd).children("td:eq(" + currentColumn + ")");
}
 
//Get the td element below current td element
function GetBelowTd(currentTd) {
    var currentColumn = GetCurrentColumnIndex(currentTd);
    return GetNextTr(currentTd).children("td:eq(" + currentColumn + ")");
}
 
//Get all odd tr element
function GetOddTr(tableId) {
    return $("#" + tableId + " tr:odd");
}
 
//Get all even tr element
function GetEvenTr(tableId) {
    return $("#" + tableId + " tr:even");
}
 
//Give odd tr elements background color
function BackgroundColorOddTr(tableId, color) {
    GetOddTr(tableId).each(function () {
        $(this).css("background-color", color);
     });
}
 
//Give even tr elements background color
function BackgroundColorEvenTr(tableId, color) {
    GetEvenTr(tableId).each(function () {
        $(this).css("background-color", color);
    });
}

  

posted @   qianlifeng  阅读(938)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示