Use variable in messageTop

Use variable in messageTop

silenssilens Posts: 101Questions: 40Answers: 0

I would like to be able to use the variable to which I make a log in the "success" data ['0']. name_tar in the messageTop: from the pdf

function accionesTG(idT) {
    var parametros = {
        "idT": idT,
    };

    tblAcciones = $('#tblAcciones').DataTable({
             dom: 'Bfrtip',
             buttons: [
            {
                extend: 'pdfHtml5',
               messageTop: "Tarea: "+tareaName //Aqui me gustaria usar `data['0'].nombre_tar`
            }
        ],

        "ajax": {
                "data": parametros,
                "url": "php/Pir/orea.php",
                "type": "POST",
                "dataSrc": "",
                "success": function (data) {
                    console.log(data['0'].nombre_tar); //Variable que me gustaría usar
                },
            },


            "columns": [
             {"data": "nombre_usr" ,"responsivePriority": "6" }
            ],
        });
}

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @silens ,

    You can make it a function, see the print button in this example,

    Cheers,

    Colin

  • silenssilens Posts: 101Questions: 40Answers: 0

    Thank you very much for answering, I have read the link you have posted but I still do not understand how you could use the variable of success. I do not understand much of the programming language but I try


    buttons: [ { extend: 'pdfHtml5', messageTop: function () { return data['0'].nombre_tar; }, }, { extend: 'excel', messageTop: "Tarea: "+tareaName }, { extend: 'print', messageTop: "Tarea: "+tareaName }, { extend: 'copy', messageTop: "Tarea: "+tareaName } ],
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This code was from that last example I suggested looking at:

                {
                    extend: 'print',
                    messageTop: function () {
                        printCounter++;
     
                        if ( printCounter === 1 ) {
                            return 'This is the first time you have printed this document.';
                        }
                        else {
                            return 'You have printed this document '+printCounter+' times';
                        }
                    },
                    messageBottom: null
                }
    

    It's using printCounter within the function as part of the message.

  • silenssilens Posts: 101Questions: 40Answers: 0

    in that case, he declares printcounter before invoking the method. In my case, the variable to use is declared when the function is executed, the variable that I need to use in the button is a field that I can only access in the success. It is a json field that feeds the table. The question is whether I can use a json field that feeds the table in the print button or move to pdf etc



    "ajax": { "data": parametros, "url": "php/Pir/orea.php", "type": "POST", "dataSrc": "", "success": function (data) { console.log(data['0'].nombre_tar); //Variable }, },
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You can get the last JSON response with ajax.json(), so you can use that in the same function.

This discussion has been closed.