jQuery DataTables - 行点击不在第一页以外的页面上注册

Mat*_*ell 17 javascript jquery events datatables javascript-events

我正在使用DataTables jQuery插件并在行单击上设置单击处理程序,如下所示:

$('#dt tbody tr').click(function () {
        alert('e');
});
Run Code Online (Sandbox Code Playgroud)

这适用于DataTables结果的第一页.

但是,当我移动到另一页结果时,点击处理程序根本不再注册.

我的假设是DataTables代码正在停止click向我的处理程序传播事件,但因为这只发生在第一次看起来不寻常之后的页面上.

因此,有任何人:

  1. 遇到(并且理想地解决)这个问题
  2. 找到一种跟踪jQuery/JS事件传播的好方法,以隔离事件被停止的原因

干杯

Kon*_*Kon 12

我猜测事件处理程序绑定仅应用于最初加载的行.但是一旦在标记中重新呈现行集合,事件处理程序就不复存在了.

查看jQuery的live()函数.事件处理程序的关键是所有符合选择标准的元素"现在和将来".

  • 请注意,live()已被弃用,您现在应该使用on()(对于jquery> 1.7).见http://api.jquery.com/on/ (3认同)
  • 如果在定义click()处理程序之前加载行,这是有意义的. (2认同)

Chr*_*itt 11

我在单页面应用程序上遇到此问题.除了回发后,现场方法对我有用.我的表是通过ajax填充的,用户可以将其销毁并重新创建.

为了解决这个问题,我使用了dataTables.$:"http://datatables.net/api#$"

这是我使用示例DataTables为隐藏行功能提供的修复.

$(document).ready(function() {
    /*
    * Insert a 'details' column to the table
    */
    var nCloneTh = document.createElement( 'th' );
    var nCloneTd = document.createElement( 'td' );
    nCloneTd.innerHTML = '<img src="../examples_support/details_open.png">';
    nCloneTd.className = "center";

    /* CHANGE: Remove all the expand control elements we may have added earlier
    * or else you'll add a new column for every postback
    */
    $('.expand-control').remove();

    /*
    * CHANGE: Add the expand-control class to these elements,
    * so we can remove them after a postback
    */
    $(nCloneTh).addClass('expand-control');
    $(nCloneTd).addClass('expand-control');

    $('#example thead tr').each( function () {
        this.insertBefore( nCloneTh, this.childNodes[0] );
    } );

    $('#example tbody tr').each( function () {
        this.insertBefore(  nCloneTd.cloneNode( true ), this.childNodes[0] );
    } );

    /*
    * Initialse DataTables, with no sorting on the 'details' column
    */
    var oTable = $('#example').dataTable( {
        "aoColumnDefs": [
            { "bSortable": false, "aTargets": [ 0 ] }
        ],
        "aaSorting": [[1, 'asc']]
    });

    /* Add event listener for opening and closing details
    * Note that the indicator for showing 
    * which row is open is not controlled by DataTables,
    * rather it is done here
    */

    /* CHANGE:  Here I use jQuery.dataTable.$ instead of
    * jQuery('#example tbody td img'),
    * this is what preserves the event handler on the 2nd (etc) 
    * pages after a postback
    * Note the use of on instead of live, recommended over live as of 1.7 
    */
    oTable.$('tr').find('img').on('click', function () {
        var nTr = $(this).parents('tr')[0];
        if ( oTable.fnIsOpen(nTr) )
        {
            /* This row is already open - close it */
            this.src = "../examples_support/details_open.png";
            oTable.fnClose( nTr );
        }
        else
        {
            /* Open this row */
            this.src = "../examples_support/details_close.png";
            oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
        }
    } );
} );
Run Code Online (Sandbox Code Playgroud)


BD_*_*ign 5

我的所有DataTables行中的按钮都存在同样的问题,在第一页结果后,click事件对任何按钮都不起作用.Kon给出了正确的分析(谢谢Kon),但对于那些寻找示例代码的人来说,这对我有用:

$('.myButton').live('click', function() {
    var id = $(this).closest("tr").attr("id");
    var string = 'div_id=' + id;
    alert(string);
       // string sent to processing script here
});
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!