Having trouble setting column width of dynamically generated table.

Having trouble setting column width of dynamically generated table.

rob_wrob_w Posts: 5Questions: 3Answers: 0

I have some jquery code that accepts a JSON response, and then generates a datatable based upon the response. Code works great. However, I'm having an issue getting the column widths to persist on these tables.

The code looks something like this:

//Some magic happens and I get the following list
json_result = ['spaghetti', 'poptarts', 'eggs'] 

//declare the table
var table = $( table_selector )
 table = result_table.DataTable({
                  //set two different width columns
                  columnDefs: [
                         { "width": "10%", "targets": 0 },
                         { "width": "90%", "targets": 1 }
                        ]
              });

//build the table rows based on result
for (i=0, i<json_result, i++){ 
    var new_row = <generate row html based on result>
    table.row.add(new_row).draw();
}

table.draw();
// table.columns.adjust().draw(); <-- tried this to no avail

Everything works fine...but the columns are exactly 50/50 width. Is there a way to make this column width persist?

Answers

  • kthorngrenkthorngren Posts: 21,572Questions: 26Answers: 4,997

    One thing I've found is if you have call nowrap on the table then the column widths will adjust to display the field without wrapping.

    Kevin

  • allanallan Posts: 63,873Questions: 1Answers: 10,528 Site admin

    We'd really need a link to a test case showing the issue please. There are so many factors that can effect the column width - primarily the content of the table.

    Allan

  • rob_wrob_w Posts: 5Questions: 3Answers: 0

    Apologies, I thought this was very clear via the simple example code. In the end, there was nothing special happening here. The inner content had nothing to do with this issue as I could set the columns empty and they would still render 50/50.

    In the end I eventually found that setting bAutoWidth to false was the solution to the issue I was having.

    Thanks everyone for your help!

  • allanallan Posts: 63,873Questions: 1Answers: 10,528 Site admin

    The problem with the code above is that it doesn't run. It is missing parts out that could cause the issue.

    If I modify it so it does run, it appears to work without issue: http://live.datatables.net/resavura/1/edit .

    Allan

This discussion has been closed.