简体   繁体   English

隐藏详细信息时,DataTables插件不起作用

[英]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 我使用的是DataTables jQuery插件,当我想在现有表上插入Hide details选项时遇到问题,这是该选项的外观: 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: 编辑2:

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. 我已经厌倦了将代码放入fnDrawCallback函数中,但是由于我的表绘制了两次,所以我有2个标题。

How to use this with sAjaxSource? 如何与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. 我想有一个aaData数组,其中0索引填充有值,该数组按预期插入到第一列中。

You can use this approach http://datatables.net/release-datatables/examples/ajax/null_data_source.html 您可以使用这种方法http://datatables.net/release-datatables/examples/ajax/null_data_source.html

In your case you could change aoColumnDefs option to: 在您的情况下,可以将aoColumnDefs选项更改为:

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

EDIT Override fnServerData 编辑覆盖fnServerData

The other idea I have is to override fnServerData callback, to change datasource as you need. 我的另一个想法是重写fnServerData回调,以根据需要更改数据源。

"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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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