JQuery 数据table 在同一数据集中具有父子关系。如何在 table 中将其显示为父子行?

JQuery Datatable with parent child relationship in same dataset. How to display it as parent child rows in the table?

我有一个嵌套数据集。数据集中很少有记录是同一数据集中其他记录的子记录。父项为 null 的记录没有任何子元素,但具有与 is 相关联的值的记录将指示其在同一数据集中的父项。我如何在 jQuery 具有父子关系的数据表中表示这一点。

我刚刚操作了公共数据集来解释我的要求。在下面的数据集示例中,名称记录 - Tiger Nixon 有两个子记录,它们在同一数据集中作为单独的记录。

如何实现的例子会有很大帮助。

{
  "data": [
    {
      "name": "Tiger Nixon",
      "parent": "null",
      "position": "System Architect",
      "salary": "0,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
    {
      "name": "Garrett Winters",
      "parent": "Tiger Nixon",
      "position": "Accountant",
      "salary": "0,750",
      "start_date": "2011/07/25",
      "office": "Tokyo",
      "extn": "8422"
    },
    {
      "name": "Ashton Cox",
      "parent": "null",
      "position": "Junior Technical Author",
      "salary": ",000",
      "start_date": "2009/01/12",
      "office": "San Francisco",
      "extn": "1562"
    },
    {
      "name": "Cedric Kelly",
      "parent": "Tiger Nixon",
      "position": "Senior Javascript Developer",
      "salary": "3,060",
      "start_date": "2012/03/29",
      "office": "Edinburgh",
      "extn": "6224"
    }
]
}

SOLUTION

您可以使用 ajax.dataSrc 将原始数据存储在 g_dataFull 中,并过滤和 return 包含 parents 的数据仅显示在主 table 中.

当 child 行在 format() 函数中时,您需要迭代整个数据集 g_dataFull 并查找并显示包含 children (this.parent === d.name ).

DEMO

var g_dataFull = [];

/* Formatting function for row details - modify as you need */
function format ( d ) {
    var html = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;" width="100%">';
      
    var dataChild = [];
    var hasChildren = false;
    $.each(g_dataFull, function(){
       if(this.parent === d.name){
          html += 
            '<tr><td>' + $('<div>').text(this.name).html() + '</td>' + 
            '<td>' +  $('<div>').text(this.position).html() + '</td>' + 
            '<td>' +  $('<div>').text(this.office).html() +'</td>' + 
            '<td>' +  $('<div>').text(this.salary).html() + '</td></tr>';
         
          hasChildren = true;
       }
    });
  
    if(!hasChildren){
        html += '<tr><td>There are no items in this view.</td></tr>';
     
    }
  
 
    html += '</table>';
    return html;
}
 
$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": {
          "url": "https://api.myjson.com/bins/3mbye",
          "dataSrc": function(d){
            
             g_dataFull = d.data;
             var dataParent = []
             $.each(d.data, function(){
                if(this.parent === "null"){
                   dataParent.push(this);  
                }
             });
            
             return dataParent;
          }
        },
         
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );
     
    // Add event listener for opening and closing details
    $('#example tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row( tr );
 
        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            // Open this row
            row.child( format(row.data()) ).show();
            tr.addClass('shown');
        }
    } );
} );
td.details-control {
    background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_open.png') no-repeat center center;
    cursor: pointer;
}
tr.shown td.details-control {
    background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_close.png') no-repeat center center;
}
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>

<table id="example" class="display">
<thead>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
    </tr>
</thead>

<tfoot>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
    </tr>
</tfoot>
</table>