简体   繁体   中英

DataTables plugin is not working when hiding a details

I am using the DataTables jQuery plugin and I am having a problem when I want to insert a hide details option on my existing table, this is how this option should look like: LINK

My problem is that table head is inserted correctly but I am not seeing a table column with plus sign to expand and see details.

Here is my code and as you can see it is almost incidental as it is on the link that I provided.

The code:

/* Formating function for row details */
function fnFormatDetails ( oTable, nTr )
{
    var aData = oTable.fnGetData( nTr );
    var sOut = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
    sOut += '<tr><td>Rendering engine:</td><td>'+aData[1]+' '+aData[4]+'</td></tr>';
    sOut += '<tr><td>Link to source:</td><td>Could provide a link here</td></tr>';
    sOut += '<tr><td>Extra info:</td><td>And any further details here (images etc)</td></tr>';
    sOut += '</table>';

    return sOut;
}

$(document).ready(function() {

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

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

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

    var oTable=$('#jphit').dataTable( {
             "sDom": 'T,C<"clear">lfrtip',
             "oTableTools": {
            "sSwfPath": "swf/copy_csv_xls_pdf.swf"
        },
             "oColVis": {
             "buttonText": "Extend table",
            "activate": "mouseover"
        },
        "aoColumnDefs": [ 
      { //"bVisible": false, "aTargets": [ 0 ],
        "bSortable": false, "aTargets": [ 0 ]     }
    ],
            "aaSorting": [[1,'asc']],
            "bProcessing": true,
            "bServerSide": true,
            "sPaginationType": "full_numbers",
            "sScrollY": "350px",
            "bDeferRender": true,
            "sAjaxSource": "increment_table.php"
        } );



    /* 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
     */
    $('#jphit tbody td img').live('click', function () {
        var nTr = $(this).parents('tr')[0];
        if ( oTable.fnIsOpen(nTr) )
        {
            /* This row is already open - close it */
            this.src = "../images/details_open.png";
            oTable.fnClose( nTr );
        }
        else
        {
            /* Open this row */
            this.src = "../images/details_close.png";
            oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
        }
    } );

    } );

As I was debugging I realized that this code is processed but its not drawing what it should draw:

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

Any idea what might went wrong with my code? And if someone is using this plugin can you please help and share your experience?

EDIT:

This is the picture that I am getting. It should be shifted one spot to the right and in that empty column I should have a pic for opening a details

表

EDIT2:

I have tried to use this code with data that I wrote manually inside the table and it is working perfectly.

I have tired to put the code inside the fnDrawCallback function but then I have 2 headers as my table is drawing twice.

How to use this with sAjaxSource?

var oTable = $('#jphit').dataTable( {
         "sDom": 'T,C<"clear">lfrtip',
             "oTableTools": {
            "sSwfPath": "swf/copy_csv_xls_pdf.swf"
        },
             "oColVis": {
             "buttonText": "Extend table",
            "activate": "mouseover"
        },
        "aoColumnDefs": [ 
      { //"bVisible": false, "aTargets": [ 0 , 2 ] ,
        "bSortable": false, "aTargets": [ 0 ] }
    ],
            "bProcessing": true,
            //"bServerSide": true,
            "sPaginationType": "full_numbers",
            "sScrollY": "350px",
            "bDeferRender": true,
            //"sAjaxSource": "live_table.php",
            "fnDrawCallback": function( oSettings )  {
                var nCloneTh = document.createElement( 'th' );
                var nCloneTd = document.createElement( 'td' );
                nCloneTd.innerHTML = '<img src="images/details_open.png" style="width:25px; height:25px;">';
                nCloneTd.className = "center";

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

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

            }
        } );

The problem here is that table renders data based on data from response. I suppose that there is aaData array where 0 index is filled with value, which is inserted in the first column as expected.

You can use this approach http://datatables.net/release-datatables/examples/ajax/null_data_source.html

In your case you could change aoColumnDefs option to:

"aoColumnDefs": [ {
     "bSortable": false, mData : null, "aTargets": [ 0 ] 
}],

EDIT Override fnServerData

The other idea I have is to override fnServerData callback, to change datasource as you need.

"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
      oSettings.jqXHR = $.ajax( {
        "dataType": 'json',
        "type": "POST",
        "url": sSource,
        "data": aoData,
        "success": function(jsonData) {
            //Here you need to shift jsonData aoData array values to right
            //to add [0] index in all values.
            //I have no possibility to test it today:( but hope it will help you
            //and Then you need to call fnCallback(jsonData) with changed jsonData

            for (var data in jsonData["aoData"]) {
               jsonData["aoData"][data].unshift(0);
            }

            fnCallback(jsonData);
        }


      } );
    }

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM