How I can aligment only one column ?

How I can aligment only one column ?

RomualdRomuald Posts: 23Questions: 3Answers: 0

Hello,

I would like aligment my last column only ?

I try this code but this no work :

customize: function (doc) {
              doc.styles.tableBodyEven.alignment = 'center'
              doc.styles.tableBodyOdd.alignment = 'center' 

I must define the column I think ?

Have you a sample code ?

Thank you for help

This question has accepted answers - jump to:

Answers

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

    Hi @Romuald ,

    Something like this with would do the trick,

    Cheers,

    Colin

  • RomualdRomuald Posts: 23Questions: 3Answers: 0

    Hi @Colin,

    Thank you for your help :smile:

    I misspoke...

    I would like aligment my last column center only when I export in PDF ? (See screen)

    I must give the number of column ? How I can to do ?

    Thank you.

    Romuald

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

    Hi @Romuald ,

    This thread here should do the trick for the export column alignment.

    Cheers,

    Colin

  • RomualdRomuald Posts: 23Questions: 3Answers: 0

    Hi @Colin,

    I try this code :

    {extend: 'pdfHtml5',text: 'Export PDF',exportOptions: {orthogonal: "ExportPDF"},            
                customize: function (doc) {              
                  //alert("Test...");
                  //var iColumns = $('tableau').length;              
                  var iColumns = 5              
                  //alert(iColumns);
                  //var rowCount = document.getElementById("tableau").rows.length;
                  var rowCount = document.getElementById("tableau").rows.length;
                  alert(rowCount);
                  for (i = 0; i < rowCount; i++) { 
                    doc.content[1].table.body[i][iColumns - 1].alignment = 'center';
                    doc.content[1].table.body[i][iColumns - 2].alignment = 'center';                
                  };
    
                  doc.footer=(function(page, pages) {
                    return {
                        columns: [
                        {
                        alignment: 'right',
                        text: [
                        { text: page.toString(), italics: true },
                        ' de ',
                        { text: pages.toString(), italics: true }
                        ]
                        }
                        ],
                    margin: [40, 0]
                    }
                }); 
    

    My problem :
    - 'var rowCount = document.getElementById("tableau").rows.length;' My result is 11 lines, but I'm 226 lines.

    It's because of the pagination I think... this code give me only the rows count of the first page

    I'm no see my error ???

    Thank you for your help :smile:

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

    If you're using serverSide, then yep, that's correct, only the visible rows are exported. Otherwise, we're happy to take a look, please can you link to a test case.

    Cheers,

    Colin

  • RomualdRomuald Posts: 23Questions: 3Answers: 0

    Hi @Colin,

    I test option "serverside", but I have a error.

    My code :

    $(document).ready(function() {                    
    oTable = $('#tableau').DataTable( {
      "serverSide": true,  
        dom: 'Bfrtip',
            "buttons": [
                {extend: 'copy',text: 'Copier',},
                {extend: 'csv',text: 'Export CSV'},
                {extend: 'excel',text: 'Export Excel'},
                {extend: 'pdfHtml5',text: 'Export PDF',exportOptions: {orthogonal: "ExportPDF"},            
                customize: function (doc) {              
                  //alert("Test...");
                  //var iColumns = $('tableau').length;              
                  var iColumns = 5                            
                  //alert(iColumns);
                  //var rowCount = document.getElementById("tableau").rows.length;
                  //var rowCount = document.getElementById("tableau").rows.length;
                  var rowCount = 226;
                  alert(rowCount);
                  for (i = 0; i < rowCount; i++) { 
                    doc.content[1].table.body[i][iColumns - 1].alignment = 'center';
                    doc.content[1].table.body[i][iColumns - 2].alignment = 'center';                
                  };
    
                  doc.footer=(function(page, pages) {
                    return {
                        columns: [
                        {
                        alignment: 'right',
                        text: [
                        { text: page.toString(), italics: true },
                        ' de ',
                        { text: pages.toString(), italics: true }
                        ]
                        }
                        ],
                    margin: [40, 0]
                    }
                });              
    

    And my error :

    jquery-3.3.1.js:3818 jQuery.Deferred exception: Cannot set property 'data' of null TypeError: Cannot set property 'data' of null
    at sa (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:36:448)
    at kb (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:37:5)
    at N (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:29:134)
    at S (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:31:123)
    at ga (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:48:392)
    at e (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:92:256)
    at HTMLTableElement.<anonymous> (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:92:342)
    at Function.each (https://code.jquery.com/jquery-3.3.1.js:354:19)
    at jQuery.fn.init.each (https://code.jquery.com/jquery-3.3.1.js:189:17)
    at jQuery.fn.init.m [as dataTable] (https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js:82:462) undefined
    jQuery.Deferred.exceptionHook @ jquery-3.3.1.js:3818
    process @ jquery-3.3.1.js:3606
    setTimeout (async)
    (anonymous) @ jquery-3.3.1.js:3640
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    fire @ jquery-3.3.1.js:3406
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    ready @ jquery-3.3.1.js:3878
    completed @ jquery-3.3.1.js:3888
    jquery-3.3.1.js:3827 Uncaught TypeError: Cannot set property 'data' of null
    at sa (jquery.dataTables.min.js:36)
    at kb (jquery.dataTables.min.js:37)
    at N (jquery.dataTables.min.js:29)
    at S (jquery.dataTables.min.js:31)
    at ga (jquery.dataTables.min.js:48)
    at e (jquery.dataTables.min.js:92)
    at HTMLTableElement.<anonymous> (jquery.dataTables.min.js:92)
    at Function.each (jquery-3.3.1.js:354)
    at jQuery.fn.init.each (jquery-3.3.1.js:189)
    at jQuery.fn.init.m [as dataTable] (jquery.dataTables.min.js:82)
    sa @ jquery.dataTables.min.js:36
    kb @ jquery.dataTables.min.js:37
    N @ jquery.dataTables.min.js:29
    S @ jquery.dataTables.min.js:31
    ga @ jquery.dataTables.min.js:48
    e @ jquery.dataTables.min.js:92
    (anonymous) @ jquery.dataTables.min.js:92
    each @ jquery-3.3.1.js:354
    each @ jquery-3.3.1.js:189
    m @ jquery.dataTables.min.js:82
    h.fn.DataTable @ jquery.dataTables.min.js:164
    (anonymous) @ script.js:22
    mightThrow @ jquery-3.3.1.js:3534
    process @ jquery-3.3.1.js:3602
    setTimeout (async)
    jQuery.readyException @ jquery-3.3.1.js:3826
    (anonymous) @ jquery-3.3.1.js:3846
    mightThrow @ jquery-3.3.1.js:3534
    process @ jquery-3.3.1.js:3602
    setTimeout (async)
    (anonymous) @ jquery-3.3.1.js:3640
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    fire @ jquery-3.3.1.js:3406
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    process @ jquery-3.3.1.js:3622
    setTimeout (async)
    (anonymous) @ jquery-3.3.1.js:3640
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    fire @ jquery-3.3.1.js:3406
    fire @ jquery-3.3.1.js:3268
    fireWith @ jquery-3.3.1.js:3398
    ready @ jquery-3.3.1.js:3878
    completed @ jquery-3.3.1.js:3888
    2script.js:40 Uncaught TypeError: Cannot set property 'alignment' of undefined
    at Object.customize (script.js:40)
    at s.action (buttons.html5.min.js:27)
    at q (dataTables.buttons.min.js:13)
    at HTMLButtonElement.<anonymous> (dataTables.buttons.min.js:14)
    at HTMLButtonElement.dispatch (jquery-3.3.1.js:5183)
    at HTMLButtonElement.elemData.handle (jquery-3.3.1.js:4991)

    On the folders of sever, I must copy the other files that on my computer local ?

    Thank you for help

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

    Those export options look wrong - ExportPDF isn't defined anywhere in that code. This example here might help, it's doing that kind of thing.

    If no joy, could you link to a running test case showing the issue so we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • RomualdRomuald Posts: 23Questions: 3Answers: 0

    Hi @Colin,

    I create a test case : live.datatables.net/leqakake/1/

    Thank you for help

    Romuald

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

    Hi @Romuald ,

    I just took a look at that test case, thanks for that, and it looks as expected to me - the two right-hand columns are center aligned, while all the rest are left aligned. What makes you think it's not working?

    Cheers,

    Colin

  • RomualdRomuald Posts: 23Questions: 3Answers: 0

    Hi @Colin,

    I would like when I export PDF my file, that the columns "Windows 7" and "Windows 10" are aligned to center.

    My problem is to calculate count total of lines :

    var rowCount = document.getElementById("example").rows.length;

    This code give me only the total lines of the 1er page.

    Thank you for help

    Romuald

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

    Ah, ok. You can get the row count using count(), so something like

    var rowCount = $('#example').DataTable().rows().count();
    

    Cheers,

    Colin

  • RomualdRomuald Posts: 23Questions: 3Answers: 0

    Hi @Colin,

    I try this function :

    var rowCount = $('#example').DataTable().rows().count();

    I have a good number of lines, but if I'm filtering and that the result give me 2 line, this function give me always the lines total.

    Example :

    • Total lines in datatable : 226 lines
    • I search a element in my datatables with engine search.
    • The result give me : 2 lines
    • Total lines are wrong, I get 226 lines instead of 2 lines

    How I can to have the numbers total of lines without or with search ?

    Thank you for help.

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

    Ah, ok. You can use page.info().

  • RomualdRomuald Posts: 23Questions: 3Answers: 0

    Hi @Colin,

    Good :smiley:

    With this code, it's fine

    rowCount = info.recordsDisplay + 1;

    Thank you for help :wink:

This discussion has been closed.