Cannot get center to work

Cannot get center to work

miltontmiltont Posts: 20Questions: 7Answers: 0

Hi,
I have the following which works great,

  <script>
            $(document).ready( function () {
            $('#myTable').DataTable(
                {
                  "order":[[1,"desc"],[2,"desc"],[3,"desc"],[4,"desc"]],                          
                  "dom":<lif<t>p>',                                    
                  lengthMenu: [[100,10,25,50,- 1],[100,10,25,50,"All"]],  
                  fixedHeader:{headerOffset:80
                } 
            );
            } );
    </script>

but every time I try to add the following to center the columns I get an error.

  columnDefs: [
      {
          targets: -1,
          className: 'dt-body-right'
      }
    ]

I have 10 columns and would like all columns except the first column to be centered.

All help appreciated.

MiltonT.

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. 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

  • miltontmiltont Posts: 20Questions: 7Answers: 0

    I can't post a test case as it will not work because of the error.

    This part works fine:

     <script>
               $(document).ready( function () {
               $('#myTable').DataTable(
                   {
                     "order":[[1,"desc"],[2,"desc"],[3,"desc"],[4,"desc"]],                         
                     "dom":<lif<t>p>',                                   
                     lengthMenu: [[100,10,25,50,- 1],[100,10,25,50,"All"]], 
                     fixedHeader:{headerOffset:80
                   }
               );
               } );
       </script>
    

    When I alter it by adding the columnDefs I get an error and the table does not display correctly:

       <script>
                $(document).ready( function () {
                $('#myTable').DataTable(
                    {
                      "order":[[1,"desc"],[2,"desc"],[3,"desc"],[4,"desc"]],
                      "dom":<lif<t>p>',                                    
                      lengthMenu: [[100,10,25,50,- 1],[100,10,25,50,"All"]],  
                      fixedHeader:{headerOffset:80} 
                      columnDefs: [
                    {
                        targets: -1,
                        className: 'dt-body-right'}
                          ]
                                           );
                } );
          </script>
    
  • kthorngrenkthorngren Posts: 20,149Questions: 26Answers: 4,736
    edited October 2021 Answer ✓

    Guessing you are getting a syntax error. Looks like you need a comma and a right brace:

    $(document).ready( function () {
      $('#myTable').DataTable(
        {
          "order":[[1,"desc"],[2,"desc"],[3,"desc"],[4,"desc"]],                        
          "dom":'<lif<t>p>',                                  
          lengthMenu: [[100,10,25,50,- 1],[100,10,25,50,"All"]],
          fixedHeader:{headerOffset:80},  // comma here
          columnDefs: [
              {
                 targets: -1,
                 className: 'dt-body-right'
              }
           ]
    
        }. // Also missing this bracket
      );
    } );
    

    Kevin

  • miltontmiltont Posts: 20Questions: 7Answers: 0

    Thankyou kthorngren, that addressed my error.

    What syntax would I use to mix the column alignments?

    i.e.
    targets: 0, className: 'dt-body-left'
    targets: 1,2,3,5,6,7,8,9, className: 'dt-body-center'
    targets: 4, className: 'dt-body-right'

  • kthorngrenkthorngren Posts: 20,149Questions: 26Answers: 4,736
    Answer ✓

    See the Conflict resolution section of the columnDefs docs for details of how to provide multiple targets.

    Kevin

  • miltontmiltont Posts: 20Questions: 7Answers: 0

    Thanks Kevin, that did the trick!

Sign In or Register to comment.