开发者

Datatables Jquery expand / collapse and animation

I am using datatables plugin and currently the code I have expands / collapse when an image in the td is clicked but I would like to be able to click the row to expand please can anyone help with this? Here is the code:

            $(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_suppo开发者_如何学运维rt/details_close.png";
                    oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
                }
            } );
        } );

Secondly I would like the expand and collapse to be a smooth animation could someone advise me how to do that?

Thanks


If you have a div within your new row code, you can do the following. In my new rows I have a div with a class of innerDetails. Here's what I did:

Line 5648 of jquery.dataTables.js is:

$(nNewRow).insertAfter(nTr);

Change it to:

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

This is the way it should work out of the box, imo.


For the first part of your question:

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

For the second part, find this.fnOpen = function( nTr, sHtml, sClass ) in jquery.dataTables.js and then find this line inside the fnOpen method:

$(nNewRow).insertAfter(nTr);

Change it to this:

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

Or whatever your chosen animation is.

This is untested but something like it will definitely work.


My solution:

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜