如何在数据表按钮上添加条件?

问题描述 投票:0回答:1

如何设置一个条件来检查数据是否为空,然后“未找到”按钮将出现,否则将被隐藏。假设客户端收到来自 API 的 Ajax 响应。

success: function(response){
    var result;
    if (response.data != null){
        result = "dataNotNull";
    } else {
        result = "dataNull";
    }
    loadFunc(result);
}

function loadFunc(result){
    $('#myTable').DataTable({
        buttons: [{
            text: 'Missmatch',
            titleAttr: 'Download in Excel',
            action: function ( e, dt, node, config ) {
                func_expMissMatch();
            },
        }, {
            text: 'Not Found',
            titleAttr: 'Download in Excel',
            action: function ( e, dt, node, config ) {
                func_expNotFound();
            },
        }],
    });
}
jquery datatables
1个回答
0
投票

试试这个:

function loadFunc(result){
    let buttons = [];
    if (result === "dataNotNull") {
        buttons.push({
            text: 'Missmatch',
            titleAttr: 'Download in Excel',
            action: function ( e, dt, node, config ) {
                func_expMissMatch();
            },
        });
    }
    else{
        buttons.push({
            text: 'Not Found',
            titleAttr: 'Download in Excel',
            action: function ( e, dt, node, config ) {
                func_expNotFound();
            },
        });
    }
    $('#myTable').DataTable({
        buttons: buttons,
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.