我正在使用jquery数据表,我需要通过首先显示复选框的第一列包含复选框对数据进行排序
oTable = $('#userTable', context).dataTable(
{
"sAjaxSource":'/ajax/getdata/',
"fnServerData": function ( sSource, aoData, fnCallback, oSettings )
{
oSettings.jqXHR = $.ajax(
{
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": params,
"success" : function(data)
{
fnCallback(data);
fnSortBySelected();
}
});
}
});
var fnSortBySelected = function()
{
var oSettings = oTable.fnSettings();
var i = 0;
$('td:first-child input[type=checkbox]',oTable).sort(function(a, b)
{
if(a.checked)
oTable.fnUpdate( oSettings.aoData[i]._aData, i, 0);
else
oTable.fnUpdate( oSettings.aoData[i+1]._aData, i, 0);
i++;
});
}
oTable = $('#userTable', context).dataTable({
"sAjaxSource":'/ajax/getdata/',
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": params,
"success" : function(data){
fnCallback(data);
fnSortBySelected();
}
} );
}
});
var fnSortBySelected = function()
{
var oSettings = oTable.fnSettings();
var i = 0;
$('td:first-child input[type=checkbox]',oTable).sort(function(a, b){
if(a.checked)
oTable.fnUpdate( oSettings.aoData[i]._aData, i, 0);
else
oTable.fnUpdate( oSettings.aoData[i+1]._aData, i, 0);
i++;
} );
}
答案 0 :(得分:2)
我通过在每个选中的复选框上创建隐藏列(第一列索引0)来使用Snickers回答,我将相应的隐藏列设置为"选择"然后我在隐藏列和名字列
上对表进行排序隐藏第一列使用
oTable.fnSetColumnVis( 0, false );
对于每个单击的复选框都执行此操作
$('input[tyope=checkbox]',oTable).each(function(){
var aPos = oTable.fnGetPosition($(this).parents('tr:first').get(0));
oTable.fnUpdate( 'selected',aPos, 0 );
$(this).attr('checked', true);
});
然后致电
oTable.fnSort( [ [0,'desc'],[2,'asc']] );
谢谢你们的帮助
答案 1 :(得分:0)
也许你可以试试这个:
$(document).ready(function() {
var oTable = $('#example').dataTable();
// Sort immediately with columns 0 and 1
oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
} );
答案 2 :(得分:0)
只需为您的列添加正确的aoColumnDefs:
oTable = $('#userTable', context).dataTable(
{
"aoColumnDefs": [
{ "sSortDataType": "dom-checkbox", "aTargets": [ 0 ] }
],
.....