数据表是否可以对每一行而不是每一行进行排序?

时间:2014-03-03 14:58:23

标签: jquery datatables jquery-datatables

数据表是否可以对每一行而不是每一行进行排序?

该表每个客户有两行。第一个是客户信息,第二个是关于客户的评论。

<table id="datatable">
  <thead>
    <tr>
      <th>Id</th>
      <th>Name</th>
      <th>Address</th>
      <th>Grade</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>123</td>
      <td>Mr. Sample I</td>
      <td>123 Somewhere Rd.</td>
      <td>A</td>
    </tr>
    <tr>
      <td colspan="4">
        This is information about the person that and is most relevant.
      </td>
    </tr>
    <tr>
      <td>456</td>
      <td>Mrs. Sample</td>
      <td>123 No Where Rd.</td>
      <td>B</td>
    </tr>
    <tr>
      <td colspan="4">
        This is information about the person that and is most relevant.
      </td>
    </tr>
    <tr>
      <td>458</td>
      <td>Mr. Bruce Wayne</td>
      <td>123 Bat Cave Rd.</td>
      <td>B</td>
    </tr>
    <tr>
      <td colspan="4">
        This is information about the person that and is most relevant.
      </td>
    </tr>
  </tbody>
</table>

我的jQuery被定义为这个。

$(document).ready(function(){
  $('#datatable').dataTable();
}); 

1 个答案:

答案 0 :(得分:1)

与你描述的不同,但听起来你想要类似于数据表提供的hidden row example

此示例显示了通过显示和隐藏其他信息来提供其他信息的方法。示例页面还提供了一个很好的演示,但示例中的相关代码发布在下面。

var oTable;

/* Formating function for row details */
function fnFormatDetails ( 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[2]+' '+aData[5]+'</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() {
    oTable = $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "scripts/details_col.php",
        "aoColumns": [
            { "sClass": "center", "bSortable": false },
            null,
            null,
            null,
            { "sClass": "center" },
            { "sClass": "center" }
        ],
        "aaSorting": [[1, 'asc']]
    } );

    $('#example 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 = "../examples_support/details_open.png";
            oTable.fnClose( nTr );
        }
        else
        {
            /* Open this row */
            this.src = "../examples_support/details_close.png";
            oTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
        }
    } );
} );