Print to secondary table

Print to secondary table

jalapejalape Posts: 117Questions: 2Answers: 1

Good afternoon everyone,
Based on the example: https://datatables.net/blog/2019-01-11
In the secondary table I have created a print button with the parameter:
messageTop: 'rowData.tb_nota.id',
What I intend is that it shows me when printing the content of the secondary table, the id of the main table.
Thank you

Replies

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    You will want to use messageTop as a function as shown in this example. In the function you will use something like this should work`:

                    messageTop: function () {
                        var table = $('#example').DataTable();
                        var id = $(table.table().node()).attr('id');
                        return 'Parent table id: '+id;
                    },
    

    Look at the table().node() docs for more details.

    Kevin

  • jalapejalape Posts: 117Questions: 2Answers: 1

    Thanks Kevin for answering,
    Print me:
    Parent table id: nota_tbl
    If the table is correctly the name of the main and:
    Parent table id: undefined
    If the name is wrong, but it doesn't show me the id

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    If the name is wrong, but it doesn't show me the id

    Not sure what you mean. Please provide a link to your page or a test case showing the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • jalapejalape Posts: 117Questions: 2Answers: 1

    http://javierlasen.es/easd2/login/admin_nota.php

    By clicking on the Sec field, the secondary table opens.
    Use the print button of the secondary table.

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    I'm seeing this in the Print file:

    Parent table id: nota_tbl

    Are you expecting something different?

    The code I gave you uses a hard coded table ID to get the table ID. Its redundant. You have this: var siteTable = $('#nota_tbl').DataTable( {...} );. Make sure the variable siteTable is in the global scope then you can make the function more generic like this:

    messageTop: function () {
        var id = $(siteTable.table().node()).attr('id');
        return 'Parent table id: '+id;
    },
    

    Kevin

  • jalapejalape Posts: 117Questions: 2Answers: 1

    I have globally declared siteTable and used the function, but the print message is still: Parent table id: nota_tbl

    var siteTable;
    
    messageTop: function () {
        var id = $(siteTable.table().node()).attr('id');
        return 'Parent table id: '+id;
    },
    

    I have updated the changes in:
    http://javierlasen.es/easd2/login/admin_nota.php

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    edited November 2020

    That code is getting the HTML ID of the parent table. If thats not what you want then provide specifically what you do want.

    Kevin

  • jalapejalape Posts: 117Questions: 2Answers: 1

    My intention is to be able to retrieve data from main table as curso and grupo when printing with secondary table button.
    Example:
    2020-21
    M-B
    Secondary table

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    Your original post said this:

    What I intend is that it shows me when printing the content of the secondary table, the id of the main table.

    That is now fixed. Are you saying you have changed your mind?

  • jalapejalape Posts: 117Questions: 2Answers: 1

    I have not been able to see the id of the main table.
    I haven't changed my mind, if I can read the id number from the main table, I will be able to see any field in the table, which is my intention.

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    http://javierlasen.es/easd2/login/admin_nota.php

    Your link shows the id of the parent table in the printing of the child table. Why can't you see it?

  • jalapejalape Posts: 117Questions: 2Answers: 1

    But it is not showing the Id of the main table record. In all registers it shows the same.
    Parent table id: nota_tbl

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    But it is not showing the Id of the main table record.

    Store the ID of the clicked row in a global variable and use the same function technique to return that global variable.

    Kevin

  • jalapejalape Posts: 117Questions: 2Answers: 1

    I'm afraid I don't know how to store the id of the main table record in a global variable. I've tried matching tb_nota.id to the name of a variable, but it doesn't work.
    I have tried using data concatenating tb_nota.id, but neither.

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    edited November 2020

    This is your click event:

        //Botón para desplegar la tabla secundaria
        $('#nota_tbl tbody').on('click', 'td.details-control', function () {
        //siteTable.on('responsive-display', function ( e, datatable, row, showHide, update) {
        //https://datatables.net/reference/event/responsive-display
            var tr = $(this).closest('tr');
            var row = siteTable.row( tr );
     .....
    

    You have this config for your parent Datatable:

        siteTable = $('#nota_tbl').DataTable( {
    .....
            columns: [
                {
                    className: 'details-control',
                    orderable: false,
                    data: null,
                    defaultContent: '',
                    width: '10%'
                },  
                { data: "tb_estudios.estudios" },
                { data: "tb_especialidad.especialidad" },
                { data: "tb_asignatura.asignatura" },
                { data: "tb_curso.curso" },
                { data: "tb_grupo.grupo" },
    
                { data: "tb_nota.trabajo01" },
                { data: "tb_nota.trabajo02" },
                { data: "tb_nota.trabajo03" },          
                { data: "tb_nota.trabajo04" },
                { data: "tb_nota.trabajoFinal" },
                { data: "tb_nota.parcial" },
                { data: "tb_nota.semestre" },
                { data: "tb_nota.ordinario" },
                { data: "tb_nota.extraordinario"},
                { data: "tb_alumno.eMail"},     
                { data: "tb_nota.anotacionNotas" },
                { 
                    data: 'vista_item', render: function ( data ) {
                        return data.length;
                    } 
                },
                { data: "tb_nota.id" },
                {
                    data: null, 
                    className: "center",
                    defaultContent: '<a href="" class="editor_edit">Detalles</a>'
                }
    
    
            ],
    ....
    

    I think you want this column { data: "tb_nota.id" },. You can do something like this with the row().data() API:

            var tr = $(this).closest('tr');
            var row = siteTable.row( tr );
            tb_nota_id = row.data().tb_nota.id;
    

    Make sure to declare a global variable, ie var tb_nota_id;. Then return that variable in the messageTop function.

    Kevin

  • jalapejalape Posts: 117Questions: 2Answers: 1

    Thank you very much Kevin for your time, now it has worked perfectly.
    Investigating a little more, I have been able to see that it can be put in any position and use as many messages as you need: messageTop, messageBottom.
    To be able to format the text and adjust the position a bit, I suppose it is using customize.
    I love Datatable Editor.

This discussion has been closed.