【问题标题】:dataTable page length change issuedataTable 页面长度更改问题
【发布时间】:2014-09-24 07:44:09
【问题描述】:

我正在使用数据表 1.10。一切正常,但我遇到了以下情况。我认为 dataTable 不支持这种行为。

默认情况下我设置页长为10,然后我点击下一页,表格显示项目从11到20。现在我将页长更改为25,表格显示项目从11到35。这不是我的事情假设有。每当我更改页面长度时,我希望从第一项开始显示。

是否可以处理dataTable中的Page Length change事件并自定义该功能?

感谢阅读。希望能得到您的帮助。

var tableHdr = '<table cellpadding="0" cellspacing="0" border="0" class="display" id="alertsList">' 
     + '<thead><tr>'
     + '<th>Level</th><th>Monitor Name</th><th>Alert Message</th><th>Raised At</th><th>Action</th>'
     + '</tr></thead></table>';

$('#overview_content').html( tableHdr );

//global variable
oTable = $('#alertsList').dataTable( {
"pagingType": "full_numbers",
"bJQueryUI": true,
"aaData": alertsData,
"bAutoWidth": false,
"aaSorting" : [[3, "desc"]],
"aoColumns": [
   { "sTitle": "Level", "mData":"alert_level", "sWidth":"10%" },
   { "sTitle": "Monitor Name", "mData":"monitor_name", "sWidth":"20%" },
   { "sTitle": "Alert Message", "mData":"alert_message", "sWidth":"30%" },
   { "sTitle": "Raised At", "mData":"triggered_datetime", "sWidth":"20%"},
   { "sTitle": "Action", "mData":"id", "bSortable":false, "bSearchable":false, "sWidth":"20%"}
],
"columnDefs": [  
{
    "targets": 1,
    "data":"monitor_name",
    "render": function ( data, type, full, meta ) {
            return escapeHTML(data);
        }
},
{
    "targets": 2,
    "data":"alert_message",
    "render": function ( data, type, full, meta ) {
            if (data == null || typeof data == 'undefined')
            {
                return "";
            }
            var description = data.length > 30? data.substr(0,30) +  '...': data;
            return escapeHTML(description);
        }
},
{
    "targets": 4,
    "render": function ( data, type, full, meta ) {
        return ("<span style='cursor:pointer' id='dismiss_alert_" + full.id + "' class='dismiss'>Dismiss</span> | " +
                "<span style='cursor:pointer' id='delete_alert_" + full.id + "' class='delete'>Delete</span> | " +
                "<span style='cursor:pointer' id='details_alert_" + full.id + "' class='details'>Details</span>");
        }
} ]
} );

【问题讨论】:

    标签: javascript jquery datatable pagination jquery-datatables


    【解决方案1】:

    试试这个,我从数据表论坛找到的。

    var t = $('#table-id').dataTable();
    
    $('#length li').click(function () {
        redrawWithNewCount(t, this.id);
    });
    
    function redrawWithNewCount(t, row_count) {
        //Lifted more or less right out of the DataTables source
        var oSettings = t.fnSettings();
    
        oSettings._iDisplayLength = parseInt(row_count, 10);
        t._fnCalculateEnd(oSettings);
    
        /* If we have space to show extra rows (backing up from the end point - then do so */
        if (oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay()) {
            oSettings._iDisplayStart = oSettings.fnDisplayEnd() - oSettings._iDisplayLength;
            if (oSettings._iDisplayStart < 0) {
                oSettings._iDisplayStart = 0;
            }
        }
    
        if (oSettings._iDisplayLength == -1) {
            oSettings._iDisplayStart = 0;
        }
    
        t.fnDraw(oSettings);
    
        return t;
    }
    

    【讨论】:

    • 感谢您的回复。但我认为你误解了我的问题。我希望检测页面长度何时更改(Show[10,25..]entries 选项)以及如何构建内置代码来自定义
    • Aww,所以看看这个帖子:datatables.net/forums/discussion/542/…
    猜你喜欢
    • 1970-01-01
    • 2023-02-13
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多