【问题标题】:Datatables Jquery expand / collapse and animationDatatables Jquery展开/折叠和动画
【发布时间】:2011-09-12 14:34:54
【问题描述】:

我正在使用数据表插件,目前我的代码在单击 td 中的图像时会展开/折叠,但我希望能够单击该行来展开,请有人帮忙吗?代码如下:

            $(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";

            $('#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
             */
            $('#example tbody td').live('click', function () {
                var nTr = this.parentNode.parentNode;
                if ( this.src.match('details_close') )
                {
                    /* 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' );
                }
            } );
        } );

其次,我希望展开和折叠是一个流畅的动画,有人可以告诉我该怎么做吗?

谢谢

【问题讨论】:

  • 你能把它放到 jsFiddle 中,这样我们就可以看到你在尝试什么?

标签: jquery datatables expand collapse


【解决方案1】:

如果您的新行代码中有一个 div,您可以执行以下操作。在我的新行中,我有一个带有 innerDetails 类的 div。这是我所做的:

jquery.dataTables.js 的第 5648 行是:

$(nNewRow).insertAfter(nTr);

改成:

var innerDetails = $(nNewRow).find('div.innerDetails');
innerDetails.css('display', 'none');
$(nNewRow).insertAfter(nTr);
innerDetails.slideDown();

这是开箱即用的工作方式,imo。

【讨论】:

    【解决方案2】:

    对于您问题的第一部分:

    $('#example tbody tr').live('click', function () {...}
    

    第二部分,在jquery.dataTables.js中找到this.fnOpen = function( nTr, sHtml, sClass ),然后在fnOpen方法里面找到这一行:

    $(nNewRow).insertAfter(nTr);
    

    改成这样:

    $(nNewRow).css('display','none').insertAfter(nTr).slideDown();
    

    或者任何你选择的动画。

    这是未经测试的,但类似的东西肯定会起作用。

    【讨论】:

      【解决方案3】:

      我的解决方案:

      JS

      $(document).ready(function () {
      ......
      $('.datatable-header-footer').on('draw.dt', function () {
                      bindRowToggle();
                  });
      
                  $('.datatable-header-footer').on('processing.dt', function () {
                      bindRowToggle();
                  });
      
                  $('.datatable-header-footer').on('search.dt', function () {
                      bindRowToggle();
                  });
      
                  bindRowToggle();
              });
      
              function bindRowToggle() {
                  $('tr.parentOrder').click(function () {
                      $(this).nextAll(':lt(2)').toggle();
                  });
              }
      

      CSS

      .parentOrder {
      
      }
      
      .childOrder {
          display: none;
      }   
      

      HTML

      <table class="table datatable-header-footer text-nowrap">
      ...
      <tr class="parentOrder"></tr>
      <tr class="childOrder"></tr>
      <tr class="childOrder"></tr>
      

      【讨论】:

        猜你喜欢
        • 2011-09-18
        • 1970-01-01
        • 2012-05-15
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        相关资源
        最近更新 更多