Dynamic number of columns - make width equal

Dynamic number of columns - make width equal

mikepoolemikepoole Posts: 11Questions: 5Answers: 0

I have a table that is dynamically created. Is there a way to make all the column widths equal?

Would I have to get the number ofcolumns then convert this into a variable containing the percentage and then apply to columnDefs width?

Answers

  • colincolin Posts: 15,176Questions: 1Answers: 2,589

    Hi @mikepoole ,

    You could do it that way, or you could just set the header afterwards, which will force the rest of the table to follow, so something like this from the example here:

      $('#example thead th:eq(0)').css( 'width', '40%' );
    

    Cheers,

    Colin

  • mikepoolemikepoole Posts: 11Questions: 5Answers: 0

    Thanks Colin

    I'm not sure I get what you mean though - so I still need to calculate the number of columns I have built and then apply that percentage to the headers?

    How would I apply it to ALL headers?

    Would $('#example thead th:eq').css( 'width', tablePercent + '%' ); work (where tablePercent is my calculated value?

  • colincolin Posts: 15,176Questions: 1Answers: 2,589

    Hi @mikepoole ,

    Yep, so you could calculate the percentage as you said, then set all widths in a single jQuery statement. i.e.:

    $('#example thead th').css('width', '20%')
    

    Cheers,

    Colin

This discussion has been closed.