Show custom button according custom option

Show custom button according custom option

rodriformigarodriformiga Posts: 47Questions: 12Answers: 0

Hello

I created a custom button instance to use in page navigation, for example, and I would like this button to only appear according to an option that I create in my application (some tables may not have the option to go back, for example. example).

I intend to automatically add the button to the table at startup if that property is active.
it is possible? What callbak or event can I use to encode this?

Example:
My button:

$.fn.dataTable.ext.buttons.back = {
    text: 'Back'
  , className:  "dt-button-nav-back"
  , action: function ( e, dt, node, config ) {
      alert("back to:"); //need to read the custom property here and execute a action
    }
};

And my code:

$(document).on( "preInit.dt", function (e, settings) { //this or another event???
  if (settings.backUrl){
    //ADD here the code to add/show the back button
  }
});


$("#myTab").DataTable({
    "ajax": {"url": "loadData.php",  type: "POST" }
  , "backUrl": "searchData.php"
  , buttons: [ "print", "excel", "colvis"] //I don´t add button here, because it depends of a property
  , "columns": [
     { "data": "cd", title: "ID"}
    ,{ "data": "nu", title: "Code" }
    ,{ "data": "no", title: "Name" }
    ,{ "data": "fl", title: "Active" }
    ,{ "data": "tp", title: "Type" }
    ,{ "data": "dt", title: "Data" }
    ,{ "data": "hr", title: "Hour" }
    ,{ "data": "vl", title: "Value" }
    ]
});

Answers

  • rodriformigarodriformiga Posts: 47Questions: 12Answers: 0

    someone please!? help!?

  • rodriformigarodriformiga Posts: 47Questions: 12Answers: 0

    Yeeess!! I do...

    My solution:

    $.fn.dataTable.ext.buttons.back = {
        text: 'Back'
      , className:  "dt-button-nav-back"
      , action: function ( e, dt, node, config ) {
          alert("back to: "+dt.settings()[0].oInit.backUrl);
        }
    };
    
    document).on( "preInit.dt", function (e, settings) {
      var tab = $("#"+settings.sTableId).DataTable();
    
      if (settings.oInit){
        var btnCnt = tab.buttons(0, "div.dt-buttons > .dt-button").length;
    
        if (settings.oInit.actDel) { tab.button().add(btnCnt++, "del"); }
        if (settings.oInit.addUrl) { tab.button().add(btnCnt++, "ins"); }
        if (settings.oInit.backUrl){ tab.button().add(btnCnt++, "back");  }
      }
    });
    
    $("#myTab").DataTable({
        "ajax": {"url": "loadData.php",  type: "POST" }
      , "backUrl": "pageSearchData.php"
      , "addUrl": "includeData.php"
      , buttons: [ "print", "excel", "colvis"]
      , "columns": [
         { "data": "cd", title: "ID"}
        ,{ "data": "nu", title: "Code" }
        ,{ "data": "no", title: "Name" }
        ,{ "data": "fl", title: "Active" }
        ,{ "data": "tp", title: "Type" }
        ,{ "data": "dt", title: "Data" }
        ,{ "data": "hr", title: "Hour" }
        ,{ "data": "vl", title: "Value" }
        ]
    });
    
This discussion has been closed.