1

我正在使用DataTables jQuery 插件,当我想在现有表上插入隐藏详细信息选项时遇到问题,此选项应如下所示:LINK

我的问题是表头已正确插入,但我没有看到带有加号的表列以展开并查看详细信息。

这是我的代码,您可以看到它几乎是偶然的,因为它位于我提供的链接上。

编码:

/* 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' );
        }
    } );

    } );

当我调试时,我意识到这段代码已被处理,但它没有画出它应该画的东西:

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

知道我的代码可能出了什么问题吗?如果有人在使用这个插件,你能帮忙并分享你的经验吗?

编辑:

这是我得到的图片。它应该向右移动一个位置,在那个空列中,我应该有一张图片来打开详细信息

桌子

编辑2:

我尝试将此代码与我在表中手动编写的数据一起使用,并且运行良好。

我已经厌倦了将代码放在 fnDrawCallback 函数中,但是我有 2 个标题,因为我的表格绘制了两次。

如何将它与 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] );
                } );

            }
        } );
4

1 回答 1

1

这里的问题是表根据来自响应的数据呈现数据。我想有一个 aaData 数组,其中 0 索引填充有值,它按预期插入到第一列中。

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

在您的情况下,您可以将 aoColumnDefs 选项更改为:

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

编辑覆盖 fnServerData

我的另一个想法是覆盖 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);
        }


      } );
    }

希望能帮助到你。

于 2012-08-10T21:38:46.573 回答