skip to Main Content

How to put a condition to check if data not null then "Not Found" button will appear, otherwise it will be hidden. Assuming client side received Ajax response from API.

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

2

Answers


  1. try this:

    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,
        });
    }
    
    Login or Signup to reply.
  2. In Boostrap, you can hide the button with

    $('#my-button').hide();
    

    Where #my-button needs to be the id of the button element.
    So…

    if (response.data != null){
        $('#my-button').show();
    } else {
        $('#my-button').hide();
    }
    

    Hide twitter bootstrap button

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search