【发布时间】:2016-02-25 18:23:43
【问题描述】:
我无法对我的数据表进行排序。我有一列显示服务器中的玩家,例如“0 / 50”,当我尝试对该列进行排序时,它没有正确排序数字。它将按如下顺序显示:
- 0 / 50
- 10 / 30
- 2 / 40
- 3 / 30
- 4 / 30
- 9 / 25
我只想按第一个数字排序。例如:
- 0 / 50
- 2 / 40
- 3 / 30
- 4 / 30
- 9 / 25
- 10 / 30
我知道这可以通过将 2 个值分开到各自的列中来完成,但出于美学原因,我希望它们位于同一列中。
这是我的 HTML 布局:
<div class="row">
<div class="col-lg-12">
<table id="serverList" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Server Infomation</th>
<th>Players</th>
</tr>
</thead>
</table>
</div>
</div>
这是我的 Javascript:
$(document).ready(function() {
var t = $('#serverList').DataTable();
$.getJSON('../php/queryAll.php', function(data){
var server = [];
$.each( data, function( key, val ) {
$.each( val, function( key2, val2 ) {
server.push(val2);
});
String.prototype.replaceAll = function(str1, str2, ignore){return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);}
if(server[14] > 0)
{
hostName = server[1].replaceAll('[','<span style="color:#');
server[1] = hostName.replaceAll(']',';">');
t.row.add( [server[0],server[1],server[8] + ' / ' + server[7]]).draw( false );
}
server = [];
});
$("#loader").fadeOut(250, function(){
$(".row").fadeIn(1000);
});
});
});
感谢您提供的任何帮助。
【问题讨论】:
-
您可以通过仅包含可排序数据(0、2、3 等)的隐藏列来做到这一点。您可以使用columns.orderData 属性指定在单击显示列排序时使用该列进行排序。周围有很多例子,例如stackoverflow.com/questions/30538878/datatables-v1-10-sorting-by-hidden-column
标签: javascript jquery twitter-bootstrap sorting datatables