我正在使用DataTables插件通过ajax调用来获取和分页我的表。我的要求是,我只需要对我获取的内容进行排序,即在服务器端获取时进行客户端排序。
为了实现这一点,我使用了tablesorter和dataTables插件。我的代码看起来像这样-
$("#ProposalSearchResults").html('<table cellpadding="0" id="proposalsTable" cellspacing="0" border="1 px" class="dataTable tablesorter">');
$("#proposalsTable").tablesorter();
$("#proposalsTable").dataTable({
"bPaginate": true,
"bJQueryUI": true,
"bLengthChange": false,
"bFilter": false,
"bSort": false,
"bInfo": true,
"bAutoWidth": false,
"bServerSide":true,
"iDisplayLength": 10,
"bProcessing": true,
"sPaginationType": "full_numbers",
"aoColumns": [
{"sTitle" : "Proposal Id"},
{"sTitle" : "Release Candidate Id"},
{"sTitle" : "Proposal Description"},
{"sTitle" : "Application"},
{"sTitle" : "Requester"},
{"sTitle" : "Proposal Status"},
{"sTitle" : "Proposal Creation Date", "sType": "full_date" },
{"sTitle" : "Proposal Planned Deployment Date", "sType": "full_date" },
{"sTitle" : "Action"}
],
"sAjaxSource": "/searchProposals",
"fnServerData": function(sSource, aoData, fnCallback){
aoData.push({"name" : "searchCriteria", "value" : JSON.stringify(proposalSearchCriteria)});
$.ajax({
"dataType": "json",
"type" : "GET",
"url" : sSource,
"data" : aoData,
"success" : function (serviceOutput){
fnCallback(serviceOutput.ret);
$("#proposalsTable").trigger("update");
}
});
}
});
现在这里的问题是,因为在开始时,表的头和tbody没有形成,所以对tablesorter()的调用返回和客户端排序没有实现。但是,当我先创建and和tbody,然后通过ajax填充它时,它是有效的。我不能解码dataTables的代码,所以不知道哪个方法实际在表中绘制/写入这些too和表头,它可以被重写,以便在其中也有对tablesorter的调用。
有人能帮帮我吗。
发布于 2011-06-21 15:13:28
您应该在调用dataTables()之后调用$("#proposalsTable").tablesorter();
。通过这种方式,datatables创建了<thead>
和所有<th>
部分(所有正确的标记等等),您可以在其上附加表排序事件。如果您这样做,则没有可附加事件的<th>
,因此tablesorter()
将失败。请记住,您只能将事件附加到DOM中已经存在的元素(好的,有一些方法也可以附加到DOM准备就绪后添加的元素,如jQuery live(),但我不认为tablesorter会使用它们),在您的情况下,datatables为表创建标记。
https://stackoverflow.com/questions/6426645
复制相似问题