Fan*_*nch 5 sorting jquery datatables emptydatatext
所以问题已在这里提出,但解决方案对我不起作用(我可能做错了).我想按字母顺序("类型":"自然")对表格进行排序,但我希望空单元格位于底部(对于desc和asc).
我尝试了fbas给出的先前解决方案:
jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= 1;
else if (y == "" || y == " ") retVal= -1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}
jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= -1;
else if (y == "" || y == " ") retVal= 1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
用:
$(document).ready(function() {
$('#classement').dataTable({
"aoColumns": [
null,
null,
{ "type" : "mystring" },
{ "type" : "mystring" },
null
]
} );
} );
Run Code Online (Sandbox Code Playgroud)
有一个像| N° | Edit | Song | Singer | Url |
仅在歌曲和歌手上排序的表格.
emty单元位于底部(正如预期的那样),但现在排序没有逻辑(没有字母顺序,我应该在dataTable中使用另一个属性吗?).
有没有人有办法解决吗?
编辑:如果我们动态添加一行,如何刷新排序?
$("#example").find('tbody')
.append($('<tr>')
.append($('<td>')
.text('Boro')
)
);
Run Code Online (Sandbox Code Playgroud)
iva*_*sim 15
更新:嵌入式堆栈代码段.
我认为这aoColumns是DataTables v 1.9 的遗留选项.话虽这么说,您可能还需要使用$ .extend来包含您的自定义排序功能.
请查看下面的Stack Snippet,或者在jsfiddle上的这个现场演示.简而言之,我将name列定义为non-empty-string表初始化期间的类型.然后我jQuery.fn.dataTableExt.oSort用一个non-empty-string-asc和一个non-empty-string-desc排序函数扩展了API .看看这是否是你要找的.
Stack Snippet:
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"non-empty-string-asc": function (str1, str2) {
if(str1 == "")
return 1;
if(str2 == "")
return -1;
return ((str1 < str2) ? -1 : ((str1 > str2) ? 1 : 0));
},
"non-empty-string-desc": function (str1, str2) {
if(str1 == "")
return 1;
if(str2 == "")
return -1;
return ((str1 < str2) ? 1 : ((str1 > str2) ? -1 : 0));
}
} );
var dataTable = $('#example').dataTable({
columnDefs: [
{type: 'non-empty-string', targets: 0} // define 'name' column as non-empty-string type
]
});
dataTable.api().row.add(['John Smith', 'Intern', 'San Francisco', 19, 2011/05/25, 62000]).draw();Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<link href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.css" rel="stylesheet"/>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
</tr>
<tr>
<td></td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td></td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
</tr>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7195 次 |
| 最近记录: |