gpt4 book ai didi

javascript - 数据表自定义初始排序

转载 作者:行者123 更新时间:2023-11-28 19:39:51 24 4
gpt4 key购买 nike

我目前正在使用 Datatable 来显示一些数据,假设我有一个包含 ID、姓名、年龄的模型

默认情况下,当呈现页面时,表格按 id 排序。我希望它按年龄排序。

有人有这样做的想法吗?

我的代码是:

<script type="text/javascript">
$(document).ready(function() {
$("#table").dataTable({"bJQueryUI" : true,
"bProcessing" : true,
"bAutoWidth" : true,
"sPaginationType" : "full_numbers",

"oTableTools": {
"sRowSelect": "single",
"aButtons": [],
},
"sDom": 'lT<f><r>t<p>'}).columnFilter();

})
</script>

<table id='table' >
<thead>
<tr>
<th>Id</th>
<th>Age</th>
<th>Name</th>
</tr>
</thead>

<tbody>

<% Model.each do |model| %>

<tr>
<td><%= model.id %></td>
<td><%= model.age %></td>
<td><%= model.name %></td>
</tr>

<% end %>

</tbody>
</table>

最佳答案

$(document)
.ready(
function() {
$('#demotable')
.dataTable(
{
"sDom" : "<'row-fluid'<'span4'><'span8'f>r>t<'row-fluid'<'span4'><'span8'>>",
"iDisplayLength" : -1,
"aoColumns" : [ null,null,null, {
"bSortable" : false
} ],
"oTableTools" : {
"sSwfPath" : "../resources/media/csv_xls_pdf.swf",
"aButtons" : [
"copy",
"print",
{
"sExtends" : "collection",
"sButtonText" : 'Save <span class="caret" />',
"aButtons" : [
{
'sExtends' : 'csv',
'mColumns' : [
0,
1,
2,
3,
4 ]
},
{
'sExtends' : 'xls',
'mColumns' : [
0,
1,
2,
3,
4 ]
},
{
'sExtends' : 'pdf',
'mColumns' : [
0,
1,
2,
3,
4 ]
} ]
} ]
}
});

});
</script>

指定bsortable: false,如上面的 id 列代码所示。

关于javascript - 数据表自定义初始排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25303473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com