如何手动分隔表格的显示数量(DataTables)?

时间:2014-09-16 09:46:02

标签: javascript jquery datatables

是否有可能手动分隔(数据)表的显示行数?

通常我们使用过滤输入,但是这些值是固定的;例如:10,25,50,100,All。

我想有一个文本字段,我可以在其中插入我想要显示的行数;例如:3,7,29等,然后点击一个按钮后,我们只显示表格的“页面”,只显示插入的行数。

我阅读了一些教程并进行了一些搜索,但无法找到任何相关内容。

这是我桌上的JavaScript(但我认为没有用):

$('#search-table').dataTable({
    "dom": "<'box-content'<'col-md-4'l><'col-md-8 text-right'f><'clearfix'>>rt<'box-content'<'col-md-5'i><'col-md-7 text-right'p><'clearfix'>>",

    "aoColumnDefs": [
        { 'bSortable': false, 'aTargets': [ 8, 9, 10 ] }
    ],

    "aLengthMenu": [[50, 100, 500, -1], [50, 100, 500, "All"]],

    "iDisplayLength": 50,
    "pagingType": "full_numbers",

    "oLanguage": {
        "sSearch": 'Filter within results: ',
        "sLengthMenu": '_MENU_'
    },
});

欢迎任何帮助!

2 个答案:

答案 0 :(得分:0)

您只需将要显示的新长度添加到_iDisplayLength属性并重绘表格。

创建数据表时,这些东西会进入完整的回调函数fnInitComplete

$('#search-table').dataTable({
    "fnInitComplete": function(oSettings,json){
        var $table = this;

        //example of how many rows to show
        var newLength = 29;         

        //add the new value to your datatable object
        oSettings._iDisplayLength = newLength;

        //redraw the table, you have to do this to see the changes
        $table.fnDraw();

    },
    //the rest of your settings
});

答案 1 :(得分:0)

直接来自数据论坛。

var oTable;

$(document).ready(function() {

    $('YourButtonIdorSelector').click( function () {
        var newDisplayLength = $("TextFieldIDOrSelectorHere").val();
        var oSettings = oTable.fnSettings();
        oSettings._iDisplayLength = newDisplayLength;
        oTable.fnDraw();
    });

    oTable = $('#search-table').dataTable({
    "dom": "<'box-content'<'col-md-4'l><'col-md-8 text-right'f><'clearfix'>>rt<'box-content'<'col-md-5'i><'col-md-7 text-right'p><'clearfix'>>",

    "aoColumnDefs": [
        { 'bSortable': false, 'aTargets': [ 8, 9, 10 ] }
    ],

    "aLengthMenu": [[50, 100, 500, -1], [50, 100, 500, "All"]],

    "iDisplayLength": 50,
    "pagingType": "full_numbers",

    "oLanguage": {
        "sSearch": 'Filter within results: ',
        "sLengthMenu": '_MENU_'
    },
});

});

var searchTable = $('#search-table').dataTable({
    "dom": "<'box-content'<'col-md-4'l><'col-md-8 text-right'f><'clearfix'>>rt<'box-content'<'col-md-5'i><'col-md-7 text-right'p><'clearfix'>>",

    "aoColumnDefs": [
        { 'bSortable': false, 'aTargets': [ 8, 9, 10 ] }
    ],

    "aLengthMenu": [[50, 100, 500, -1], [50, 100, 500, "All"]],

    "iDisplayLength": 50,
    "pagingType": "full_numbers",

    "oLanguage": {
        "sSearch": 'Filter within results: ',
        "sLengthMenu": '_MENU_'
    },
});
searchTable.fnSettings()._iDisplayLength = $("TextFieldIDOrSelectorHere").val();
searchTable.fnDraw(); //redraw the table