使用 jQuery 按列值对正文元素进行排序
Sorting tbody elements by column value with jQuery
我正在尝试对表格进行排序 - 因此当用户单击表格标题时,它将按升序/降序排序。我已经到了可以根据列值对表进行排序的程度。但是,我有表格行的分组(每个表正文两行),我想根据每个表体第一行的列中的值对列进行排序,但是当它对表重新排序时,它希望它重新排序表正文,而不是表行。
<table width="100%" id="my-tasks" class="gen-table">
<thead>
<tr>
<th class="sortable"><p>Name</p></th>
<th class="sortable"><p>Project</p></th>
<th class="sortable"><p>Priority</p></th>
<th class="sortable"><p>%</p></th>
</tr>
</thead>
<tbody>
<tr class="sortable-row" id="44">
<td><p>dfgdf</p></td><td><p>Test</p></td>
<td><p>1</p></td><td><p>0</p></td>
</tr>
<tr>
<td></td>
<td colspan="3"><p>asdfds</p></td>
</tr>
</tbody>
<tbody>
<tr class="sortable-row" id="43">
<td><p>a</p></td>
<td><p>Test</p></td>
<td><p>1</p></td>
<td><p>11</p></td>
</tr>
<tr>
<td></td>
<td colspan="3"><p>asdf</p></td>
</tr>
</tbody>
<tbody>
<tr class="sortable-row" id="40">
<td><p>Filter Tasks</p></td>
<td><p>Propel</p></td>
<td><p>10</p></td>
<td><p>10</p></td>
</tr>
<tr>
<td></td>
<td colspan="3"><p>Add a button to filter tasks.</p></td>
</tr>
</tbody>
</table>
使用以下 JavaScript:
jQuery(document).ready(function () {
jQuery('thead th').each(function(column) {
jQuery(this).addClass('sortable').click(function() {
var findSortKey = function($cell) {
return $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase();
};
var sortDirection = jQuery(this).is('.sorted-asc') ? -1 : 1;
var $rows = jQuery(this).parent().parent().parent().find('.sortable-row').get();
jQuery.each($rows, function(index, row) {
row.sortKey = findSortKey(jQuery(row).children('td').eq(column));
});
$rows.sort(function(a, b) {
if (a.sortKey < b.sortKey) return -sortDirection;
if (a.sortKey > b.sortKey) return sortDirection;
return 0;
});
jQuery.each($rows, function(index, row) {
jQuery('#propel-my-tasks').append(row);
row.sortKey = null;
});
jQuery('th').removeClass('sorted-asc sorted-desc');
var $sortHead = jQuery('th').filter(':nth-child(' + (column + 1) + ')');
sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');
jQuery('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
});
});
});
您需要
对tbody
元素进行排序,而不是对row
元素进行排序。 你在问题的描述中说了你自己,但你的代码实际上是对行进行排序,而不是对行进行排序。
次要问题是您的排序将所有内容视为字符串,当将 1 位数字字符串 ("2") 与两位字符串 ("10") 排序时会中断。
要修复,请替换以下内容:
var $rows = jQuery(this).parent().parent().parent()
.find('.sortable-row').get();
jQuery.each($rows, function(index, row) {
row.sortKey = findSortKey(jQuery(row).children('td').eq(column));
});
有了这个:
var $tbodies = jQuery(this).parent().parent().parent()
.find('.sortable-row').parent().get();
jQuery.each($tbodies, function(index, tbody) {
var x = findSortKey(jQuery(tbody).find('tr > td').eq(column));
var z = ~~(x); // if integer, z == x
tbody.sortKey = (z == x) ? z : x;
});
然后在整个脚本中将$rows替换为$tbodies,并使用 tbody 进行行。
例:
http://jsbin.com/oxuva5
我强烈推荐jQuery插件 http://tablesorter.com/而不是自己推出。
它功能齐全,支持良好。
相关文章:
- 如何使用jquery对元素进行排序和定位
- 根据输入文本按元素排序,AngularJS
- 将元素排序到数组的顶部
- Scriptaculous排序表和句柄的问题(元素排序不正确)
- 如何使用jquery随机元素排序
- 是对集合中的所有元素排序,还是只对添加的元素排序
- 在case语句中对表元素排序时出现问题
- 类似于mason .js或同位素.js的打包布局,不按现有元素排序
- 如何根据子元素的值对父元素排序?
- 如何捕捉元素排序使用jQuery sortable
- Javascript:对数组的数组按每个内部数组中的第二个元素排序
- JS基于子元素对父元素排序
- 如何将Firebase子元素中的值与其他元素排序
- 工具提示在元素排序后停止工作
- 遍历元素以对其子元素排序
- 元素排序例程在Firefox中工作,但在Chrome中崩溃
- Javascript按数组中的元素排序
- JS:基于数据元素排序列表项
- 如何按三个元素排序
- JQ UI排序表:停止排序表内的一些元素排序