如何放置一个条件来判断如果数据不为空,则会出现"找不到"按钮,否则将隐藏.假设客户端收到来自API的AJAX响应.

enter image description here

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();
                }
            }
        ]
    });
}

推荐答案

试试这个:

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

Javascript相关问答推荐

react 页面更改类别时如何返回第0页

如何从对象嵌套数组的第二级对象中过滤出键

在JavaScript中检索一些文本

微软Edge Select 间隙鼠标退出问题

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

深嵌套的ng-container元素仍然可以在Angular 布局组件中正确渲染内容吗?

Chrome是否忽略WebAuthentication userVerification设置?""

判断表格单元格中是否存在文本框

如何添加绘图条形图图例单击角形事件

在带有背景图像和圆形的div中添加长方体阴影时的重影线

保持物品顺序的可变大小物品分配到平衡组的算法

优化Google Sheet脚本以将下拉菜单和公式添加到多行

无法使用单击按钮时的useState将数据从一个页面传递到另一个页面

编辑文本无响应.onClick(扩展脚本)

将核心模块导入另一个组件模块时存在多个主题

Use Location位置成员在HashRouter内为空

Phaserjs-创建带有层纹理的精灵层以自定义外观

警告框不显示包含HTML输入字段的总和

如何在和IF语句中使用||和&;&;?

Angel Auth Guard-用户只有在未登录时才能访问登录页面,只有在登录时才能访问其他页面