admin管理员组文章数量:1401673
I'm looking to sort a html table alphabetically on page load the same way you can sort an unordered list.
I tried to use the code below but did not work:
var mylist = $('#myTable');
var listitems = mylist.children('tr').get();
listitems.sort(function(a, b) {
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });
And then
<table id="myTable">
<tr>
<td>B</td>
<td>12/01/2016</td>
</tr>
<tr>
<td>A</td>
<td>12/01/2016</td>
</tr>
</table>
I want it to sort the table alphabetically by the first column in a row.
I can't find anything about this online.
I'm looking to sort a html table alphabetically on page load the same way you can sort an unordered list.
I tried to use the code below but did not work:
var mylist = $('#myTable');
var listitems = mylist.children('tr').get();
listitems.sort(function(a, b) {
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });
And then
<table id="myTable">
<tr>
<td>B</td>
<td>12/01/2016</td>
</tr>
<tr>
<td>A</td>
<td>12/01/2016</td>
</tr>
</table>
I want it to sort the table alphabetically by the first column in a row.
I can't find anything about this online.
Share Improve this question edited Jan 12, 2016 at 18:49 Shane Buckley asked Jan 12, 2016 at 18:43 Shane BuckleyShane Buckley 1341 silver badge11 bronze badges 02 Answers
Reset to default 5Your problem is this line did not return your trs:
var listitems = mylist.children('tr').get();
Modify to below and everything will work.
var listitems = mylist.find('tr');
var mylist = $('#myTable');
var listitems = mylist.find('tr');
listitems.sort(function(a, b) {
return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
<tr>
<td>B</td>
<td>12/01/2016</td>
</tr>
<tr>
<td>A</td>
<td>12/01/2016</td>
</tr>
</table>
Here is a snippet that I use. note: This is faster than using jquery find()
function sortTable(){
var table = document.getElementById("myTable");
var Arr = [];
for(var i=0, ln=table.rows.length; i<ln; i++){
var row = table.rows[i];
var firstCell = row.cells[0].textContent;
Arr.push([firstCell, row]); //temporary array
}
//sort by first column of inner arrays
Arr = Arr.sort(function(a,b) {
return a[0] > b[0];
});
for(var i=0, ln=Arr.length; i<ln; i++){
table.appendChild(Arr[i][1]);
}
Arr = null;
}
sortTable();
<table id="myTable">
<tr>
<td>d</td>
<td>12/03/2016</td>
</tr>
<tr>
<td>B</td>
<td>12/01/2016</td>
</tr>
<tr>
<td>A</td>
<td>12/04/2016</td>
</tr>
<tr>
<td>C</td>
<td>12/04/2016</td>
</tr>
</table>
本文标签: javascriptSort html table alphabetically on page loadStack Overflow
版权声明:本文标题:javascript - Sort html table alphabetically on page load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744298216a2599448.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论