Can you "Name" your datatable ?

Can you "Name" your datatable ?

BartTechneBartTechne Posts: 24Questions: 6Answers: 0

For example:

I got multiple datatables on a site.
I need to export every table to PDF.
But when i press Export it needs to have the messageTop that stands in the h5 above the table.
Is it maybe a solution to name every datatable and gets picked up via API ?

Answers

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Just assign different ids to your h5s above your various tables.

    You can set the title to whatever you like.

    This is part of the definition of an Excel button (pdf works the same):

    {extend: "excel",
        title:    function () { return lang === 'de' ? 'Vertragsübersicht' : 'Contract Overview' },
        filename: function () { return lang === 'de' ? 'Vertragsübersicht' : 'Contract Overview' },
        messageBottom: function() {
            return tableFooter + ctrTable.rows().count();
        },
    
  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    I'm assuming this is a question related to your other thread where you are combining multiple tables into one for the export. The PDF export only knows about the one combined table. You can use messageTop as a function to gather the information you want to be displayed at the top of the export. It won't know where to add messages between tables. See tis example.

    Maybe this particular solution of merging the tables is not for you. You might need to use the customize function. See the least example on the pdfHtml5 button docs. Sorry I don't have any examples but you might find some on the forum that can be useful.

    Kevin

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

    Not sure where it came from nor how well it works but maybe this example will give you some ideas:
    https://codepen.io/RedJokingInn/pen/XMVoXL

    Kevin

  • BartTechneBartTechne Posts: 24Questions: 6Answers: 0
    edited May 2021

    Tried it towards the method of @rf1234

    My javascript is not that good...
    So when i do:

    extend: 'pdfHtml5',
                    messageTop :  function () {
                        title = document.getElementsByTagName('h5')
                        return title
                    },
                    filename: 'export',
                    footer: true
    

    It only does my first h5 in the document wich is normal. but i can not seem to say:
    for table 1 use h5 number 1 for table 2 use h5 number2

    thanks for the help !

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    That's why I recommended to use individual ids for each table and get the respective h5 by its id which is unique for each table.

  • BartTechneBartTechne Posts: 24Questions: 6Answers: 0

    Problem is all my tables are generated in a for loop. so then put an id in the for loop and put that as table id variable ?

  • BartTechneBartTechne Posts: 24Questions: 6Answers: 0

    This is also not going to work since i use the class and not the ids to display.

    pfffff im really on the verge on giving up ...

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2021

    You can use jQuery to loop through the h5s and assign different ids - e.g. counting up from 1 - n to them.

    Something like:

    var sequence = 0;
    $('h5').each(function() {
        sequence++;
        $(this).attr('id', 'id'+sequence);
    });
    

    that would assign id1, id2 ..., idn to your h5s.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    This is also not going to work since i use the class and not the ids to display.
    pfffff im really on the verge on giving up ...

    Sorry no idea what you mean. You can use a class for a different purpose, no problem. But if you want to identify your h5s you can use an id, that's all.

  • BartTechneBartTechne Posts: 24Questions: 6Answers: 0

    But this cant work because datatables does not know how to fetch wich id belongs to his datatable using the display class right ?

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    You lost me completely. Sorry.

  • BartTechneBartTechne Posts: 24Questions: 6Answers: 0
    edited May 2021

    I got 3 datatables now all with the table id empty and display as class

    my h5's in every of that table now have an ID.
    how can i let that datatable know thats the ID he needs to use ?

    <table id='' class="display table table-striped table-bordered" style="width:100%">
            <h5 id = 73> Content for the Title</h5></br>
    
  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Why are you assinging an id with value space? Not sure whether that would work at all; never seen this before. You can add and remove ids dynamically, no need to assign "empty" ids I would say.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    I have pages with multiple data tables as well. You just assign a different id to each data table. If you want to use classes instead of ids you can assign a different class to each table. Since the id or class should refer to exactly one data table I would not use classes but ids. You can find that in the examples as well.

    If you generate the html for the data tables I would use a routine that assigns a different id to each generated table to be able to identify them individually. Similar to my example above.

  • BartTechneBartTechne Posts: 24Questions: 6Answers: 0
    edited May 2021

    Exactly as i expected then... damn..

This discussion has been closed.