Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am running into an issue where when I apply columnFilter() to jQuery Datatables, the column widths become very large. I have tried using sWidth for each aoColumn with no success. Also, I've turned off bAutoWidth, too, without success.

Here is my dataTable:

oAgentDataTable = $("#agentDataTable").dataTable({
    "bServerSide": true,
    "sAjaxSource": "Agents/GetAgents",
    "bProcessing": true,
    "aoColumns": [
        {
            "mDataProp": "FirstName"
        },
        { "mDataProp": "LastName" },
        {
            "mDataProp": "AnniversaryDate",
            "bSearchable": false,
            "bSortable": false
        },
        { "mDataProp": "Location" },
        {
            "mDataProp": "Active",
            "bSearchable": false
        },
        {
            "mDataProp": "Links",
            "bSearchable": false,
            "bSortable": false,
            "fnRender": function (oObj) {
                return "<input type='hidden' value='" + oObj.aData['ID'] + "'/>";
            }
        }
    ],
    "fnDrawCallback": function (oSettings) {
        $('#agentDataTable tbody tr').each(function () {
            $(this).click(function () {
                // Calls Agent Edit Partial
                $.get("/Agents/Edit/" + $(this).find("td:last input").val(), function (data) {
                    $("#partialContentHolder").html(data);
                    $(".datepicker").datepicker();
                    applyUnPaidFeeDataTable();
                    applayPaidFeeDataTable();
                });
            });
        });
    }
}).columnFilter({
    sPlaceHolder: "head:before",
    "aoColumns": [
        { type: "text" },
        { type: "text" },
        { type: "date-range" },
        { type: "text" },
        {
            type: "select",
            values: ["True", "False"]
        },
        null
    ]
});

Markup:

<table id="agentDataTable">
<thead>
    <tr>
        <th>
            First Name
        </th>
        <th>
            Last Name
        </th>
        <th>
            Anniversary Date
        </th>
        <th>
            Location
        </th>
        <th>
            Both
        </th>
        <th></th>
    </tr>
</thead>
<tbody>
</tbody>
<tfoot>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Anniversary Date</th>
        <th>Location</th>
        <th>Active</th>
        <th></th>
    </tr>
</tfoot>
</table>

Here is the result upon render:

Result Update:

I figured out what it was. The site.css that asp.net mvc generates had a style for inputs of:

input, textarea {
border: 1px solid #e2e2e2;
background: #fff;
color: #333;
font-size: 1.2em;
margin: 5px 0 6px 0;
padding: 5px;
width: 300px;

}

share|improve this question

2 Answers

up vote 0 down vote accepted

I think the css-style causes the wide columns. The width of the inputs seem to be defined with a fixed number (may be with !important), therefore datatables cannot compute the column widths dynamically. So look into your css-stylesheet and correct the width specifications of the inputs. Hope this helps.

share|improve this answer
Nailed it. Thanks! – Packersville Apr 1 at 17:08

I ran into this issue as well with Datatables. The easiest and cleanest fix would be to set the widths of the columns in the headers as a css class.

  <thead>
    <tr>
      <th class="firstNameColumn">
        First Name
      </th>
      <th class="lastNameColumn">
        Last Name
      </th>
      <th class="AnniversaryDataColumn">
        Anniversary Date
      </th>
      <th class="LocationColumn">
        Location
      </th>
      <th class="BothColumn">
        Both
      </th>
      <th></th>
  </tr>
</thead>

Then depending on whether you are working with a responsive UI or not you could set your exact column widths or the responsive ui min/max widths. Also make sure to bound the table as well with styling. I ended up setting a min-width for the table so that my data would always be presented properly. It's not ideal for responsive UI but when has a table ever been ideal? haha.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.