我一直在为许多项目使用
Jquery数据表,它适用于所有场景.
对于我当前的项目,我要求仅对当前页面进行排序,但默认情况下,datatable将对整个数据进行排序并重绘整个表.
我确信会有一些简单的配置来启用此功能,但除了这些来自论坛的行之外无法找到
var table = $('#example').DataTable(); // Sort by column 1 and then re-draw table .order( [[ 1,'asc' ]] ) .draw( false );
解决方法
更新1
虽然我同意rtruszk,但我仍然认为如果你没有选择或1)你的客户不愿意讨论用户体验的变化,应该找到/探索解决方案.
工作实例:http://jsfiddle.net/ebRXw/63/
通过从DataTable中过滤掉当前可见的行,我能够完成您所追求的目标.我尽力在DataTables中找到一个解决方案,其中一个可能存在,但最后我使用jQuery和哈希表来识别和过滤掉当前可见的行.
我相信这可以进一步优化.希望它能让你朝着正确的方向前进.
$(document).ready(function () { var table = $('#example').DataTable({ "columnDefs": [{ "targets": [0],"visible": false,"searchable": true }] }); // Show page 0 table.page(0).draw(false); // Sort by column 1 (Name) table.order([1,'asc']).draw(false); $("#ReloadTable").click(function () { // Clear the current filter oTable = $('#example').dataTable(); oTable.fnFilter('',0); oTable.fnFilter(''); // Reset all "visible" values in the first cell var table = $('#example').DataTable(); table.rows().indexes().each(function (idx) { var d = table.row(idx).data(); table.row(idx).data()[0] = 0; d.counter++; table.row(idx).data(d); }); }); $("#FilterCurrentPage").click(function () { // Create a hash of the index of currently visible rows var h = new Object(); var x = $('.odd,.even').filter(function () { var idx = $(this)[0]._DT_RowIndex; h[idx] = idx; return this.style.display == '' }); // update the first value based on the currently visible rows var table = $('#example').DataTable(); table.rows().indexes().each(function (idx) { var d = table.row(idx).data(); if (h.hasOwnProperty(idx)) { table.row(idx).data()[0] = 1; } d.counter++; table.row(idx).data(d); }); // filter rows with a value of 1 in the first cell oTable = $('#example').dataTable(); oTable.fnFilter(1,0); }); });
更新0
我还没有找到解决方法,但我确信存在一个.下面的代码将选择并返回数据表中当前可见的行.
var x = $('.odd,.even').filter(function () { $(this).addClass('selected'); // Select the visible rows return this.style.display == '' });
From the DataTables maintainer:
Is there an easy way to tell datatables that I want to sort and redraw only current pagination?
在DataTables 1.9-不,没有
这样做的方法,但在1.10你可以使用table.order(…).draw(
错误的);保留分页. 1.10 pre beta可以在git中找到,如果你
想给它一个bash