向数据表中的某些条目添加详细信息/子行 [英] Adding detailed information / child rows to some entries in datatables

查看:48
本文介绍了向数据表中的某些条目添加详细信息/子行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档数据表中,我们看到了如何添加子行或详细信息信息发送到我们数据(JSON/CSV文件)的每一行.但是我不希望使用扩展按钮,扩展名是空的,就像前两个数据条目一样.这就是我的工作方式:

From the docs of datatables we see how one can add child rows or detailed information to every line in our data (JSON/CSV file). However I don't want an expand button the extension is empty like the first two data entries. This is what I work with:

JavaScript

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 = $('#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']]
    } );

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

HTML

<table id="example" class="display" cellspacing="0" width="100%">
        <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>

JSON

{
  "data": [
    {
      "id": "1",
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": ""
    },
    {
      "id": "2",
      "name": "Garrett Winters",
      "position": "Accountant",
      "salary": "$170,750",
      "start_date": "2011/07/25",
      "office": "Tokyo",
      "extn": ""
    },
    {
      "id": "3",
      "name": "Ashton Cox",
      "position": "Junior Technical Author",
      "salary": "$86,000",
      "start_date": "2009/01/12",
      "office": "San Francisco",
      "extn": "1562"
    },
    {
      "id": "4",
      "name": "Cedric Kelly",
      "position": "Senior Javascript Developer",
      "salary": "$433,060",
      "start_date": "2012/03/29",
      "office": "Edinburgh",
      "extn": "6224"
    },
  ]

您可以看到条目1和2的扩展名都是空的,在这里我根本不想显示扩展按钮,只是空白.扩展名非空的其他两个仍应具有显示此信息的按钮.

As you can see both entry 1 and 2 has empty extension, and here I don't want the expand button to show up at all, just blank space. The other two where the extension is non-empty should still have the button to show this information.

推荐答案

您可以使用 createdRow()检查详细信息单元格(或多个单元格)的内容,并在该单元格不为空的情况下有条件地添加 details-control 类.

You can use createdRow() to check the content of the detail cell (or cells), and conditionally add the details-control class if that cell is not empty.

例如,它检查 extn 字段是否具有非空值

For example, this checks to see if the extn field has a non-empty value

$(document).ready(function() {
  var table = $('#example').DataTable({
    "data": data.data,
    "columns": [{
      "orderable": false,
      "data": null,
      "defaultContent": ''
    }, {
      "data": "name"
    }, {
      "data": "position"
    }, {
      "data": "office"
    }, {
      "data": "salary"
    }],
    "order": [
      [1, 'asc']
    ],
    "createdRow": function(row, data, dataIndex) {
      if (data.extn !== "") {
      $(row).find("td:eq(0)").addClass('details-control');
      }
    }
  });

这里是小提琴演示: https://jsfiddle.net/zephyr_hex/rhgL0294/20/

这篇关于向数据表中的某些条目添加详细信息/子行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆