数据表子行实现

时间:2015-06-03 17:04:39

标签: jquery-plugins datatables-1.10

最近,我要求在表中添加Child行的实现。我已经浏览了几个API,发现数据表符合我的要求。截至目前,我正在Springs中实现此Web应用程序并从控制器获取数据。 那是$ resultSet。 现在这个动态数据我必须在jsp页面中呈现。在这里,我被困住了,因为我无法用datatable实现。我已经看到http://datatables.net/examples/api/row_details.html的示例,并尝试删除Ajax数据并使用c:foreach循环。但我没有运气。

那么请你告诉我如何使用数据表和dyncamic数据来显示子行。

我主要担心的是:

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": "../ajax/data/objects.txt",  
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );

我如何表示上述块。 我尝试了<table id="xx"> <c:foreach loop to iterate>

1 个答案:

答案 0 :(得分:0)

您可以使用data选项直接将数据提供给DataTables,而不是使用服务器端脚本通过Ajax返回数据。从JavaScript数据源或自定义Ajax数据get创建表时,此初始化选项非常有用。但是,数据必须是Array类型。

我不熟悉Spring框架,但我假设你可以生成一个包含JSON格式数据的字符串,并将其输出到你的页面中以分配给table_data_json。我使用了示例JSON字符串var table_data_json = '[ /* skipped */ ]';

&#13;
&#13;
/* Formatting function for row details - modify as you need */
function format ( d ) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
        '<tr>'+
            '<td>Full name:</td>'+
            '<td>'+d.name+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extension number:</td>'+
            '<td>'+d.extn+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extra info:</td>'+
            '<td>And any further details here (images etc)...</td>'+
        '</tr>'+
    '</table>';
}
 
$(document).ready(function() {
  
    var table_data_json = '[{"name":"Tiger Nixon","position":"System Architect","salary":"$320,800","start_date":"2011/04/25","office":"Edinburgh","extn":"5421"},{"name":"Garrett Winters","position":"Accountant","salary":"$170,750","start_date":"2011/07/25","office":"Tokyo","extn":"8422"},{"name":"Ashton Cox","position":"Junior Technical Author","salary":"$86,000","start_date":"2009/01/12","office":"San Francisco","extn":"1562"}]';
  
    var table = $('#example').DataTable( {
        "data": JSON.parse(table_data_json),
        "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;

如果我误解了你的问题,请告诉我,我会更新我的答案。