gpt4 book ai didi

jquery - 使用 jQuery Table Sort 对标题数量未知的表格进行排序,然后仅对 2 个标题进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 15:20:44 25 4
gpt4 key购买 nike

场景

我在网页上有一个使用 JSP 动态生成的表格。列/标题的数量是可变的(可以是 6 或最多 40+)。我正在使用 jQuery Tablesorter对表格进行排序。该表目前应该只对标题元素 5 和 6 进行排序。

问题

使用 Tablesorter 可以让您 set certain header elements to false ,从排序选项中禁用它们。由于标题的数量是可变的,我需要一种反转选项的方法,以便我可以将排序器选项设置为 true,其余的默认情况下被禁用。要么,要么我需要一种方法来获取 header 元素的总数并运行一个循环以将它们设置为 false。

语法对我来说有点棘手,因为我不确定如何遍历以下内容:

$(document).ready(function() { 
$("table").tablesorter({
headers: {
0: {
sorter: false
},
1: {
sorter: false
}
.
.
.
}
});
});

因此,在此示例中,我只想对“原发疾病”和“风险指数”进行排序

代码

另一个小问题是我有一行需要出现在表头上方。它基本上是表格信息的更高级别的标题。 Here's a fiddle用我的代码。

CSS

th.headerSortUp { 
background-image: url(../img/small_asc.gif);
background-color: #3399FF;
}
th.headerSortDown {
background-image: url(../img/small_desc.gif);
background-color: #3399FF;
}
th.header {
background-image: url(../img/small.gif);
cursor: pointer;
font-weight: bold;
background-repeat: no-repeat;
background-position: center left;
padding-left: 20px;
border-right: 1px solid #dad9c7;
margin-left: -1px;
}

jQuery

$(document).ready(function() { 
$("#myTable").tablesorter({
headers: {
2: {
sorter: true
},
3: {
sorter: true
}
}
});
});

HTML

    <table border="3" cellspacing="2" cellpadding="3" valign="top" id="myTable" class="tablesorter">
<thead>
<tr>
<th class="stopGapTH" colspan="4">
Patient List - all
</th>
<th class="stopGapTH" colspan="99">
Clinical Adherence Information
</th>
</tr>
<tr height="30">
<th width="10%" align="middle">
Patient Name
</th>
<th width="5%" align="middle">
DOB
</th>
<th width="5%" align="middle">
Primary Disease
</th>
<th width="5%" align="middle">
Risk Index
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Asthma</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Bipolar Disorder</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Chronic Kidney Disease</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Chronic Obstructive Pulmonary Disease</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Coronary Artery Disease</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Depression</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Diabetes</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Dyslipidemia</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Heart Failure</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Hypertension</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Peripheral Vascular Disease</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Schizophrenia</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Seizure Disorder</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Transient Ischemic Attack</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Preventive Care</a>
</th>
<th class="stopGapConditionHeader" align="middle">
<a href="">Wellness</a>
</th>
</tr>
</thead>
<tbody>
<tr class="stopGapRow">
<td>
Louis Armstrong
</td>
<td>
02/01/1987
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
80%
</td>
<td class="stopGapCondition red">
42%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
90%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition red">
16%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition notApplicable">
N/A
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
40%
</td>
<td class="stopGapCondition yellow">
50%
</td>
</tr>
<tr class="stopGapRow">
<td>
Bob Barker
</td>
<td>
10/10/1951
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition yellow">
80%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
75%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
77%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
33%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
50%
</td>
</tr>
<tr class="stopGapRow">
<td>
David Brinkley
</td>
<td>
10/10/1951
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition yellow">
80%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
16%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
70%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
42%
</td>
<td class="stopGapCondition red">
40%
</td>
</tr>
<tr class="stopGapRow">
<td>
Tom Brokaw
</td>
<td>
10/10/1954
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition red">
40%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
85%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
70%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
57%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition notApplicable">
N/A
</td>
<td class="stopGapCondition yellow">
71%
</td>
<td class="stopGapCondition red">
40%
</td>
</tr>
<tr class="stopGapRow">
<td>
Cad2 Sandy
</td>
<td>
01/01/1955
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
75%
</td>
<td class="stopGapCondition yellow">
62%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
63%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
60%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition notApplicable">
N/A
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
60%
</td>
<td class="stopGapCondition yellow">
66%
</td>
</tr>
<tr class="stopGapRow">
<td>
Cad3 Sandy
</td>
<td>
01/01/1955
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
60%
</td>
<td class="stopGapCondition yellow">
75%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
63%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
57%
</td>
<td class="stopGapCondition yellow">
50%
</td>
</tr>
<tr class="stopGapRow">
<td>
Cad4 Sandy
</td>
<td>
01/01/1955
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition red">
33%
</td>
<td class="stopGapCondition red">
37%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
63%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition notApplicable">
N/A
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
80%
</td>
<td class="stopGapCondition yellow">
83%
</td>
</tr>
<tr class="stopGapRow">
<td>
Cad5 Sandy
</td>
<td>
01/01/1955
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition red">
40%
</td>
<td class="stopGapCondition red">
33%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
75%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
63%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
85%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
14%
</td>
<td class="stopGapCondition yellow">
50%
</td>
</tr>
<tr class="stopGapRow">
<td>
Alex Trebek
</td>
<td>
10/10/1951
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition red">
33%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
16%
</td>
<td class="stopGapCondition yellow">
75%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
63%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
33%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
80%
</td>
<td class="stopGapCondition red">
40%
</td>
</tr>
<tr class="stopGapRow">
<td>
Chucka Woolerya
</td>
<td>
10/15/1951
</td>
<td>
NA
</td>
<td>
NA
</td>
<td class="stopGapCondition yellow">
83%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition yellow">
66%
</td>
<td class="stopGapCondition red">
42%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
54%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
71%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition green">
100%
</td>
<td class="stopGapCondition red">
0%
</td>
<td class="stopGapCondition yellow">
50%
</td>
<td class="stopGapCondition yellow">
50%
</td>
</tr>
</tbody>
</table>

编辑:因此标题“患者列表 - 所有”和“临床依从性信息”是标题 0 和 1。因此标题“原发疾病”和“风险级别”实际上是 5 和 6。

EDIT2:需要使用 IE8+。

最佳答案

如果您尝试我的 tablesorter fork ,也许会更容易在 github 上,它允许你 apply class names to the header禁用排序。然后你需要做的就是这样:

$('table thead th')
.addClass('sorter-false')
.filter(':eq(2), :eq(3)').removeClass('sorter-false');

好的,试试这个方法(demo):

var headers = {},
$table = $('#myTable'), i,
l = $table.find('thead th').length,

// columns 0 and 1 are the top row "Patient List - all"
// and "Clinical Adherence Information"
sortcolumns = [2, 3, 4, 5];

// build headers object; based on sortcolumn selections
for (i = 0; i < l; i++) {
if ($.inArray(i, sortcolumns) < 0) {
headers[i] = {
sorter: false
}
}
}
$table.tablesorter({
widgets: ['zebra'],
headers: headers // headers object built above
});

关于jquery - 使用 jQuery Table Sort 对标题数量未知的表格进行排序,然后仅对 2 个标题进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9592094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com