JQuery Datatable子行对齐与父列不匹配

时间:2017-09-25 13:13:18

标签: jquery html css datatable datatables

我真的需要你的帮助来将子行列与父行列对齐。我发现一个例子与我所做的完全匹配,所以我没有把整个代码放在这里,因为我公司有限制。所以我在这里给出了代码/示例可用的参考。 示例:JQuery Datatable with parent child relationship in same dataset. How to display it as parent child rows in the table?

我附上了路线的截图。谁能请帮忙。 注意:我尝试了一些CSS html表cellspacing / cellpadding,但它没有锻炼。enter image description here



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');
        }
    } );
} );
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

如果有人仍在寻找答案,则需要将$添加到子元素HTML。下面是显示相同内容的示例。

function format ( d ) {
  return $('<tr>'+
        '<td></td>'+
        '<td>'+d.name+'</td>'+
        '<td>'+d.position+'</td>'+
        '<td>'+d.office+'</td>'+
        '<td>'+d.extn+'</td>'+
        '<td>'+d.start_date+'</td>'+
        '<td>'+d.salary+'</td>'+
    '</tr>');
}

return语句之后的$可以解决问题。要了解更多信息,您可以访问this thread from datatable

答案 1 :(得分:0)

添加此css:

table#example table {
    padding-left: 25px!important; /* maybe you put another value instead of 25 */
}

首先尝试不使用重要的。

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;
}
table#example table {
    padding-left: 25px!important;
}
<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>