javascript - 数据表子行处理空值

标签 javascript jquery html datatables

我正在使用 this example来自数据表网站以显示表格并提供扩展行和显示其他数据的能力。请参阅下面的工作代码;

/* 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”一词。而我想显示空字符串或自定义默认内容(“不可用”)。

数据表文档解释了如何使用 columns.defaultContent 处理空值类似于下面的代码,但我不确定如何将其应用于子行?

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

如有任何建议,我们将不胜感激。

最佳答案

data field除了字符串之外,还可以接受一个函数。在这种情况下,您可以使用基本函数来检查字符串值是否等于 null。我相信这样的事情应该有效。 “defaultContent”在这一点上可能完全没有必要,但我留下它以防万一。如果需要,您可以尝试通过一些测试将其删除。

$('#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。您可以将这些相同的基本检查移至该功能。我将检查移动到它们自己的 formatValue 函数,如下所示。

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;
    }
}

关于javascript - 数据表子行处理空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55283579/

相关文章:

javascript - 仅当我放置警报框但仅在 chrome 中时,AngularJs+Bootstrap+Typeahead+Ajax 才起作用

jquery - 检索给定用户的所有上传的视频

javascript - 渲染自定义自动完成结果

html - 在 CSS 中设置边框的高度

javascript - 隐藏/显示具有特定类的表行,直到具有不同类名的另一行?

javascript - 如何以编程方式触发浏览器的搜索?

javascript - Angular Bootstrap UI 模态实例的第二个 Controller

javascript - .addClass() 到 $(this) 附近但在其外部的元素

php - ajax获取下拉依赖值

html - 针对相同目录和子目录文件的相对 URL 是否以点和斜杠开头?