英文:
Tablesorter — How can I sort a column by first or last name (both ascending) rather than toggling between ascending and decending?
问题
I want to use tablesorter to sort a column (column contains first name + last name) so that clicking the header toggles between sorting by first name and sorting by last name, both in ascending order.
我想使用 tablesorter 来对一个包含名字和姓氏的列进行排序,点击标题时切换按名字升序和按姓氏升序排序。
I have found a jsfiddle (link) that is very close to what I want to do.
我找到了一个 jsfiddle(链接),非常接近我想要的功能。
It toggles between sorting by name (ascending) and sorting by percentage (descending) when the header is clicked. However, in my case, I want it to toggle between sorting first name (ascending) and last name (ascending). I want both sorts to be ascending and I'm not sure how to modify this code to make that happen. If nothing else, I can make them separate columns instead and give up on this, but I think it would look better to have the full name displayed together.
当点击标题时,它在按名称(升序)和按百分比(降序)之间切换排序。然而,在我的情况下,我希望它在名字(升序)和姓氏(升序)之间切换排序。我希望两种排序都是升序的,但我不确定如何修改这段代码以实现这一点。如果没有其他办法,我可以将它们分为两列,但我认为将全名一起显示会更好看。
I tried looking through the documentation, but I wasn't able to figure out a way.
我尝试查看了文档,但我无法找到解决方法。
英文:
I want to use tablesorter to sort a column (column contains first name + last name) so that clicking the header toggles between sorting by first name and sorting by last name, both in ascending order.
I have found a jsfiddle (link) that is very close to what I want to do.
$(function () {
var $cell;
$('#games').tablesorter({
theme: 'blue',
textExtraction: {
0: function (node, table, cellIndex) {
var $n = $(node);
// add semi-colon between values
return $n.find('.name').text() + ';' + $n.find('.perc').text();
}
},
textSorter: function (a, b) {
var x = a.split(';'),
y = b.split(';'),
i = $cell && $cell.is('.active') ? 1 : 0;
return $.tablesorter.sortNatural($.trim(x[i]), $.trim(y[i]));
},
initialized: function () {
$cell = $('#games').find('.percSort');
$cell.click(function () {
// activate percentage sort
$cell.addClass('active');
return false;
}).closest('th').click(function () {
// clicking on header outside of percSort
// inactivates the percentage sort
$cell.removeClass('active');
});
}
});
});
It toggles between sorting by name (ascending) and sorting by percentage (descending) when the header is clicked. However, in my case, I want it to toggle between sorting first name (ascending) and last name (ascending). I want both sorts to be ascending and I'm not sure how to modify this code to make that happen. If nothing else, I can make them separate columns instead and give up on this, but I think it would look better to have the full name displayed together.
I tried looking through the documentation, but I wasn't able to figure out a way.
答案1
得分: 0
只需保持HTML结构不变(<div class="xxxx"><th><td>),只更改其文本内容:
<h3>点击“%”符号以按百分比排序第一列;<br>单击外部以按名称排序</h3>
<table id="games">
<thead>
<tr>
<th>名字 <span class="percSort">&nbsp;姓氏&nbsp;</span>
</th>
<th>数值</th>
<th>动物</th>
<th>网站</th>
</tr>
</thead>
<tbody>
<tr>
<th class="gamename">
<div style="width:66%;background-color: hsla(84,100%,50%,0.7);"></div>
<span class="name">Alice</span>
<span class="perc">Petterson</span>
</th>
<td>10</td>
<td>考拉</td>
<td>http://www.google.com</td>
</tr>
<tr>
<th class="gamename">
<div style="width:46%;background-color: hsla(34,100%,50%,0.7);"></div>
<span class="name">Peter</span>
<span class="perc">Brook</span>
</th>
<td>234</td>
<td>牛</td>
<td>http://www.yahoo.com</td>
</tr>
英文:
Just keep the html structure as it is (<div class="xxxx"><th><td>), and change just its text-content:
<h3>Click on the "%" sign to sort the first column by percentages;<br>Click outside to sort by name</h3>
<table id="games">
<thead>
<tr>
<th>Firstname <span class="percSort">&nbsp;Lastname&nbsp;</span>
</th>
<th>Numeric</th>
<th>Animals</th>
<th>Sites</th>
</tr>
</thead>
<tbody>
<tr>
<th class="gamename">
<div style="width:66%;background-color: hsla(84,100%,50%,0.7);"></div>
<span class="name">Alice</span>
<span class="perc">Petterson</span>
</th>
<td>10</td>
<td>Koala</td>
<td>http://www.google.com</td>
</tr>
<tr>
<th class="gamename">
<div style="width:46%;background-color: hsla(34,100%,50%,0.7);"></div>
<span class="name">Peter</span>
<span class="perc">Brook</span>
</th>
<td>234</td>
<td>Ox</td>
<td>http://www.yahoo.com</td>
</tr>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论