只对表的几行进行排序 - jquery/ javascript
Sort only few rows of table - jquery/ javascript
我只想对子tr的数据进行排序,不想移动父tr。只有子 tr 会移动到下一个父级。我有一张这样的表:
<table>
<tr>
<th id="column1">Column 1</th>
<th id="column2">Column 2</th>
<th>Column 3</th>
</tr>
<tr class="parent">
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
<tr class="child">
<td>96</td>
<td>102</td>
<td>121</td>
</tr>
<tr class="child">
<td>455</td>
<td>422</td>
<td>410</td>
</tr>
<tr class="child">
<td>212</td>
<td>430</td>
<td>203</td>
</tr>
<tr class="parent">
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
<tr class="child">
<td>363</td>
<td>581</td>
<td>231</td>
</tr>
<tr class="child">
<td>632</td>
<td>115</td>
<td>212</td>
</tr>
</table>
脚本代码:
$('#column1, #column2')
.each(function(){
var th = $(this),
thIndex = th.index(),
inverse = false;
th.click(function() {
// sorting classes don't work here b/c this function gets called repeatedly - moved to afterRequest: function
table.find('tr.parent td').filter(function(){
return $(this).index() === thIndex;
}).sortElements(function(a, b){
return $.text([a]) > $.text([b]) ?
inverse ? 1 : -1
: inverse ? -1 : 1;
}, function(){
// parentNode is the element we want to move
return this.parentNode;
// this.parentNode
});
inverse = !inverse;
});
});
小提琴:演示
将
每个"部分"包装在自己的<tbody />
<tbody>
<tr class="parent"><!-- ... --></tr>
<tr class="child"><!-- ... --></tr>
<tr class="child"><!-- ... --></tr>
<tr class="child"><!-- ... --></tr>
</tbody>
<tbody>
<tr class="parent"><!-- ... --></tr>
<tr class="child"><!-- ... --></tr>
<tr class="child"><!-- ... --></tr>
</tbody>
<!-- ... -->
并对每个<tbody />
进行排序
$("tbody").each(function() {
$(this).find('tr:not(.parent) td') // ignore the "parent" row
.filter(function () {
return $(this).index() === thIndex;
}).sortElements(function (a, b) {
return $(a).text() > $(b).text() ? inverse ? -1 : 1 : inverse ? 1 : -1;
}, function () {
return this.parentNode;
});
});
例
相关文章:
- jQuery UI可排序-多连接列表拖动
- jquery Onclick函数带有导致双击的回调排序函数
- 使用Jquery Issue垂直重新排序Divs
- 免费的JQuery表排序插件(带搜索)
- 如何删除项目的可排序 JQuery
- 只对表的几行进行排序 - jquery/ javascript
- 排序 Jquery 返回的 HTML 元素
- 在可排序 - jQuery 中按标题查找元素
- 给定两个连接的可排序 JQuery 列表,我怎么知道元素被放在哪个容器上
- 可通过Ajax从回显列表中排序jQuery列表
- 重新排序JQuery Masonry中的框
- I'm试图制作一个带有可排序jquery函数的add-remove输入框来对动态创建的输入框进行排序
- 按字母顺序排序jquery结果
- 组排序JQuery列表元素
- 按字母顺序排序Jquery列表
- 用余烬排序jQuery UI后删除项目
- 排序jQuery's自动完成结果
- 带有按钮的可排序jQuery UI列表
- 是否可以对同位素进行列排序?(jQuery库)
- 重新排序jQuery "sortable"问题