Datatables Child Row Hanlde Null Values



我正在使用DataTables网站上的此示例,以显示表格并提供余地以展开行并显示其他数据。请参阅下面的工作代码;

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

这一切都按预期工作,但是我注意到,如果子行包含任何null值,则显示" null"一词。而我想显示一个空字符串或自定义默认内容("不可用")。

DataTableS文档说明了如何使用columnss.defaultContent与下面的代码相似,但我不确定如何将其应用于子行?

$('#example').dataTable( {
  "columns": [
    null,
    null,
    null,
    {
      "data": "first_name", // can be null or undefined
      "defaultContent": "<i>Not set</i>"
    }
  ]
} );

任何建议都将不胜感激。

数据字段除了仅仅是字符串之外,还可以采用功能。在这种情况下,您可以使用基本功能检查字符串值等于NULL。我相信这样的事情应该起作用。此时,"默认符号"可能是完全不必要的,但我将其留在以防万一。如果需要,您可以尝试通过一些测试将其删除。

$('#example').dataTable( {
    "columns": [
        null,
        null,
        null,
       {
            "data": function ( row, type, set ) {
                 if(!row.property || row.property == "null" || row.property == "undefined"){
                     return "<i>Not set</i>";
                 } else {
                     return row.mavenArtifact;
                 }
             },
            "defaultContent": "<i>Not set</i>"
       }
   ]
} );

编辑:

我没有看到你在谈论孩子行。看起来格式函数正在生成HTML以显示为您的孩子行。您可以将这些相同的基本检查移至该功能。我将支票移至他们自己的格式值函数,如下所示。

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>' + formatValue(d.name) + '</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extension number:</td>'+
            '<td>' + formatValue(d.extn) + '</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extra info:</td>'+
            '<td>And any further details here (images etc)...</td>'+
        '</tr>'+
     '</table>';
}
function formatValue(value){
    if(!value || value == 'undefined' || value == "null"){
        return "<i>Not set</i>";
    } else {
        return value;
    }
}

最新更新